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.

437 lines
9.8 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 <strsafe.h>
  13. #include "Globals.h"
  14. #include "ClassFac.h"
  15. #include "Guids.h"
  16. #include "aggregator.h"
  17. #include "ProvRegistrar.h"
  18. #include "ProvEvents.h"
  19. #include "OS.h"
  20. HINSTANCE g_hInst=NULL;
  21. enum { RUN_AS_PROVIDER = 1, RUN_AS_CLIENT=2 };
  22. int run_as = 0;
  23. CriticalSection s_CriticalSection(NOTHROW_LOCK) ;
  24. /******************************************************************************
  25. *
  26. * Name:
  27. *
  28. *
  29. * Description:
  30. *
  31. *
  32. *****************************************************************************/
  33. BOOL APIENTRY DllMain (
  34. HINSTANCE hInstance,
  35. ULONG ulReason ,
  36. LPVOID pvReserved
  37. )
  38. {
  39. g_hInst=hInstance;
  40. BOOL t_Status = TRUE ;
  41. if ( DLL_PROCESS_DETACH == ulReason )
  42. {
  43. HRESULT t_Result = DecoupledProviderSubSystem_Globals :: Global_Shutdown () ;
  44. if ( SUCCEEDED ( t_Result ) )
  45. {
  46. t_Status = TRUE ;
  47. }
  48. else
  49. {
  50. t_Status = FALSE ;
  51. }
  52. t_Status = TRUE ;
  53. }
  54. else if ( DLL_PROCESS_ATTACH == ulReason )
  55. {
  56. if (!s_CriticalSection.valid())
  57. return FALSE;
  58. HRESULT t_Result = DecoupledProviderSubSystem_Globals :: Global_Startup () ;
  59. if ( SUCCEEDED ( t_Result ) )
  60. {
  61. t_Status = TRUE ;
  62. }
  63. else
  64. {
  65. t_Status = FALSE ;
  66. }
  67. }
  68. else if ( DLL_THREAD_DETACH == ulReason )
  69. {
  70. t_Status = TRUE ;
  71. }
  72. else if ( DLL_THREAD_ATTACH == ulReason )
  73. {
  74. t_Status = TRUE ;
  75. }
  76. return t_Status ;
  77. }
  78. /******************************************************************************
  79. *
  80. * Name:
  81. *
  82. *
  83. * Description:
  84. *
  85. *
  86. *****************************************************************************/
  87. STDAPI DllGetClassObject (
  88. REFCLSID rclsid ,
  89. REFIID riid,
  90. void **ppv
  91. )
  92. {
  93. CS_GUARD_RETURN(monitor, s_CriticalSection, E_OUTOFMEMORY) ;
  94. HRESULT status = S_OK ;
  95. if ( rclsid == CLSID_WmiDCProxyProvider)
  96. {
  97. run_as = RUN_AS_PROVIDER;
  98. CServerClassFactory <DCProxy,IWbemServices> *lpunk = new CServerClassFactory <DCProxy,IWbemServices> ;
  99. if ( lpunk == NULL )
  100. {
  101. status = E_OUTOFMEMORY ;
  102. }
  103. else
  104. {
  105. status = lpunk->QueryInterface ( riid , ppv ) ;
  106. if ( FAILED ( status ) )
  107. {
  108. delete lpunk ;
  109. }
  110. else
  111. {
  112. }
  113. }
  114. } else if ( rclsid == CLSID_WbemDecoupledRegistrar)
  115. {
  116. run_as = RUN_AS_CLIENT;
  117. CServerClassFactory <CServerObject_ProviderRegistrar,IWbemDecoupledRegistrar> *lpunk = new CServerClassFactory <CServerObject_ProviderRegistrar,IWbemDecoupledRegistrar> ;
  118. if ( lpunk == NULL )
  119. {
  120. status = E_OUTOFMEMORY ;
  121. }
  122. else
  123. {
  124. status = lpunk->QueryInterface ( riid , ppv ) ;
  125. if ( FAILED ( status ) )
  126. {
  127. delete lpunk ;
  128. }
  129. else
  130. {
  131. }
  132. }
  133. }
  134. else if ( rclsid == CLSID_WbemDecoupledBasicEventProvider)
  135. {
  136. run_as = RUN_AS_CLIENT;
  137. CServerClassFactory <CServerObject_ProviderEvents,IWbemDecoupledBasicEventProvider> *lpunk = new CServerClassFactory <CServerObject_ProviderEvents,IWbemDecoupledBasicEventProvider> ;
  138. if ( lpunk == NULL )
  139. {
  140. status = E_OUTOFMEMORY ;
  141. }
  142. else
  143. {
  144. status = lpunk->QueryInterface ( riid , ppv ) ;
  145. if ( FAILED ( status ) )
  146. {
  147. delete lpunk ;
  148. }
  149. else
  150. {
  151. }
  152. }
  153. }
  154. else
  155. {
  156. status = CLASS_E_CLASSNOTAVAILABLE ;
  157. }
  158. return status ;
  159. }
  160. /******************************************************************************
  161. *
  162. * Name:
  163. *
  164. *
  165. * Description:
  166. *
  167. *
  168. *****************************************************************************/
  169. STDAPI DllCanUnloadNow ()
  170. {
  171. CS_GUARD_RETURN(monitor, s_CriticalSection, E_OUTOFMEMORY) ; bool unload = true;
  172. switch(run_as)
  173. {
  174. case 0:
  175. break;
  176. case RUN_AS_PROVIDER:
  177. {
  178. DC_registrar * dcReg = DC_registrar::instance_;
  179. if (dcReg && DecoupledProviderSubSystem_Globals::s_ObjectsInProgress == 0)
  180. {
  181. dcReg->Delete();
  182. dcReg->Release();
  183. }
  184. unload = DecoupledProviderSubSystem_Globals::s_LocksInProgress || DecoupledProviderSubSystem_Globals::s_ObjectsInProgress ||
  185. DecoupledProviderSubSystem_Globals::s_RegistrarUsers;
  186. break;
  187. }
  188. case RUN_AS_CLIENT:
  189. unload = DecoupledProviderSubSystem_Globals::s_LocksInProgress || DecoupledProviderSubSystem_Globals::s_ObjectsInProgress;
  190. break;
  191. };
  192. unload = ! unload ;
  193. return unload ? ResultFromScode ( S_OK ) : ResultFromScode ( S_FALSE ) ;
  194. }
  195. /******************************************************************************
  196. *
  197. * Name:
  198. *
  199. *
  200. * Description:
  201. *
  202. *
  203. *****************************************************************************/
  204. //Strings used during self registeration
  205. #define REG_FORMAT_STR ("%s\\%s")
  206. #define NOT_INSERT_STR ("NotInsertable")
  207. #define INPROC32_STR ("InprocServer32")
  208. #define LOCALSERVER32_STR ("LocalServer32")
  209. #define THREADING_MODULE_STR ("ThreadingModel")
  210. #define APARTMENT_STR ("Both")
  211. #define APPID_VALUE_STR ("APPID")
  212. #define APPID_STR ("APPID\\")
  213. #define CLSID_STR ("CLSID\\")
  214. #define WMI_DECCUPLED_PROXY_PROVIDER ("Microsoft WMI Adaptor Provider for Decoupled Provider")
  215. #define WMI_PROVIDER_DECOUPLED_REGISTRAR ("Microsoft WMI Provider Subsystem Decoupled Registrar")
  216. #define WMI_PROVIDER_DECOUPLED_BASIC_EVENT_PROVIDER ("Microsoft WMI Provider Subsystem Decoupled Basic Event Provider")
  217. const char * WmiDCProxyProviderKey= ("{54D8502C-527D-43f7-A506-A9DA075E229C}");
  218. const char * WbemDecoupledRegistrarKey=("{4cfc7932-0f9d-4bef-9c32-8ea2a6b56fcb}");
  219. const char * WbemDecoupledBasicEventProviderKey = ("{f5f75737-2843-4f22-933d-c76a97cda62f}");
  220. /******************************************************************************
  221. *
  222. * Name:
  223. *
  224. *
  225. * Description:
  226. *
  227. *
  228. *****************************************************************************/
  229. BOOL SetKeyAndValue ( char *pszKey , char *pszSubkey , char *pszValueName , char *pszValue )
  230. {
  231. HKEY hKey;
  232. char szKey[256];
  233. StringCchCopyA ( szKey , 256, pszKey ) ;
  234. if ( NULL != pszSubkey )
  235. {
  236. StringCchCatA ( szKey , 256, ("\\") ) ;
  237. StringCchCatA ( szKey , 256, pszSubkey ) ;
  238. }
  239. if ( ERROR_SUCCESS != RegCreateKeyExA (
  240. HKEY_CLASSES_ROOT ,
  241. szKey ,
  242. 0,
  243. NULL,
  244. REG_OPTION_NON_VOLATILE ,
  245. KEY_ALL_ACCESS,
  246. NULL,
  247. &hKey,
  248. NULL
  249. )
  250. )
  251. {
  252. return FALSE ;
  253. }
  254. if ( NULL != pszValue )
  255. {
  256. if ( ERROR_SUCCESS != RegSetValueExA (
  257. hKey,
  258. pszValueName,
  259. 0,
  260. REG_SZ,
  261. (BYTE *) pszValue ,
  262. (lstrlenA(pszValue)+1)*sizeof(char)
  263. )
  264. )
  265. {
  266. return FALSE;
  267. }
  268. }
  269. RegCloseKey ( hKey ) ;
  270. return TRUE;
  271. }
  272. /******************************************************************************
  273. *
  274. * Name:
  275. *
  276. *
  277. * Description:
  278. *
  279. *
  280. *****************************************************************************/
  281. STDAPI RegisterServer ( const char * szProviderClassID , char *a_ProviderName )
  282. {
  283. CServerObject_DecoupledClientRegistration_Element::VerifyClientKey();
  284. char szModule[512];
  285. if (GetModuleFileNameA(g_hInst,(char *)szModule, sizeof(szModule)/sizeof(char))==0)
  286. return HRESULT_FROM_WIN32(GetLastError());
  287. char szProviderCLSIDClassID[128];
  288. StringCchCopyA(szProviderCLSIDClassID,128, CLSID_STR);
  289. StringCchCatA(szProviderCLSIDClassID,128, szProviderClassID);
  290. //Create entries under CLSID
  291. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NULL, NULL, a_ProviderName ))
  292. return SELFREG_E_CLASS;
  293. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NOT_INSERT_STR, NULL, NULL))
  294. return SELFREG_E_CLASS;
  295. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR, NULL,szModule))
  296. return SELFREG_E_CLASS;
  297. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  298. return SELFREG_E_CLASS;
  299. return S_OK;
  300. }
  301. /******************************************************************************
  302. *
  303. * Name:
  304. *
  305. *
  306. * Description:
  307. *
  308. *
  309. *****************************************************************************/
  310. STDAPI UnregisterServer( const char * szProviderClassID )
  311. {
  312. char szTemp[128];
  313. char szProviderCLSIDClassID[128];
  314. StringCchCopyA(szProviderCLSIDClassID,128, CLSID_STR);
  315. StringCchCatA(szProviderCLSIDClassID,128, szProviderClassID);
  316. //Delete entries under CLSID
  317. StringCchPrintfA(szTemp, 128, REG_FORMAT_STR, szProviderCLSIDClassID, NOT_INSERT_STR);
  318. RegDeleteKeyA(HKEY_CLASSES_ROOT, szTemp);
  319. StringCchPrintfA(szTemp, 128, REG_FORMAT_STR,szProviderCLSIDClassID, INPROC32_STR);
  320. RegDeleteKeyA(HKEY_CLASSES_ROOT, szTemp);
  321. RegDeleteKeyA(HKEY_CLASSES_ROOT, szProviderCLSIDClassID);
  322. return S_OK;
  323. }
  324. /******************************************************************************
  325. *
  326. * Name:
  327. *
  328. *
  329. * Description:
  330. *
  331. *
  332. *****************************************************************************/
  333. STDAPI DllRegisterServer ()
  334. {
  335. HRESULT t_Result ;
  336. t_Result = RegisterServer ( WmiDCProxyProviderKey , WMI_DECCUPLED_PROXY_PROVIDER ) ;
  337. t_Result = RegisterServer ( WbemDecoupledRegistrarKey , WMI_PROVIDER_DECOUPLED_REGISTRAR ) ;
  338. t_Result = RegisterServer ( WbemDecoupledBasicEventProviderKey , WMI_PROVIDER_DECOUPLED_BASIC_EVENT_PROVIDER ) ;
  339. return t_Result ;
  340. }
  341. /******************************************************************************
  342. *
  343. * Name:
  344. *
  345. *
  346. * Description:
  347. *
  348. *
  349. *****************************************************************************/
  350. STDAPI DllUnregisterServer ()
  351. {
  352. HRESULT t_Result ;
  353. t_Result = UnregisterServer ( WmiDCProxyProviderKey ) ;
  354. t_Result = UnregisterServer ( WbemDecoupledRegistrarKey ) ;
  355. t_Result = UnregisterServer ( WbemDecoupledBasicEventProviderKey ) ;
  356. return t_Result ;
  357. }