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.

181 lines
4.5 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. if (0)//CLSID_MsgListView == rclsid)
  57. {
  58. hres = _Module.GetClassObject(rclsid, riid, ppvObj);
  59. }
  60. else
  61. {
  62. hres = CClassFactory_Create(rclsid, riid, ppvObj);
  63. }
  64. DllRelease();
  65. return hres;
  66. }
  67. /*****************************************************************************
  68. *
  69. * DllCanUnloadNow
  70. *
  71. * OLE entry point. Fail iff there are outstanding refs.
  72. *
  73. * There is an unavoidable race condition between DllCanUnloadNow
  74. * and the creation of a new IClassFactory: Between the time we
  75. * return from DllCanUnloadNow() and the caller inspects the value,
  76. * another thread in the same process may decide to call
  77. * DllGetClassObject, thus suddenly creating an object in this DLL
  78. * when there previously was none.
  79. *
  80. * It is the caller's responsibility to prepare for this possibility;
  81. * there is nothing we can do about it.
  82. *
  83. *****************************************************************************/
  84. STDMETHODIMP DllCanUnloadNow(void)
  85. {
  86. HRESULT hres;
  87. ENTERCRITICAL;
  88. hres = g_cRef ? S_FALSE : S_OK;
  89. if (S_OK == hres)
  90. {
  91. hres = (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  92. }
  93. TraceMsg(TF_WMTHEME, "DllCanUnloadNow() returning hres=%#08lx. (S_OK means yes)", hres);
  94. LEAVECRITICAL;
  95. return hres;
  96. }
  97. /*
  98. // Table of all window classes we register so we can unregister them
  99. // at DLL unload.
  100. const LPCTSTR c_rgszClasses[] = {
  101. // g_cszPopServiceWndClass
  102. };
  103. // Since we are single-binary, we have to play it safe and do
  104. // this cleanup (needed only on NT, but harmless on Win95).
  105. #define UnregisterWindowClasses() \
  106. SHUnregisterClasses(HINST_THISDLL, c_rgszClasses, ARRAYSIZE(c_rgszClasses))
  107. */
  108. /*****************************************************************************
  109. *
  110. * Entry32
  111. *
  112. * DLL entry point.
  113. *
  114. * BUGBUG -- On a thread detach, must check if the thread owns any
  115. * global timeouts. If so, we must transfer the timeout to another
  116. * thread or something.
  117. *
  118. *****************************************************************************/
  119. STDAPI_(BOOL) DllEntry(HINSTANCE hinst, DWORD dwReason, LPVOID lpReserved)
  120. {
  121. static s_hresOle = E_FAIL;
  122. switch (dwReason)
  123. {
  124. case DLL_PROCESS_ATTACH:
  125. {
  126. #ifdef DEBUG
  127. CcshellGetDebugFlags();
  128. #endif
  129. InitializeCriticalSection(&g_csDll);
  130. g_hinst = hinst;
  131. DisableThreadLibraryCalls(hinst);
  132. break;
  133. }
  134. case DLL_PROCESS_DETACH:
  135. {
  136. DeleteCriticalSection(&g_csDll);
  137. if (INVALID_HANDLE_VALUE != g_hLogFile)
  138. {
  139. CloseHandle(g_hLogFile);
  140. }
  141. break;
  142. }
  143. }
  144. return 1;
  145. }