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.

543 lines
15 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // CopyRight (c) 1999-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // ClusterWMIProvider.cpp
  7. //
  8. // Description:
  9. // Implementation of the provider registration and entry point.
  10. //
  11. // Author:
  12. // Henry Wang (HenryWa) 24-AUG-1999
  13. //
  14. //////////////////////////////////////////////////////////////////////
  15. #include "Pch.h"
  16. #include <initguid.h>
  17. #include "ProvFactory.h"
  18. #include "InstanceProv.h"
  19. #include "EventProv.h"
  20. #include "ClusterWMIProvider.tmh"
  21. //////////////////////////////////////////////////////////////////////////////
  22. // Global Data
  23. //////////////////////////////////////////////////////////////////////////////
  24. // {598065EA-EDC9-4b2c-913B-5104D04D098A}
  25. DEFINE_GUID( CLSID_CLUSTER_WMI_PROVIDER,
  26. 0x598065ea, 0xedc9, 0x4b2c, 0x91, 0x3b, 0x51, 0x4, 0xd0, 0x4d, 0x9, 0x8a );
  27. // {6A52C339-DCB0-4682-8B1B-02DE2C436A6D}
  28. DEFINE_GUID( CLSID_CLUSTER_WMI_CLASS_PROVIDER,
  29. 0x6a52c339, 0xdcb0, 0x4682, 0x8b, 0x1b, 0x2, 0xde, 0x2c, 0x43, 0x6a, 0x6d );
  30. // {92863246-4EDE-4eff-B606-79C1971DB230}
  31. DEFINE_GUID( CLSID_CLUSTER_WMI_EVENT_PROVIDER,
  32. 0x92863246, 0x4ede, 0x4eff, 0xb6, 0x6, 0x79, 0xc1, 0x97, 0x1d, 0xb2, 0x30 );
  33. // Count number of objects and number of locks.
  34. long g_cObj = 0;
  35. long g_cLock = 0;
  36. HMODULE g_hModule;
  37. FactoryData g_FactoryDataArray[] =
  38. {
  39. {
  40. &CLSID_CLUSTER_WMI_PROVIDER,
  41. CInstanceProv::S_HrCreateThis,
  42. L"Cluster service WMI instance provider"
  43. },
  44. {
  45. &CLSID_CLUSTER_WMI_CLASS_PROVIDER,
  46. CClassProv::S_HrCreateThis,
  47. L"Cluster service WMI class provider"
  48. },
  49. {
  50. &CLSID_CLUSTER_WMI_EVENT_PROVIDER,
  51. CEventProv::S_HrCreateThis,
  52. L"Cluster service WMI event provider"
  53. },
  54. };
  55. //////////////////////////////////////////////////////////////////////////////
  56. //++
  57. //
  58. // BOOL
  59. // WINAPI
  60. // DllMain(
  61. // HANDLE hModule,
  62. // DWORD ul_reason_for_call,
  63. // LPVOID lpReserved
  64. // )
  65. //
  66. // Description:
  67. // Main DLL entry point.
  68. //
  69. // Arguments:
  70. // hModule -- DLL module handle.
  71. // ul_reason_for_call --
  72. // lpReserved --
  73. //
  74. // Return Values:
  75. // TRUE
  76. //
  77. //--
  78. //////////////////////////////////////////////////////////////////////////////
  79. BOOL
  80. WINAPI
  81. DllMain(
  82. HANDLE hModule,
  83. DWORD ul_reason_for_call,
  84. LPVOID lpReserved
  85. )
  86. {
  87. // begin_wpp config
  88. // CUSTOM_TYPE(dllreason, ItemListLong(DLL_PROCESS_DETACH, DLL_PROCESS_ATTACH, DLL_THREAD_ATTACH, DLL_THREAD_DETACH) );
  89. //
  90. // CUSTOM_TYPE(EventIdx, ItemSetLong(NODE_STATE, NODE_DELETED, NODE_ADDED, NODE_PROPERTY, REGISTRY_NAME,REGISTRY_ATTRIBUTES, REGISTRY_VALUE, REGISTRY_SUBTREE, RESOURCE_STATE, RESOURCE_DELETED, RESOURCE_ADDED, RESOURCE_PROPERTY, GROUP_STATE, GROUP_DELETED, GROUP_ADDED, GROUP_PROPERTY, RESOURCE_TYPE_DELETED, RESOURCE_TYPE_ADDED, RESOURCE_TYPE_PROPERTY, CLUSTER_RECONNECT, NETWORK_STATE, NETWORK_DELETED, NETWORK_ADDED, NETWORK_PROPERTY, NETINTERFACE_STATE, NETINTERFACE_DELETED, NETINTERFACE_ADDED, NETINTERFACE_PROPERTY, QUORUM_STATE, CLUSTER_STATE, CLUSTER_PROPERTY, HANDLE_CLOSE));
  91. //
  92. // CUSTOM_TYPE(GroupState, ItemListLong(Online, Offline, Failed, PartialOnline, Pending) );
  93. // CUSTOM_TYPE(ResourceState, ItemListLong(Initing, Initializing, Online, Offline, Failed) );
  94. // end_wpp
  95. //
  96. // Cluster event filter flags.
  97. //
  98. /*
  99. NODE_STATE = 0x00000001,
  100. NODE_DELETED = 0x00000002,
  101. NODE_ADDED = 0x00000004,
  102. NODE_PROPERTY = 0x00000008,
  103. REGISTRY_NAME = 0x00000010,
  104. REGISTRY_ATTRIBUTES = 0x00000020,
  105. REGISTRY_VALUE = 0x00000040,
  106. REGISTRY_SUBTREE = 0x00000080,
  107. RESOURCE_STATE = 0x00000100,
  108. RESOURCE_DELETED = 0x00000200,
  109. RESOURCE_ADDED = 0x00000400,
  110. RESOURCE_PROPERTY = 0x00000800,
  111. GROUP_STATE = 0x00001000,
  112. GROUP_DELETED = 0x00002000,
  113. GROUP_ADDED = 0x00004000,
  114. GROUP_PROPERTY = 0x00008000,
  115. RESOURCE_TYPE_DELETED = 0x00010000,
  116. RESOURCE_TYPE_ADDED = 0x00020000,
  117. RESOURCE_TYPE_PROPERTY = 0x00040000,
  118. CLUSTER_RECONNECT = 0x00080000,
  119. NETWORK_STATE = 0x00100000,
  120. NETWORK_DELETED = 0x00200000,
  121. NETWORK_ADDED = 0x00400000,
  122. NETWORK_PROPERTY = 0x00800000,
  123. NETINTERFACE_STATE = 0x01000000,
  124. NETINTERFACE_DELETED = 0x02000000,
  125. NETINTERFACE_ADDED = 0x04000000,
  126. NETINTERFACE_PROPERTY = 0x08000000,
  127. QUORUM_STATE = 0x10000000,
  128. CLUSTER_STATE = 0x20000000,
  129. CLUSTER_PROPERTY = 0x40000000,
  130. HANDLE_CLOSE = 0x80000000,
  131. */
  132. //#ifdef _DEBUG
  133. // _CrtSetBreakAlloc( 228 );
  134. //#endif
  135. TracePrint(("ClusWMI: DllMain entry, reason = %!dllreason!", ul_reason_for_call));
  136. g_hModule = static_cast< HMODULE >( hModule );
  137. switch ( ul_reason_for_call ) {
  138. case DLL_PROCESS_ATTACH:
  139. WPP_INIT_TRACING( NULL );
  140. break;
  141. case DLL_PROCESS_DETACH:
  142. WPP_CLEANUP();
  143. break;
  144. default:
  145. break;
  146. }
  147. return TRUE;
  148. } //*** DllMain()
  149. //////////////////////////////////////////////////////////////////////////////
  150. //++
  151. //
  152. // STDAPI
  153. // DllCanUnloadNow( void )
  154. //
  155. // Description:
  156. // Called periodically by Ole in order to determine if the
  157. // DLL can be freed.
  158. //
  159. // Arguments:
  160. // None.
  161. //
  162. // Return Values:
  163. // S_OK if there are no objects in use and the class factory
  164. // isn't locked.
  165. //
  166. //--
  167. //////////////////////////////////////////////////////////////////////////////
  168. STDAPI DllCanUnloadNow( void )
  169. {
  170. SCODE sc;
  171. //It is OK to unload if there are no objects or locks on the
  172. // class factory.
  173. sc = ( (0L == g_cObj) && (0L == g_cLock) ) ? S_OK : S_FALSE;
  174. TracePrint(("ClusWMI: DllCanUnloadNow is returning %d", sc));
  175. return sc;
  176. } //*** DllCanUnloadNow()
  177. //////////////////////////////////////////////////////////////////////////////
  178. //++
  179. //
  180. // STDAPI
  181. // DllRegisterServer( void )
  182. //
  183. // Description:
  184. // Called during setup or by regsvr32.
  185. //
  186. // Arguments:
  187. // None.
  188. //
  189. // Return Values:
  190. // NOERROR if registration successful, error otherwise.
  191. // SELFREG_E_CLASS
  192. //
  193. //--
  194. //////////////////////////////////////////////////////////////////////////////
  195. STDAPI DllRegisterServer( void )
  196. {
  197. WCHAR wszID[ 128 ];
  198. WCHAR wszCLSID[ 128 ];
  199. WCHAR wszModule[ MAX_PATH ];
  200. INT idx;
  201. WCHAR * pwszModel = L"Both";
  202. HKEY hKey1 = NULL;
  203. HKEY hKey2 = NULL;
  204. DWORD dwRt = ERROR_SUCCESS;
  205. INT cArray = sizeof ( g_FactoryDataArray ) / sizeof ( FactoryData );
  206. TracePrint(("ClusWMI: DllRegisterServer entry"));
  207. // Create the path.
  208. try
  209. {
  210. DWORD cbModuleNameSize = 0;
  211. if ( GetModuleFileNameW( g_hModule, wszModule, MAX_PATH ) == 0 )
  212. {
  213. dwRt = GetLastError();
  214. throw( dwRt );
  215. }
  216. wszModule[ MAX_PATH - 1 ] = L'\0';
  217. cbModuleNameSize = (DWORD) ( wcslen( wszModule ) + 1 ) * sizeof( wszModule[ 0 ] );
  218. for ( idx = 0 ; idx < cArray && dwRt == ERROR_SUCCESS ; idx++ )
  219. {
  220. LPCWSTR pwszName = g_FactoryDataArray[ idx ].m_pwszRegistryName;
  221. StringFromGUID2(
  222. *g_FactoryDataArray[ idx ].m_pCLSID,
  223. wszID,
  224. 128
  225. );
  226. HRESULT hr = StringCchPrintfW( wszCLSID, RTL_NUMBER_OF( wszCLSID ), L"Software\\Classes\\CLSID\\%ws", wszID );
  227. dwRt = (DWORD) hr;
  228. if ( dwRt != ERROR_SUCCESS )
  229. {
  230. throw( dwRt );
  231. }
  232. // Create entries under CLSID
  233. dwRt = RegCreateKeyExW(
  234. HKEY_LOCAL_MACHINE,
  235. wszCLSID,
  236. 0,
  237. NULL,
  238. REG_OPTION_NON_VOLATILE,
  239. KEY_CREATE_SUB_KEY | KEY_WRITE,
  240. NULL,
  241. &hKey1,
  242. NULL
  243. );
  244. if ( dwRt != ERROR_SUCCESS )
  245. {
  246. throw( dwRt );
  247. }
  248. dwRt = RegSetValueExW(
  249. hKey1,
  250. NULL,
  251. 0,
  252. REG_SZ,
  253. (BYTE *) pwszName,
  254. (DWORD) ( sizeof( WCHAR ) * ( wcslen( pwszName ) + 1 ) )
  255. );
  256. if ( dwRt != ERROR_SUCCESS )
  257. {
  258. throw( dwRt );
  259. }
  260. dwRt = RegCreateKeyExW(
  261. hKey1,
  262. L"InprocServer32",
  263. 0,
  264. NULL,
  265. REG_OPTION_NON_VOLATILE,
  266. KEY_CREATE_SUB_KEY | KEY_WRITE,
  267. NULL,
  268. &hKey2,
  269. NULL
  270. );
  271. if ( dwRt != ERROR_SUCCESS )
  272. {
  273. throw( dwRt );
  274. }
  275. dwRt = RegSetValueExW(
  276. hKey2,
  277. NULL,
  278. 0,
  279. REG_SZ,
  280. (BYTE *) wszModule,
  281. cbModuleNameSize
  282. );
  283. if ( dwRt != ERROR_SUCCESS )
  284. {
  285. throw( dwRt );
  286. }
  287. dwRt = RegSetValueExW(
  288. hKey2,
  289. L"ThreadingModel",
  290. 0,
  291. REG_SZ,
  292. (BYTE *) pwszModel,
  293. (DWORD) ( sizeof( WCHAR ) * ( wcslen( pwszModel ) + 1 ) )
  294. );
  295. if ( dwRt != ERROR_SUCCESS )
  296. {
  297. throw( dwRt );
  298. }
  299. } // for: each entry in factory entry array
  300. if ( dwRt != ERROR_SUCCESS )
  301. {
  302. throw( dwRt );
  303. }
  304. }
  305. catch ( DWORD sc )
  306. {
  307. dwRt = sc;
  308. }
  309. catch ( ... )
  310. {
  311. dwRt = (DWORD) SELFREG_E_CLASS;
  312. }
  313. TracePrint(("ClusWMI: RegisterServer returned %d", dwRt));
  314. if ( hKey1 != NULL )
  315. {
  316. RegCloseKey( hKey1 );
  317. }
  318. if ( hKey2 != NULL )
  319. {
  320. RegCloseKey( hKey2 );
  321. }
  322. return dwRt;
  323. } //*** DllRegisterServer()
  324. //////////////////////////////////////////////////////////////////////////////
  325. //++
  326. //
  327. // STDAPI
  328. // DllUnregisterServer( void )
  329. //
  330. // Description:
  331. // Called when it is time to remove the registry entries.
  332. //
  333. // Arguments:
  334. // None.
  335. //
  336. // Return Values:
  337. // NOERROR if registration successful, error otherwise.
  338. // SELFREG_E_CLASS
  339. //
  340. //--
  341. //////////////////////////////////////////////////////////////////////////////
  342. STDAPI DllUnregisterServer( void )
  343. {
  344. WCHAR wszID[ 128 ];
  345. WCHAR wszCLSID[ 128 ];
  346. HKEY hKey;
  347. INT idx;
  348. DWORD dwRet = ERROR_SUCCESS;
  349. for ( idx = 0 ; idx < 2 && dwRet == ERROR_SUCCESS ; idx++ )
  350. {
  351. StringFromGUID2(
  352. *g_FactoryDataArray[ idx ].m_pCLSID,
  353. wszID,
  354. 128
  355. );
  356. HRESULT hr = StringCchPrintfW( wszCLSID, RTL_NUMBER_OF( wszCLSID ), L"Software\\Classes\\CLSID\\%ws", wszID );
  357. dwRet = (DWORD) hr;
  358. if ( dwRet != ERROR_SUCCESS )
  359. {
  360. break;
  361. }
  362. // First delete the InProcServer subkey.
  363. dwRet = RegOpenKeyExW(
  364. HKEY_LOCAL_MACHINE,
  365. wszCLSID,
  366. 0,
  367. KEY_ALL_ACCESS,
  368. &hKey
  369. );
  370. if ( dwRet != ERROR_SUCCESS )
  371. {
  372. break;
  373. }
  374. dwRet = RegDeleteKeyW( hKey, L"InProcServer32" );
  375. RegCloseKey( hKey );
  376. if ( dwRet != ERROR_SUCCESS )
  377. {
  378. break;
  379. }
  380. dwRet = RegOpenKeyExW(
  381. HKEY_LOCAL_MACHINE,
  382. L"Software\\Classes\\CLSID",
  383. 0,
  384. KEY_ALL_ACCESS,
  385. &hKey
  386. );
  387. if ( dwRet != ERROR_SUCCESS )
  388. {
  389. break;
  390. }
  391. dwRet = RegDeleteKeyW( hKey,wszID );
  392. RegCloseKey( hKey );
  393. if ( dwRet != ERROR_SUCCESS )
  394. {
  395. break;
  396. }
  397. } // for: each object
  398. if ( dwRet != ERROR_SUCCESS )
  399. {
  400. dwRet = (DWORD) SELFREG_E_CLASS;
  401. }
  402. return dwRet;
  403. } //*** DllUnregisterServer()
  404. //////////////////////////////////////////////////////////////////////////////
  405. //++
  406. //
  407. // STDAPI
  408. // DllGetClassObject(
  409. // REFCLSID rclsidIn,
  410. // REFIID riidIn,
  411. // PPVOID ppvOut
  412. // )
  413. //
  414. // Description:
  415. // Called by Ole when some client wants a class factory. Return
  416. // one only if it is the sort of class this DLL supports.
  417. //
  418. // Arguments:
  419. // rclsidIn --
  420. // riidIn --
  421. // ppvOut --
  422. //
  423. // Return Values:
  424. // NOERROR if registration successful, error otherwise.
  425. // E_OUTOFMEMORY
  426. // E_FAIL
  427. //
  428. //--
  429. //////////////////////////////////////////////////////////////////////////////
  430. STDAPI
  431. DllGetClassObject(
  432. REFCLSID rclsidIn,
  433. REFIID riidIn,
  434. PPVOID ppvOut
  435. )
  436. {
  437. HRESULT hr;
  438. CProvFactory * pObj = NULL;
  439. UINT idx;
  440. UINT cDataArray = sizeof ( g_FactoryDataArray ) / sizeof ( FactoryData );
  441. for ( idx = 0 ; idx < cDataArray ; idx++ )
  442. {
  443. if ( rclsidIn == *g_FactoryDataArray[ idx ].m_pCLSID )
  444. {
  445. pObj= new CProvFactory( &g_FactoryDataArray[ idx ] );
  446. if ( NULL == pObj )
  447. {
  448. return E_OUTOFMEMORY;
  449. }
  450. hr = pObj->QueryInterface( riidIn, ppvOut );
  451. if ( FAILED( hr ) )
  452. {
  453. delete pObj;
  454. }
  455. return hr;
  456. }
  457. }
  458. return E_FAIL;
  459. } //*** DllGetClassObject()