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.

277 lines
7.4 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WBEM Framework Instance provider
  6. //
  7. // Purpose: Contains DLL entry points. Also has code that controls
  8. // when the DLL can be unloaded by tracking the number of
  9. // objects and locks as well as routines that support
  10. // self registration.
  11. //
  12. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  13. //
  14. //***************************************************************************
  15. #include "precomp.h"
  16. #include <dllunreg.h>
  17. #include <DllCommon.h>
  18. #include <initguid.h>
  19. #include "FactoryRouter.h"
  20. #include "ResourceManager.h"
  21. #include "timerqueue.h"
  22. #include <shutdownevent.h>
  23. #include <volumechange.h>
  24. HMODULE ghModule;
  25. // {04788120-12C2-498d-83C1-A7D92E677AC6}
  26. DEFINE_GUID(CLSID_CimWinProviderA,
  27. 0x4788120, 0x12c2, 0x498d, 0x83, 0xc1, 0xa7, 0xd9, 0x2e, 0x67, 0x7a, 0xc6);
  28. // {A3E41207-BE04-492a-AFF0-19E880FF7545}
  29. DEFINE_GUID(CLSID_ShutdownEventProvider,
  30. 0xa3e41207, 0xbe04, 0x492a, 0xaf, 0xf0, 0x19, 0xe8, 0x80, 0xff, 0x75, 0x45);
  31. // {E2CBCB87-9C07-4523-A78F-061499C83987}
  32. DEFINE_GUID(CLSID_VolumeChangeEventProvider,
  33. 0xe2cbcb87, 0x9c07, 0x4523, 0xa7, 0x8f, 0x6, 0x14, 0x99, 0xc8, 0x39, 0x87);
  34. #define PROVIDER_NAME L"WMIPCIMA"
  35. //====================================================================================
  36. // initialize class globals
  37. //====================================================================================
  38. CFactoryRouterData g_FactoryRouterData;
  39. CShutdownEventFactory* gp_ShutdownEventFactory = NULL;
  40. CVolumeChangeFactory* gp_VolumeChangeFactory = NULL;
  41. CTimerQueue CTimerQueue :: s_TimerQueue ;
  42. CResourceManager CResourceManager::sm_TheResourceManager ;
  43. //Count number of objects and number of locks.
  44. long g_cLock = 0;
  45. //***************************************************************************
  46. //
  47. // DllGetClassObject
  48. //
  49. // Purpose: Called by Ole when some client wants a class factory. Return
  50. // one only if it is the sort of class this DLL supports.
  51. //
  52. //***************************************************************************
  53. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  54. {
  55. HRESULT hr = S_OK;
  56. try
  57. {
  58. if ( CLSID_CimWinProviderA == rclsid )
  59. {
  60. hr = CommonGetClassObject(riid, ppv, PROVIDER_NAME, g_cLock);
  61. }
  62. else
  63. {
  64. hr = g_FactoryRouterData.DllGetClassObject( rclsid, riid, ppv ) ;
  65. }
  66. }
  67. catch ( ... )
  68. {
  69. hr = E_OUTOFMEMORY;
  70. }
  71. return hr;
  72. }
  73. //***************************************************************************
  74. //
  75. // DllCanUnloadNow
  76. //
  77. // Purpose: Called periodically by Ole in order to determine if the
  78. // DLL can be freed.
  79. //
  80. // Return: S_OK if there are no objects in use and the class factory
  81. // isn't locked.
  82. //
  83. //***************************************************************************
  84. STDAPI DllCanUnloadNow()
  85. {
  86. SCODE sc = S_FALSE;
  87. try
  88. {
  89. // It is OK to unload if there are no locks on the
  90. // class factory and the framework allows us to go.
  91. if (g_FactoryRouterData.DllCanUnloadNow())
  92. {
  93. sc = CommonCanUnloadNow(PROVIDER_NAME, g_cLock);
  94. }
  95. if ( sc == S_OK )
  96. {
  97. CTimerQueue::s_TimerQueue.OnShutDown();
  98. CResourceManager::sm_TheResourceManager.ForcibleCleanUp () ;
  99. #ifdef WIN9XONLY
  100. HoldSingleCim32NetPtr::FreeCim32NetApiPtr() ;
  101. #endif
  102. }
  103. }
  104. catch ( ... )
  105. {
  106. // sc should already be set correctly
  107. }
  108. return sc;
  109. }
  110. //***************************************************************************
  111. //
  112. // DllRegisterServer
  113. //
  114. // Purpose: Called during setup or by regsvr32.
  115. //
  116. // Return: NOERROR if registration successful, error otherwise.
  117. //***************************************************************************
  118. STDAPI DllRegisterServer(void)
  119. {
  120. HRESULT t_status = S_OK;
  121. try
  122. {
  123. t_status = RegisterServer( _T("WBEM Framework Instance Provider CIMA"), CLSID_CimWinProviderA ) ;
  124. if( NOERROR == t_status )
  125. {
  126. t_status = g_FactoryRouterData.DllRegisterServer() ;
  127. }
  128. }
  129. catch ( ... )
  130. {
  131. t_status = E_OUTOFMEMORY;
  132. }
  133. return t_status ;
  134. }
  135. //***************************************************************************
  136. //
  137. // DllUnregisterServer
  138. //
  139. // Purpose: Called when it is time to remove the registry entries.
  140. //
  141. // Return: NOERROR if registration successful, error otherwise.
  142. //***************************************************************************
  143. STDAPI DllUnregisterServer(void)
  144. {
  145. HRESULT t_status = S_OK;
  146. try
  147. {
  148. t_status = UnregisterServer( CLSID_CimWinProviderA ) ;
  149. if( NOERROR == t_status )
  150. {
  151. t_status = g_FactoryRouterData.DllUnregisterServer() ;
  152. }
  153. }
  154. catch ( ... )
  155. {
  156. t_status = E_OUTOFMEMORY;
  157. }
  158. return t_status ;
  159. }
  160. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  161. BOOL InitializeEventFactories(void)
  162. {
  163. BOOL fRet = FALSE;
  164. gp_ShutdownEventFactory = new CShutdownEventFactory( CLSID_ShutdownEventProvider,SHUTDOWN_EVENT_CLASS ) ;
  165. if( gp_ShutdownEventFactory )
  166. {
  167. gp_VolumeChangeFactory = new CVolumeChangeFactory( CLSID_VolumeChangeEventProvider,VOLUME_CHANGE_EVENT ) ;
  168. if( gp_VolumeChangeFactory )
  169. {
  170. fRet = TRUE;
  171. }
  172. }
  173. return fRet;
  174. }
  175. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  176. void CleanupEventFactories(void)
  177. {
  178. if( gp_ShutdownEventFactory )
  179. {
  180. delete gp_ShutdownEventFactory;
  181. gp_ShutdownEventFactory = NULL;
  182. }
  183. if( gp_VolumeChangeFactory )
  184. {
  185. delete gp_VolumeChangeFactory;
  186. gp_VolumeChangeFactory = NULL;
  187. }
  188. }
  189. //***************************************************************************
  190. //
  191. // DllMain
  192. //
  193. // Purpose: Called by the operating system when processes and threads are
  194. // initialized and terminated, or upon calls to the LoadLibrary
  195. // and FreeLibrary functions
  196. //
  197. // Return: TRUE if load was successful, else FALSE
  198. //***************************************************************************
  199. BOOL APIENTRY DllMain( HINSTANCE hInstDLL, // handle to DLL module
  200. DWORD fdwReason, // reason for calling function
  201. LPVOID lpReserved ) // reserved
  202. {
  203. BOOL bRet = TRUE;
  204. try
  205. {
  206. LogMessage2( L"%s -> DllMain", PROVIDER_NAME);
  207. // Perform actions based on the reason for calling.
  208. switch( fdwReason )
  209. {
  210. case DLL_PROCESS_ATTACH:
  211. {
  212. bRet = CommonProcessAttach(PROVIDER_NAME, g_cLock, hInstDLL);
  213. if( bRet )
  214. {
  215. bRet = InitializeEventFactories();
  216. }
  217. }
  218. break;
  219. case DLL_THREAD_ATTACH:
  220. {
  221. // Do thread-specific initialization.
  222. }
  223. break;
  224. case DLL_THREAD_DETACH:
  225. {
  226. // Do thread-specific cleanup.
  227. }
  228. break;
  229. case DLL_PROCESS_DETACH:
  230. {
  231. CleanupEventFactories();
  232. // Perform any necessary cleanup.
  233. LogMessage( L"DLL_PROCESS_DETACH" );
  234. }
  235. break;
  236. }
  237. }
  238. catch ( ... )
  239. {
  240. bRet = FALSE;
  241. }
  242. return bRet; // Status of DLL_PROCESS_ATTACH.
  243. }