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.

157 lines
4.6 KiB

  1. #include "pch.hxx"
  2. #define DEFINE_STRING_CONSTANTS
  3. #include <commctrl.h>
  4. #include "dllmain.h"
  5. #include "shared.h"
  6. #include "strconst.h"
  7. #include "demand.h"
  8. #define ICC_FLAGS (ICC_LISTVIEW_CLASSES|ICC_PROGRESS_CLASS|ICC_NATIVEFNTCTL_CLASS)
  9. // --------------------------------------------------------------------------------
  10. // Globals - Object count and lock count
  11. // --------------------------------------------------------------------------------
  12. CRITICAL_SECTION g_csDllMain = {0};
  13. LONG g_cRef = 0;
  14. LONG g_cLock = 0;
  15. HINSTANCE g_hInstImp = NULL;
  16. LPMALLOC g_pMalloc = NULL;
  17. SYSTEM_INFO g_SystemInfo={0};
  18. OSVERSIONINFO g_OSInfo={0};
  19. BOOL g_fAttached = FALSE;
  20. inline BOOL fIsNT5() { return((g_OSInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) && (g_OSInfo.dwMajorVersion >= 5)); }
  21. // --------------------------------------------------------------------------------
  22. // Debug Globals
  23. // --------------------------------------------------------------------------------
  24. #ifdef DEBUG
  25. DWORD dwDOUTLevel=0;
  26. DWORD dwDOUTLMod=0;
  27. DWORD dwDOUTLModLevel=0;
  28. #endif
  29. static HINSTANCE s_hInst = NULL;
  30. // --------------------------------------------------------------------------------
  31. // InitGlobalVars
  32. // --------------------------------------------------------------------------------
  33. void InitGlobalVars(void)
  34. {
  35. INITCOMMONCONTROLSEX icex = { sizeof(icex), ICC_FLAGS };
  36. // Initialize Global Critical Sections
  37. InitializeCriticalSection(&g_csDllMain);
  38. g_fAttached = TRUE;
  39. // Create OLE Task Memory Allocator
  40. CoGetMalloc(1, &g_pMalloc);
  41. Assert(g_pMalloc);
  42. // Initialize Demand-loaded Libs
  43. InitDemandLoadedLibs();
  44. InitCommonControlsEx(&icex);
  45. }
  46. // --------------------------------------------------------------------------------
  47. // FreeGlobalVars
  48. // --------------------------------------------------------------------------------
  49. void FreeGlobalVars(void)
  50. {
  51. // Free libraries that demand.cpp loaded
  52. FreeDemandLoadedLibs();
  53. // Release Global Memory allocator
  54. SafeRelease(g_pMalloc);
  55. // Delete Global Critical Sections
  56. g_fAttached = FALSE;
  57. DeleteCriticalSection(&g_csDllMain);
  58. }
  59. // --------------------------------------------------------------------------------
  60. // Dll Entry Point
  61. // --------------------------------------------------------------------------------
  62. int APIENTRY DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved)
  63. {
  64. // Handle Attach - detach reason
  65. switch (dwReason)
  66. {
  67. case DLL_PROCESS_ATTACH:
  68. // Set global instance handle
  69. s_hInst = hInst;
  70. // Initialize Global Variables
  71. InitGlobalVars();
  72. g_hInstImp = LoadLangDll(s_hInst, c_szOEResDll, fIsNT5());
  73. // No Thread Attach Stuff
  74. // SideAssert(DisableThreadLibraryCalls(hInst));
  75. // Done
  76. break;
  77. case DLL_PROCESS_DETACH:
  78. // Free Global Variables
  79. FreeGlobalVars();
  80. // Done
  81. break;
  82. }
  83. // Done
  84. return TRUE;
  85. }
  86. // --------------------------------------------------------------------------------
  87. // DllAddRef
  88. // --------------------------------------------------------------------------------
  89. ULONG DllAddRef(void)
  90. {
  91. InterlockedIncrement(&g_cRef);
  92. return g_cRef;
  93. }
  94. // --------------------------------------------------------------------------------
  95. // DllRelease
  96. // --------------------------------------------------------------------------------
  97. ULONG DllRelease(void)
  98. {
  99. InterlockedDecrement(&g_cRef);
  100. return g_cRef;
  101. }
  102. // --------------------------------------------------------------------------------
  103. // DllCanUnloadNow
  104. // --------------------------------------------------------------------------------
  105. STDAPI DllCanUnloadNow(void)
  106. {
  107. HRESULT hr;
  108. if(!g_fAttached) // critacal sections was deleted (or not created): we defently can be unloaded
  109. return S_OK;
  110. EnterCriticalSection(&g_csDllMain);
  111. DebugTrace("DllCanUnloadNow: %s - Reference Count: %d, LockServer Count: %d\n", __FILE__, g_cRef, g_cLock);
  112. hr = (0 == g_cRef && 0 == g_cLock) ? S_OK : S_FALSE;
  113. LeaveCriticalSection(&g_csDllMain);
  114. return hr;
  115. }
  116. STDAPI DllRegisterServer(void)
  117. {
  118. HRESULT hr;
  119. hr = CallRegInstall(s_hInst, s_hInst, c_szReg, NULL);
  120. return(hr);
  121. }
  122. STDAPI DllUnregisterServer(void)
  123. {
  124. HRESULT hr;
  125. hr = CallRegInstall(s_hInst, s_hInst, c_szUnReg, NULL);
  126. return(hr);
  127. }