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.

221 lines
6.1 KiB

  1. // --------------------------------------------------------------------------------
  2. // Dllmain.cpp
  3. // Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  4. // Steven J. Bailey
  5. // --------------------------------------------------------------------------------
  6. #include "pch.hxx"
  7. #define DEFINE_STRING_CONSTANTS
  8. #define DEFINE_STRCONST
  9. #define DEFINE_PROPSYMBOLS
  10. #define DEFINE_TRIGGERS
  11. #include "msoert.h"
  12. #include "Mimeole.h"
  13. #include <advpub.h>
  14. #include "dllmain.h"
  15. #include "init.h"
  16. // --------------------------------------------------------------------------------
  17. // Globals - Object count and lock count
  18. // --------------------------------------------------------------------------------
  19. CRITICAL_SECTION g_csDllMain={0};
  20. LONG g_cRef=0;
  21. LONG g_cLock=0;
  22. HINSTANCE g_hInst=NULL;
  23. IMalloc *g_pMalloc=NULL;
  24. // --------------------------------------------------------------------------------
  25. // Debug Globals
  26. // --------------------------------------------------------------------------------
  27. // --------------------------------------------------------------------------------
  28. // InitGlobalVars
  29. // --------------------------------------------------------------------------------
  30. void InitGlobalVars(void)
  31. {
  32. // Locals
  33. SYSTEM_INFO rSystemInfo;
  34. // Initialize Global Critical Sections
  35. InitializeCriticalSection(&g_csDllMain);
  36. // Create OLE Task Memory Allocator
  37. CoGetMalloc(1, &g_pMalloc);
  38. Assert(g_pMalloc);
  39. }
  40. // --------------------------------------------------------------------------------
  41. // FreeGlobalVars
  42. // --------------------------------------------------------------------------------
  43. void FreeGlobalVars(void)
  44. {
  45. DeleteCriticalSection(&g_csDllMain);
  46. SafeRelease(g_pMalloc);
  47. }
  48. // --------------------------------------------------------------------------------
  49. // Win32 Dll Entry Point
  50. // --------------------------------------------------------------------------------
  51. EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved)
  52. {
  53. // Handle Attach - detach reason
  54. switch (dwReason)
  55. {
  56. case DLL_PROCESS_ATTACH:
  57. g_hInst = hInst;
  58. InitGlobalVars();
  59. SideAssert(DisableThreadLibraryCalls(hInst));
  60. break;
  61. case DLL_PROCESS_DETACH:
  62. FreeGlobalVars();
  63. break;
  64. }
  65. // Done
  66. return TRUE;
  67. }
  68. // --------------------------------------------------------------------------------
  69. // DllAddRef
  70. // --------------------------------------------------------------------------------
  71. ULONG DllAddRef(void)
  72. {
  73. TraceCall("DllAddRef");
  74. if (g_cRef == 0 && !g_fInitialized)
  75. InitGWNoteThread(TRUE);
  76. return (ULONG)InterlockedIncrement(&g_cRef);
  77. }
  78. // --------------------------------------------------------------------------------
  79. // DllRelease
  80. // --------------------------------------------------------------------------------
  81. ULONG DllRelease(void)
  82. {
  83. TraceCall("DllRelease");
  84. return (ULONG)InterlockedDecrement(&g_cRef);
  85. }
  86. // --------------------------------------------------------------------------------
  87. // DllCanUnloadNow
  88. // --------------------------------------------------------------------------------
  89. STDAPI DllCanUnloadNow(void)
  90. {
  91. EnterCriticalSection(&g_csDllMain);
  92. HRESULT hr = (0 == g_cRef && 0 == g_cLock) ? S_OK : S_FALSE;
  93. if (hr==S_OK && g_fInitialized)
  94. InitGWNoteThread(FALSE);
  95. LeaveCriticalSection(&g_csDllMain);
  96. return hr;
  97. }
  98. // --------------------------------------------------------------------------------
  99. // CallRegInstall - Self-Registration Helper
  100. // --------------------------------------------------------------------------------
  101. HRESULT CallRegInstall(LPCSTR szSection)
  102. {
  103. // Locals
  104. HRESULT hr=S_OK;
  105. HINSTANCE hAdvPack=NULL;
  106. REGINSTALL pfnri;
  107. // TraceCAll
  108. TraceCall("CallRegInstall");
  109. // Load ADVPACK.DLL
  110. hAdvPack = LoadLibraryA("ADVPACK.DLL");
  111. if (NULL == hAdvPack)
  112. {
  113. hr = TraceResult(TYPE_E_CANTLOADLIBRARY);
  114. goto exit;
  115. }
  116. // Get Proc Address for registration util
  117. pfnri = (REGINSTALL)GetProcAddress(hAdvPack, achREGINSTALL);
  118. if (NULL == pfnri)
  119. {
  120. hr = TraceResult(TYPE_E_CANTLOADLIBRARY);
  121. goto exit;
  122. }
  123. // Call the self-reg routine
  124. IF_FAILEXIT(hr = pfnri(g_hInst, szSection, NULL));
  125. exit:
  126. // Cleanup
  127. SafeFreeLibrary(hAdvPack);
  128. // Done
  129. return hr;
  130. }
  131. // --------------------------------------------------------------------------------
  132. // DllRegisterServer
  133. // --------------------------------------------------------------------------------
  134. STDAPI DllRegisterServer(void)
  135. {
  136. // Locals
  137. HRESULT hr=S_OK;
  138. // Trace
  139. TraceCall("DllRegisterServer");
  140. // Register my self
  141. IF_FAILEXIT(hr = CallRegInstall("Reg"));
  142. exit:
  143. // Done
  144. return hr;
  145. }
  146. // --------------------------------------------------------------------------------
  147. // DllUnregisterServer
  148. // --------------------------------------------------------------------------------
  149. STDAPI DllUnregisterServer(void)
  150. {
  151. // Locals
  152. HRESULT hr=S_OK;
  153. // Trace
  154. TraceCall("DllUnregisterServer");
  155. // UnRegister
  156. IF_FAILEXIT(hr = CallRegInstall("UnReg"));
  157. exit:
  158. // Done
  159. return hr;
  160. }
  161. // --------------------------------------------------------------------------------
  162. // Override new operator
  163. // --------------------------------------------------------------------------------
  164. void * __cdecl operator new(UINT cb)
  165. {
  166. LPVOID lpv = 0;
  167. lpv = CoTaskMemAlloc(cb);
  168. if (lpv)
  169. {
  170. #ifdef DEBUG
  171. memset(lpv, 0xFF, cb);
  172. #endif
  173. }
  174. return lpv;
  175. }
  176. // --------------------------------------------------------------------------------
  177. // Override delete operator
  178. // --------------------------------------------------------------------------------
  179. #ifndef WIN16
  180. void __cdecl operator delete(LPVOID pv)
  181. #else
  182. void __cdecl operator delete(PVOID pv)
  183. #endif
  184. {
  185. CoTaskMemFree(pv);
  186. }