Source code of Windows XP (NT5)
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.

176 lines
4.3 KiB

  1. /*****************************************************************************\
  2. FILE: dllmain.cpp
  3. DESCRIPTION:
  4. This file will take care of the DLL lifetime.
  5. BryanSt 4/4/2000 (Bryan Starbuck)
  6. Copyright (C) Microsoft Corp 2000-2000. All rights reserved.
  7. \*****************************************************************************/
  8. #include "priv.h"
  9. extern HANDLE g_hLogFile;
  10. /*****************************************************************************
  11. *
  12. * Dynamic Globals. There should be as few of these as possible.
  13. *
  14. * All access to dynamic globals must be thread-safe.
  15. *
  16. *****************************************************************************/
  17. ULONG g_cRef = 0; // Global reference count
  18. CRITICAL_SECTION g_csDll; // The shared critical section
  19. #ifdef DEBUG
  20. DWORD g_TlsMem = 0xffffffff;
  21. #endif // DEBUG
  22. CComModule _Module;
  23. BEGIN_OBJECT_MAP(ObjectMap)
  24. //OBJECT_ENTRY(CLSID_MsgListView, CMsgListView)
  25. END_OBJECT_MAP()
  26. /*****************************************************************************
  27. *
  28. * DllAddRef / DllRelease
  29. *
  30. * Maintain the DLL reference count.
  31. *
  32. *****************************************************************************/
  33. void DllAddRef(void)
  34. {
  35. InterlockedIncrement((LPLONG)&g_cRef);
  36. }
  37. void DllRelease(void)
  38. {
  39. InterlockedDecrement((LPLONG)&g_cRef);
  40. }
  41. /*****************************************************************************
  42. *
  43. * DllGetClassObject
  44. *
  45. * OLE entry point. Produces an IClassFactory for the indicated GUID.
  46. *
  47. * The artificial refcount inside DllGetClassObject helps to
  48. * avoid the race condition described in DllCanUnloadNow. It's
  49. * not perfect, but it makes the race window much smaller.
  50. *
  51. *****************************************************************************/
  52. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppvObj)
  53. {
  54. HRESULT hres;
  55. DllAddRef();
  56. hres = CClassFactory_Create(rclsid, riid, ppvObj);
  57. DllRelease();
  58. return hres;
  59. }
  60. /*****************************************************************************
  61. *
  62. * DllCanUnloadNow
  63. *
  64. * OLE entry point. Fail iff there are outstanding refs.
  65. *
  66. * There is an unavoidable race condition between DllCanUnloadNow
  67. * and the creation of a new IClassFactory: Between the time we
  68. * return from DllCanUnloadNow() and the caller inspects the value,
  69. * another thread in the same process may decide to call
  70. * DllGetClassObject, thus suddenly creating an object in this DLL
  71. * when there previously was none.
  72. *
  73. * It is the caller's responsibility to prepare for this possibility;
  74. * there is nothing we can do about it.
  75. *
  76. *****************************************************************************/
  77. STDMETHODIMP DllCanUnloadNow(void)
  78. {
  79. HRESULT hres;
  80. ENTERCRITICAL;
  81. hres = g_cRef ? S_FALSE : S_OK;
  82. if (S_OK == hres)
  83. {
  84. hres = (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  85. }
  86. TraceMsg(TF_WMTHEME, "DllCanUnloadNow() returning hres=%#08lx. (S_OK means yes)", hres);
  87. LEAVECRITICAL;
  88. return hres;
  89. }
  90. /*
  91. // Table of all window classes we register so we can unregister them
  92. // at DLL unload.
  93. const LPCTSTR c_rgszClasses[] = {
  94. // g_cszPopServiceWndClass
  95. };
  96. // Since we are single-binary, we have to play it safe and do
  97. // this cleanup (needed only on NT, but harmless on Win95).
  98. #define UnregisterWindowClasses() \
  99. SHUnregisterClasses(HINST_THISDLL, c_rgszClasses, ARRAYSIZE(c_rgszClasses))
  100. */
  101. /*****************************************************************************
  102. *
  103. * Entry32
  104. *
  105. * DLL entry point.
  106. *
  107. *****************************************************************************/
  108. STDAPI_(BOOL) DllEntry(HINSTANCE hinst, DWORD dwReason, LPVOID lpReserved)
  109. {
  110. static s_hresOle = E_FAIL;
  111. switch (dwReason)
  112. {
  113. case DLL_PROCESS_ATTACH:
  114. {
  115. #ifdef DEBUG
  116. CcshellGetDebugFlags();
  117. #endif
  118. InitializeCriticalSection(&g_csDll);
  119. g_hinst = hinst;
  120. DisableThreadLibraryCalls(hinst);
  121. SHFusionInitializeFromModuleID(hinst, 124);
  122. break;
  123. }
  124. case DLL_PROCESS_DETACH:
  125. {
  126. DeleteCriticalSection(&g_csDll);
  127. if (INVALID_HANDLE_VALUE != g_hLogFile)
  128. {
  129. CloseHandle(g_hLogFile);
  130. }
  131. SHFusionUninitialize();
  132. break;
  133. }
  134. }
  135. return 1;
  136. }