Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. dllmain.c
  5. Abstract:
  6. DLL entry point. Does nothing for now. We may add to it later.
  7. Author:
  8. Dragos C. Sambotin (dragoss) 10-Aug-2000
  9. --*/
  10. // Private nt headers.
  11. //
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. // Public windows headers.
  16. //
  17. #include <windows.h>
  18. //+---------------------------------------------------------------------------
  19. // DLL Entry Point
  20. //
  21. // DllMain should do as little work as possible. Here's why:
  22. // 1. Incorrectly assuming that thread attach/detach are 1:1. (explain)
  23. // 2. Performance
  24. // a) touching pages (#3 below)
  25. // b) assume you will be loaded by someone who is performance-critical.
  26. //
  27. // 1. For every process that the DLL is attached to, DllMain gets called
  28. // with DLL_PROCESS_ATTACH. For any new threads created or destroyed
  29. // after DLL_PROCESS_ATTACH, DllMain is called with DLL_THREAD_ATTACH
  30. // and DLL_THREAD_DETACH events. Since it is rare that a DLL controls
  31. // or even knows about the clients that use it, it shouldn't assume
  32. // that DllMain is called only once (or even a small number of times).
  33. // In fact, you should assume the worst case (that it is called a lot)
  34. // and code for that case. This is not unrealistic either. If your
  35. // DLL gets attached to services.exe for example, you will be hit
  36. // with a lot of thread attach/detach events. If you don't need these
  37. // events (and you shouldn't) your DllMain code needs to get paged in
  38. // (assuming it's not paged in) and called.
  39. //
  40. // 2. Over time, people tend to lose sight of why and when DLLs are loaded.
  41. // Further, as more APIs are added to the DLL the likelihood that the
  42. // DLL will be loaded increases. (i.e. It becomes more useful.) It
  43. // is your responsibility to keep the performance of your DLL at a level
  44. // compatible with your most demanding (performance wise) client. For
  45. // example: Say a very performance-critical client needs to use a small
  46. // piece of functionality in your DLL. If you've done things in DllMain
  47. // (like create heaps, or access the registry, etc.) that don't strictly
  48. // need to be done to access that small piece of functionality, then
  49. // it is wasteful to do so and may be the straw that broke the camel's
  50. // back in terms of your client deciding your DLL is "too heavy" to be
  51. // used. For the functionality in your DLL to "scale" from your first
  52. // very simple client to the Nth performance-critical client, you've got
  53. // to keep DllMain absolutely lean and mean.
  54. //
  55. // 3. Fewer code in DllMain means fewer pages touched when your DLL is
  56. // loaded. If your DLL is loaded at all during boot of the OS or
  57. // an application, this means faster startup times. Let's say it again
  58. // in another way -- "the more code you add to DllMain, the slower the
  59. // OS or application boots up". You may think now that your DLL won't
  60. // be loaded during boot. I'll bet most of the developers of the DLLs
  61. // that are now loaded during boot thought the same thing in the
  62. // beginning. ;-) As your DLL becomes more useful, it gets used by
  63. // more and more parts of the system.
  64. //
  65. BOOL
  66. WINAPI
  67. DllMain (
  68. HINSTANCE hinst,
  69. DWORD dwReason,
  70. LPVOID pvReserved
  71. )
  72. {
  73. if (DLL_PROCESS_ATTACH == dwReason)
  74. {
  75. }
  76. else if (DLL_PROCESS_DETACH == dwReason)
  77. {
  78. }
  79. return TRUE;
  80. }