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.

339 lines
7.2 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WMI 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) 2000-2001 Microsoft Corporation, All Rights Reserved
  13. //
  14. //***************************************************************************
  15. #include <stdafx.h>
  16. #include <provexpt.h>
  17. #include <provtempl.h>
  18. #include <provmt.h>
  19. #include <typeinfo.h>
  20. #include <provcont.h>
  21. #include <provevt.h>
  22. #include <provval.h>
  23. #include <provtype.h>
  24. #include <provtree.h>
  25. #include <provdnf.h>
  26. #include <winsock.h>
  27. #include "ipexport.h"
  28. #include "icmpapi.h"
  29. #include <Allocator.h>
  30. #include <Thread.h>
  31. #include <HashTable.h>
  32. #include <PingProv.h>
  33. #include <Pingfac.h>
  34. #include <dllunreg.h>
  35. HMODULE ghModule ;
  36. //============
  37. // {734AC5AE-68E1-4fb5-B8DA-1D92F7FC6661}
  38. DEFINE_GUID(CLSID_CPINGPROVIDER,
  39. 0x734ac5ae, 0x68e1, 0x4fb5, 0xb8, 0xda, 0x1d, 0x92, 0xf7, 0xfc, 0x66, 0x61);
  40. //Count number of objects and number of locks.
  41. long g_cLock = 0 ;
  42. //***************************************************************************
  43. //
  44. // DllGetClassObject
  45. //
  46. // Purpose: Called by Ole when some client wants a class factory. Return
  47. // one only if it is the sort of class this DLL supports.
  48. //
  49. //***************************************************************************
  50. STDAPI DllGetClassObject (
  51. REFCLSID rclsid,
  52. REFIID riid,
  53. PPVOID ppv
  54. )
  55. {
  56. HRESULT hr = E_FAIL;
  57. SetStructuredExceptionHandler seh;
  58. try
  59. {
  60. CPingProviderClassFactory *pObj = NULL;
  61. if ( CLSID_CPINGPROVIDER == rclsid )
  62. {
  63. pObj = new CPingProviderClassFactory () ;
  64. hr = pObj->QueryInterface(riid, ppv);
  65. if (FAILED(hr))
  66. {
  67. delete pObj;
  68. }
  69. }
  70. }
  71. catch(Structured_Exception e_SE)
  72. {
  73. hr = E_UNEXPECTED;
  74. }
  75. catch(Heap_Exception e_HE)
  76. {
  77. hr = E_OUTOFMEMORY;
  78. }
  79. catch(...)
  80. {
  81. hr = E_UNEXPECTED;
  82. }
  83. return hr ;
  84. }
  85. //***************************************************************************
  86. //
  87. // DllCanUnloadNow
  88. //
  89. // Purpose: Called periodically by Ole in order to determine if the
  90. // DLL can be freed.
  91. //
  92. // Return: S_OK if there are no objects in use and the class factory
  93. // isn't locked.
  94. //
  95. //***************************************************************************
  96. STDAPI DllCanUnloadNow ()
  97. {
  98. // It is OK to unload if there are no objects or locks on the
  99. // class factory.
  100. SCODE sc = S_FALSE;
  101. SetStructuredExceptionHandler seh;
  102. try
  103. {
  104. CCritSecAutoUnlock t_lock( & CPingProvider::s_CS ) ;
  105. if ( ( 0 == CPingProviderClassFactory::s_LocksInProgress ) &&
  106. ( 0 == CPingProviderClassFactory::s_ObjectsInProgress )
  107. )
  108. {
  109. CPingProvider::Global_Shutdown();
  110. sc = S_OK;
  111. }
  112. }
  113. catch(Structured_Exception e_SE)
  114. {
  115. sc = E_UNEXPECTED;
  116. }
  117. catch(Heap_Exception e_HE)
  118. {
  119. sc = E_OUTOFMEMORY;
  120. }
  121. catch(...)
  122. {
  123. sc = E_UNEXPECTED;
  124. }
  125. return sc;
  126. }
  127. STDAPI DllRegisterServer(void)
  128. {
  129. SetStructuredExceptionHandler seh;
  130. try
  131. {
  132. HRESULT t_status;
  133. t_status = RegisterServer( _T("WBEM Ping Provider"), CLSID_CPINGPROVIDER ) ;
  134. if( NOERROR != t_status )
  135. {
  136. return t_status ;
  137. }
  138. return NOERROR;
  139. }
  140. catch(Structured_Exception e_SE)
  141. {
  142. return E_UNEXPECTED;
  143. }
  144. catch(Heap_Exception e_HE)
  145. {
  146. return E_OUTOFMEMORY;
  147. }
  148. catch(...)
  149. {
  150. return E_UNEXPECTED;
  151. }
  152. }
  153. //***************************************************************************
  154. //
  155. // DllUnregisterServer
  156. //
  157. // Purpose: Called when it is time to remove the registry entries.
  158. //
  159. // Return: NOERROR if registration successful, error otherwise.
  160. //***************************************************************************
  161. STDAPI DllUnregisterServer(void)
  162. {
  163. SetStructuredExceptionHandler seh;
  164. try
  165. {
  166. HRESULT t_status;
  167. t_status = UnregisterServer( CLSID_CPINGPROVIDER ) ;
  168. if( NOERROR != t_status )
  169. {
  170. return t_status ;
  171. }
  172. return NOERROR;
  173. }
  174. catch(Structured_Exception e_SE)
  175. {
  176. return E_UNEXPECTED;
  177. }
  178. catch(Heap_Exception e_HE)
  179. {
  180. return E_OUTOFMEMORY;
  181. }
  182. catch(...)
  183. {
  184. return E_UNEXPECTED;
  185. }
  186. }
  187. //***************************************************************************
  188. //
  189. // DllMain
  190. //
  191. // Purpose: Called by the operating system when processes and threads are
  192. // initialized and terminated, or upon calls to the LoadLibrary
  193. // and FreeLibrary functions
  194. //
  195. // Return: TRUE if load was successful, else FALSE
  196. //***************************************************************************
  197. BOOL APIENTRY DllMain (
  198. HINSTANCE hInstDLL, // handle to dll module
  199. DWORD fdwReason, // reason for calling function
  200. LPVOID lpReserved // reserved
  201. )
  202. {
  203. BOOL bRet = TRUE;
  204. SetStructuredExceptionHandler seh;
  205. try
  206. {
  207. // Perform actions based on the reason for calling.
  208. switch( fdwReason )
  209. {
  210. case DLL_PROCESS_ATTACH:
  211. {
  212. // TO DO: Consider adding DisableThreadLibraryCalls().
  213. // Initialize once for each new process.
  214. // Return FALSE to fail DLL load.
  215. DisableThreadLibraryCalls(hInstDLL);
  216. ghModule = hInstDLL ;
  217. InitializeCriticalSection(& CPingProvider::s_CS);
  218. /*
  219. * Use the global process heap for this particular boot operation
  220. */
  221. WmiAllocator t_Allocator ;
  222. WmiStatusCode t_StatusCode = t_Allocator.New (
  223. ( void ** ) & CPingProvider::s_Allocator ,
  224. sizeof ( WmiAllocator )
  225. ) ;
  226. if ( t_StatusCode == e_StatusCode_Success )
  227. {
  228. :: new ( ( void * ) CPingProvider::s_Allocator ) WmiAllocator ;
  229. t_StatusCode = CPingProvider::s_Allocator->Initialize () ;
  230. if ( t_StatusCode != e_StatusCode_Success )
  231. {
  232. t_Allocator.Delete ( ( void * ) CPingProvider::s_Allocator ) ;
  233. CPingProvider::s_Allocator = NULL;
  234. bRet = FALSE ;
  235. }
  236. }
  237. else
  238. {
  239. bRet = FALSE ;
  240. CPingProvider::s_Allocator = NULL;
  241. }
  242. }
  243. break;
  244. case DLL_THREAD_ATTACH:
  245. {
  246. // Do thread-specific initialization.
  247. }
  248. break;
  249. case DLL_THREAD_DETACH:
  250. {
  251. // Do thread-specific cleanup.
  252. /*
  253. * Use the global process heap for this particular boot operation
  254. */
  255. }
  256. break;
  257. case DLL_PROCESS_DETACH:
  258. {
  259. // Perform any necessary cleanup.
  260. if (CPingProvider::s_Allocator)
  261. {
  262. WmiAllocator t_Allocator ;
  263. t_Allocator.Delete ( ( void * ) CPingProvider::s_Allocator ) ;
  264. }
  265. DeleteCriticalSection(& CPingProvider::s_CS);
  266. }
  267. break;
  268. }
  269. }
  270. catch(Structured_Exception e_SE)
  271. {
  272. bRet = FALSE;
  273. }
  274. catch(Heap_Exception e_HE)
  275. {
  276. bRet = FALSE;
  277. }
  278. catch(...)
  279. {
  280. bRet = FALSE;
  281. }
  282. return bRet ; // Sstatus of DLL_PROCESS_ATTACH.
  283. }