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.

426 lines
9.6 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. MainDll.cpp
  5. Abstract:
  6. History:
  7. --*/
  8. #include "PreComp.h"
  9. #include <wbemint.h>
  10. #include <comdef.h>
  11. #include <stdio.h>
  12. #include "Globals.h"
  13. #include "ClassFac.h"
  14. #include "Guids.h"
  15. #include "ProvRegistrar.h"
  16. #include "ProvEvents.h"
  17. #include <lockst.h>
  18. /******************************************************************************
  19. *
  20. * Name:
  21. *
  22. *
  23. * Description:
  24. *
  25. *
  26. *****************************************************************************/
  27. HINSTANCE g_ModuleInstance=NULL;
  28. CriticalSection s_CriticalSection(NOTHROW_LOCK) ;
  29. /******************************************************************************
  30. *
  31. * Name:
  32. *
  33. *
  34. * Description:
  35. *
  36. *
  37. *****************************************************************************/
  38. BOOL APIENTRY DllMain (
  39. HINSTANCE a_ModuleInstance,
  40. ULONG a_Reason ,
  41. LPVOID a_Reserved
  42. )
  43. {
  44. g_ModuleInstance = a_ModuleInstance ;
  45. BOOL t_Status = TRUE ;
  46. if ( DLL_PROCESS_DETACH == a_Reason )
  47. {
  48. HRESULT t_Result = DecoupledProviderSubSystem_Globals :: Global_Shutdown () ;
  49. if ( SUCCEEDED ( t_Result ) )
  50. {
  51. t_Status = TRUE ;
  52. }
  53. else
  54. {
  55. t_Status = FALSE ;
  56. }
  57. WmiHelper :: DeleteCriticalSection ( & s_CriticalSection ) ;
  58. t_Status = TRUE ;
  59. }
  60. else if ( DLL_PROCESS_ATTACH == a_Reason )
  61. {
  62. WmiStatusCode t_StatusCode = WmiHelper :: InitializeCriticalSection ( & s_CriticalSection ) ;
  63. if ( t_StatusCode == e_StatusCode_Success )
  64. {
  65. HRESULT t_Result = DecoupledProviderSubSystem_Globals :: Global_Startup () ;
  66. if ( SUCCEEDED ( t_Result ) )
  67. {
  68. t_Status = TRUE ;
  69. }
  70. else
  71. {
  72. t_Status = FALSE ;
  73. }
  74. }
  75. else
  76. {
  77. t_Status = FALSE ;
  78. }
  79. DisableThreadLibraryCalls ( a_ModuleInstance ) ;
  80. }
  81. else if ( DLL_THREAD_DETACH == a_Reason )
  82. {
  83. t_Status = TRUE ;
  84. }
  85. else if ( DLL_THREAD_ATTACH == a_Reason )
  86. {
  87. t_Status = TRUE ;
  88. }
  89. return t_Status ;
  90. }
  91. /******************************************************************************
  92. *
  93. * Name:
  94. *
  95. *
  96. * Description:
  97. *
  98. *
  99. *****************************************************************************/
  100. STDAPI DllGetClassObject (
  101. REFCLSID a_Clsid ,
  102. REFIID a_Riid ,
  103. void **a_Void
  104. )
  105. {
  106. HRESULT t_Result = S_OK ;
  107. if ( a_Clsid == CLSID_WbemDecoupledRegistrar )
  108. {
  109. CServerClassFactory <CServerObject_ProviderRegistrar,CServerObject_ProviderRegistrar> *t_Unknown = new CServerClassFactory <CServerObject_ProviderRegistrar,CServerObject_ProviderRegistrar> ;
  110. if ( t_Unknown == NULL )
  111. {
  112. t_Result = E_OUTOFMEMORY ;
  113. }
  114. else
  115. {
  116. t_Result = t_Unknown->QueryInterface ( a_Riid , a_Void ) ;
  117. if ( FAILED ( t_Result ) )
  118. {
  119. delete t_Unknown ;
  120. }
  121. else
  122. {
  123. }
  124. }
  125. }
  126. else if ( a_Clsid == CLSID_WbemDecoupledBasicEventProvider )
  127. {
  128. CServerClassFactory <CServerObject_ProviderEvents,CServerObject_ProviderEvents> *t_Unknown = new CServerClassFactory <CServerObject_ProviderEvents,CServerObject_ProviderEvents> ;
  129. if ( t_Unknown == NULL )
  130. {
  131. t_Result = E_OUTOFMEMORY ;
  132. }
  133. else
  134. {
  135. t_Result = t_Unknown->QueryInterface ( a_Riid , a_Void ) ;
  136. if ( FAILED ( t_Result ) )
  137. {
  138. delete t_Unknown ;
  139. }
  140. else
  141. {
  142. }
  143. }
  144. }
  145. else
  146. {
  147. t_Result = CLASS_E_CLASSNOTAVAILABLE ;
  148. }
  149. return t_Result ;
  150. }
  151. /******************************************************************************
  152. *
  153. * Name:
  154. *
  155. *
  156. * Description:
  157. *
  158. *
  159. *****************************************************************************/
  160. STDAPI DllCanUnloadNow ()
  161. {
  162. /*
  163. * Place code in critical section
  164. */
  165. WmiStatusCode t_StatusCode = WmiHelper :: EnterCriticalSection ( & s_CriticalSection , FALSE ) ;
  166. if ( t_StatusCode == e_StatusCode_Success )
  167. {
  168. BOOL t_Unload = (
  169. DecoupledProviderSubSystem_Globals :: s_LocksInProgress || DecoupledProviderSubSystem_Globals :: s_ObjectsInProgress
  170. ) ;
  171. t_Unload = ! t_Unload ;
  172. WmiHelper :: LeaveCriticalSection ( & s_CriticalSection ) ;
  173. return t_Unload ? ResultFromScode ( S_OK ) : ResultFromScode ( S_FALSE ) ;
  174. }
  175. else
  176. {
  177. return FALSE ;
  178. }
  179. }
  180. /******************************************************************************
  181. *
  182. * Name:
  183. *
  184. *
  185. * Description:
  186. *
  187. *
  188. *****************************************************************************/
  189. //Strings used during self registeration
  190. #define REG_FORMAT_STR L"%s\\%s"
  191. #define NOT_INSERT_STR L"NotInsertable"
  192. #define INPROC32_STR L"InprocServer32"
  193. #define LOCALSERVER32_STR L"LocalServer32"
  194. #define THREADING_MODULE_STR L"ThreadingModel"
  195. #define APARTMENT_STR L"Both"
  196. #define APPID_VALUE_STR L"APPID"
  197. #define APPID_STR L"APPID\\"
  198. #define CLSID_STR L"CLSID\\"
  199. #define WMI_PROVIDER_DECOUPLED_REGISTRAR __TEXT("Microsoft WMI Provider Subsystem Decoupled Registrar")
  200. #define WMI_PROVIDER_DECOUPLED_BASIC_EVENT_PROVIDER __TEXT("Microsoft WMI Provider Subsystem Decoupled Basic Event Provider")
  201. /******************************************************************************
  202. *
  203. * Name:
  204. *
  205. *
  206. * Description:
  207. *
  208. *
  209. *****************************************************************************/
  210. BOOL SetKeyAndValue ( wchar_t *pszKey , wchar_t *pszSubkey , wchar_t *pszValueName , wchar_t *pszValue )
  211. {
  212. HKEY hKey;
  213. wchar_t szKey[256];
  214. HRESULT t_Result = StringCchCopyW ( szKey , sizeof ( szKey ) / sizeof ( wchar_t ) , pszKey ) ;
  215. if ( FAILED ( t_Result ) )
  216. {
  217. return FALSE ;
  218. }
  219. if ( NULL != pszSubkey )
  220. {
  221. t_Result = StringCchCatW ( szKey , sizeof ( szKey ) / sizeof ( wchar_t ) , L"\\" ) ;
  222. if ( FAILED ( t_Result ) )
  223. {
  224. return FALSE ;
  225. }
  226. t_Result = StringCchCatW ( szKey , sizeof ( szKey ) / sizeof ( wchar_t ) , pszSubkey ) ;
  227. if ( FAILED ( t_Result ) )
  228. {
  229. return FALSE ;
  230. }
  231. }
  232. if ( ERROR_SUCCESS != RegCreateKeyEx (
  233. HKEY_CLASSES_ROOT ,
  234. szKey ,
  235. 0,
  236. NULL,
  237. REG_OPTION_NON_VOLATILE ,
  238. KEY_ALL_ACCESS,
  239. NULL,
  240. &hKey,
  241. NULL
  242. )
  243. )
  244. {
  245. return FALSE ;
  246. }
  247. if ( NULL != pszValue )
  248. {
  249. if ( ERROR_SUCCESS != RegSetValueEx (
  250. hKey,
  251. pszValueName,
  252. 0,
  253. REG_SZ,
  254. (BYTE *) pszValue ,
  255. (lstrlen(pszValue)+1)*sizeof(wchar_t)
  256. )
  257. )
  258. {
  259. return FALSE;
  260. }
  261. }
  262. RegCloseKey ( hKey ) ;
  263. return TRUE;
  264. }
  265. /******************************************************************************
  266. *
  267. * Name:
  268. *
  269. *
  270. * Description:
  271. *
  272. *
  273. *****************************************************************************/
  274. STDAPI RegisterServer ( GUID a_ProviderClassId , wchar_t *a_ProviderName )
  275. {
  276. wchar_t szModule[512];
  277. GetModuleFileName(g_ModuleInstance,(wchar_t*)szModule, sizeof(szModule)/sizeof(wchar_t));
  278. wchar_t szProviderClassID[128];
  279. wchar_t szProviderCLSIDClassID[128];
  280. int iRet = StringFromGUID2(a_ProviderClassId,szProviderClassID, 128);
  281. StringCchCopyW(szProviderCLSIDClassID,sizeof(szProviderCLSIDClassID)/sizeof(wchar_t),CLSID_STR);
  282. StringCchCatW(szProviderCLSIDClassID,sizeof(szProviderCLSIDClassID)/sizeof(wchar_t),szProviderClassID);
  283. //Create entries under CLSID
  284. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NULL, NULL, a_ProviderName ))
  285. return SELFREG_E_CLASS;
  286. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NOT_INSERT_STR, NULL, NULL))
  287. return SELFREG_E_CLASS;
  288. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR, NULL,szModule))
  289. return SELFREG_E_CLASS;
  290. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  291. return SELFREG_E_CLASS;
  292. return S_OK;
  293. }
  294. /******************************************************************************
  295. *
  296. * Name:
  297. *
  298. *
  299. * Description:
  300. *
  301. *
  302. *****************************************************************************/
  303. STDAPI UnregisterServer( GUID a_ProviderClassId )
  304. {
  305. wchar_t szTemp[128];
  306. wchar_t szProviderClassID[128];
  307. wchar_t szProviderCLSIDClassID[128];
  308. int iRet = StringFromGUID2(a_ProviderClassId ,szProviderClassID, 128);
  309. StringCchCopyW (szProviderCLSIDClassID,sizeof(szProviderCLSIDClassID)/sizeof(wchar_t),CLSID_STR);
  310. StringCchCatW (szProviderCLSIDClassID,sizeof(szProviderCLSIDClassID)/sizeof(wchar_t),szProviderClassID);
  311. //Delete entries under CLSID
  312. StringCchPrintfW(szTemp, sizeof(szTemp)/sizeof(wchar_t),REG_FORMAT_STR, szProviderCLSIDClassID, NOT_INSERT_STR);
  313. RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  314. StringCchPrintfW(szTemp, sizeof(szTemp)/sizeof(wchar_t),REG_FORMAT_STR,szProviderCLSIDClassID, INPROC32_STR);
  315. RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  316. RegDeleteKey(HKEY_CLASSES_ROOT, szProviderCLSIDClassID);
  317. return S_OK;
  318. }
  319. /******************************************************************************
  320. *
  321. * Name:
  322. *
  323. *
  324. * Description:
  325. *
  326. *
  327. *****************************************************************************/
  328. STDAPI DllRegisterServer ()
  329. {
  330. HRESULT t_Result ;
  331. t_Result = RegisterServer ( CLSID_WbemDecoupledRegistrar , WMI_PROVIDER_DECOUPLED_REGISTRAR ) ;
  332. t_Result = RegisterServer ( CLSID_WbemDecoupledBasicEventProvider , WMI_PROVIDER_DECOUPLED_BASIC_EVENT_PROVIDER ) ;
  333. return t_Result ;
  334. }
  335. /******************************************************************************
  336. *
  337. * Name:
  338. *
  339. *
  340. * Description:
  341. *
  342. *
  343. *****************************************************************************/
  344. STDAPI DllUnregisterServer ()
  345. {
  346. HRESULT t_Result ;
  347. t_Result = UnregisterServer ( CLSID_WbemDecoupledRegistrar ) ;
  348. t_Result = UnregisterServer ( CLSID_WbemDecoupledBasicEventProvider ) ;
  349. return t_Result ;
  350. }