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.

138 lines
4.2 KiB

  1. // --------------------------------------------------------------------------------
  2. // Dllmain.cpp
  3. // --------------------------------------------------------------------------------
  4. #include "pch.hxx"
  5. #define DEFINE_STRCONST
  6. #include "strconst.h"
  7. #include "listen.h"
  8. #include "shared.h"
  9. // --------------------------------------------------------------------------------
  10. // Globals - Object count and lock count
  11. // --------------------------------------------------------------------------------
  12. CRITICAL_SECTION g_csDllMain={0};
  13. CRITICAL_SECTION g_csDBListen={0};
  14. SYSTEM_INFO g_SystemInfo={0};
  15. LONG g_cRef=0;
  16. LONG g_cLock=0;
  17. HINSTANCE g_hInst=NULL;
  18. IMalloc *g_pMalloc=NULL;
  19. BOOL g_fAttached = FALSE;
  20. BOOL g_fIsWinNT=FALSE;
  21. // --------------------------------------------------------------------------------
  22. // Win32 Dll Entry Point
  23. // --------------------------------------------------------------------------------
  24. EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved)
  25. {
  26. // Locals
  27. OSVERSIONINFO Version;
  28. // Process Attach
  29. if (DLL_PROCESS_ATTACH == dwReason)
  30. {
  31. // Set g_hInst
  32. g_hInst = hInst;
  33. // Get Mall
  34. CoGetMalloc(1, &g_pMalloc);
  35. // Set Version
  36. Version.dwOSVersionInfoSize = sizeof(Version);
  37. // Get Version
  38. if (GetVersionEx(&Version) && Version.dwPlatformId == VER_PLATFORM_WIN32_NT)
  39. g_fIsWinNT = TRUE;
  40. else
  41. g_fIsWinNT = FALSE;
  42. // Initialize Global Critical Sections
  43. InitializeCriticalSection(&g_csDllMain);
  44. InitializeCriticalSection(&g_csDBListen);
  45. g_fAttached = TRUE;
  46. // Get System Info
  47. GetSystemInfo(&g_SystemInfo);
  48. // Don't tell me about thread attaches/detaches
  49. SideAssert(DisableThreadLibraryCalls(hInst));
  50. }
  51. // Otherwise, process detach
  52. else if (DLL_PROCESS_DETACH == dwReason)
  53. {
  54. // Delete Global Critical Sections
  55. g_fAttached = FALSE;
  56. DeleteCriticalSection(&g_csDllMain);
  57. DeleteCriticalSection(&g_csDBListen);
  58. }
  59. // Done
  60. return(TRUE);
  61. }
  62. // --------------------------------------------------------------------------------
  63. // DllAddRef
  64. // --------------------------------------------------------------------------------
  65. ULONG DllAddRef(void)
  66. {
  67. TraceCall("DllAddRef");
  68. return (ULONG)InterlockedIncrement(&g_cRef);
  69. }
  70. // --------------------------------------------------------------------------------
  71. // DllRelease
  72. // --------------------------------------------------------------------------------
  73. ULONG DllRelease(void)
  74. {
  75. TraceCall("DllRelease");
  76. return (ULONG)InterlockedDecrement(&g_cRef);
  77. }
  78. // --------------------------------------------------------------------------------
  79. // DllCanUnloadNow
  80. // --------------------------------------------------------------------------------
  81. STDAPI DllCanUnloadNow(void)
  82. {
  83. // Tracing
  84. TraceCall("DllCanUnloadNow");
  85. if(!g_fAttached) // critacal sections was deleted (or not created): we defently can be unloaded
  86. return S_OK;
  87. // Thread Safety
  88. EnterCriticalSection(&g_csDllMain);
  89. // Can We Unload
  90. HRESULT hr = (0 == g_cRef && 0 == g_cLock) ? S_OK : S_FALSE;
  91. // Thread Safety
  92. LeaveCriticalSection(&g_csDllMain);
  93. // Done
  94. return(hr);
  95. }
  96. // --------------------------------------------------------------------------------
  97. // DllRegisterServer
  98. // --------------------------------------------------------------------------------
  99. STDAPI DllRegisterServer(void)
  100. {
  101. // Trace
  102. TraceCall("DllRegisterServer");
  103. // Register
  104. return(CallRegInstall(g_hInst, g_hInst, c_szReg, NULL));
  105. }
  106. // --------------------------------------------------------------------------------
  107. // DllUnregisterServer
  108. // --------------------------------------------------------------------------------
  109. STDAPI DllUnregisterServer(void)
  110. {
  111. // Trace
  112. TraceCall("DllUnregisterServer");
  113. // UnRegister
  114. return(CallRegInstall(g_hInst, g_hInst, c_szUnReg, NULL));
  115. }