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.

131 lines
3.5 KiB

  1. /*****************************************************************************
  2. *
  3. * Powertoy.cpp - Powertoy bookkeeping
  4. *
  5. *****************************************************************************/
  6. #include "priv.h"
  7. #define DECL_CRTFREE
  8. #include <crtfree.h>
  9. /*****************************************************************************
  10. *
  11. * Dynamic Globals. There should be as few of these as possible.
  12. *
  13. * All access to dynamic globals must be thread-safe.
  14. *
  15. *****************************************************************************/
  16. ULONG g_cRef = 0; /* Global reference count */
  17. CRITICAL_SECTION g_csDll; /* The shared critical section */
  18. HINSTANCE g_hinst = NULL;
  19. #ifdef DEBUG
  20. DWORD g_TlsMem = 0xffffffff;
  21. #endif // DEBUG
  22. /*****************************************************************************
  23. *
  24. * DllAddRef / DllRelease
  25. *
  26. * Maintain the DLL reference count.
  27. *
  28. *****************************************************************************/
  29. void DllAddRef(void)
  30. {
  31. InterlockedIncrement((LPLONG)&g_cRef);
  32. }
  33. void DllRelease(void)
  34. {
  35. InterlockedDecrement((LPLONG)&g_cRef);
  36. }
  37. /*****************************************************************************
  38. *
  39. * DllGetClassObject
  40. *
  41. * OLE entry point. Produces an IClassFactory for the indicated GUID.
  42. *
  43. * The artificial refcount inside DllGetClassObject helps to
  44. * avoid the race condition described in DllCanUnloadNow. It's
  45. * not perfect, but it makes the race window much smaller.
  46. *
  47. *****************************************************************************/
  48. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppvObj)
  49. {
  50. HRESULT hres;
  51. DllAddRef();
  52. hres = CFactory_Create(rclsid, riid, ppvObj);
  53. DllRelease();
  54. return hres;
  55. }
  56. /*****************************************************************************
  57. *
  58. * DllCanUnloadNow
  59. *
  60. * OLE entry point. Fail iff there are outstanding refs.
  61. *
  62. * There is an unavoidable race condition between DllCanUnloadNow
  63. * and the creation of a new IClassFactory: Between the time we
  64. * return from DllCanUnloadNow() and the caller inspects the value,
  65. * another thread in the same process may decide to call
  66. * DllGetClassObject, thus suddenly creating an object in this DLL
  67. * when there previously was none.
  68. *
  69. * It is the caller's responsibility to prepare for this possibility;
  70. * there is nothing we can do about it.
  71. *
  72. *****************************************************************************/
  73. STDMETHODIMP DllCanUnloadNow(void)
  74. {
  75. HRESULT hres;
  76. ENTERCRITICAL;
  77. hres = g_cRef ? S_FALSE : S_OK;
  78. TraceMsg(TF_LIFE, "DllCanUnloadNow() returning hres=%#08lx. (S_OK means yes)", hres);
  79. LEAVECRITICAL;
  80. return hres;
  81. }
  82. /*****************************************************************************
  83. *
  84. * Entry32
  85. *
  86. * DLL entry point.
  87. *
  88. * On a thread detach, must check if the thread owns any
  89. * global timeouts. If so, we must transfer the timeout to another
  90. * thread or something.
  91. *
  92. *****************************************************************************/
  93. STDAPI_(BOOL) DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID lpReserved)
  94. {
  95. static s_hresOle = E_FAIL;
  96. switch (dwReason)
  97. {
  98. case DLL_PROCESS_ATTACH:
  99. InitializeCriticalSection(&g_csDll);
  100. g_hinst = hinst;
  101. DisableThreadLibraryCalls(hinst);
  102. SHFusionInitializeFromModule(hinst);
  103. break;
  104. case DLL_PROCESS_DETACH:
  105. SHFusionUninitialize();
  106. DeleteCriticalSection(&g_csDll);
  107. break;
  108. }
  109. return 1;
  110. }