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.

434 lines
11 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2000 Microsoft Corporation
  4. //
  5. // Module Name: vdswmi.cpp
  6. //
  7. // Implementation of the provider registration and entry point.
  8. //
  9. // Author: MSP Prabu (mprabu) 04-Dec-2000
  10. // Jim Benton (jbenton) 15-Oct-2000
  11. //
  12. //////////////////////////////////////////////////////////////////////
  13. #include "Pch.h"
  14. #include <initguid.h>
  15. #include "ProvFactory.h"
  16. #include "InstanceProv.h"
  17. ////////////////////////////////////////////////////////////////////////
  18. //// Standard foo for file name aliasing. This code block must be after
  19. //// all includes of VDS header files.
  20. ////
  21. #ifdef VDS_FILE_ALIAS
  22. #undef VDS_FILE_ALIAS
  23. #endif
  24. #define VDS_FILE_ALIAS "VDSWMIPR"
  25. ////
  26. //////////////////////////////////////////////////////////////////////////
  27. //////////////////////////////////////////////////////////////////////////////
  28. // Global Data
  29. //////////////////////////////////////////////////////////////////////////////
  30. const int g_cchRegkey = 128;
  31. // {890CB943-D715-401b-98B1-CF82DCF36D7C}
  32. DEFINE_GUID(CLSID_VDS_PROVIDER,
  33. 0x890CB943,
  34. 0xD715,
  35. 0x401b,
  36. 0x98, 0xB1, 0xCF, 0x82, 0xDC, 0xF3, 0x6D, 0x7C);
  37. // Count number of objects and number of locks.
  38. long g_cObj = 0;
  39. long g_cLock = 0;
  40. HMODULE g_hModule;
  41. FactoryData g_FactoryDataArray[] =
  42. {
  43. {
  44. &CLSID_VDS_PROVIDER,
  45. CInstanceProv::S_HrCreateThis,
  46. PVD_WBEM_PROVIDERNAME
  47. }
  48. };
  49. //////////////////////////////////////////////////////////////////////////////
  50. //++
  51. //
  52. // BOOL
  53. // WINAPI
  54. // DllMain(
  55. // HANDLE hModule,
  56. // DWORD ul_reason_for_call,
  57. // LPVOID lpReserved
  58. // )
  59. //
  60. // Description:
  61. // Main DLL entry point.
  62. //
  63. // Arguments:
  64. // hModule -- DLL module handle.
  65. // ul_reason_for_call --
  66. // lpReserved --
  67. //
  68. // Return Values:
  69. // TRUE
  70. //
  71. //--
  72. //////////////////////////////////////////////////////////////////////////////
  73. BOOL
  74. WINAPI
  75. DllMain(
  76. HANDLE hModule,
  77. DWORD dwReason,
  78. LPVOID lpReserved
  79. )
  80. {
  81. g_hModule = static_cast< HMODULE >( hModule );
  82. if (dwReason == DLL_PROCESS_ATTACH)
  83. {
  84. }
  85. else if (dwReason == DLL_PROCESS_DETACH)
  86. {
  87. }
  88. return TRUE;
  89. } //*** DllMain()
  90. //////////////////////////////////////////////////////////////////////////////
  91. //++
  92. //
  93. // STDAPI
  94. // DllCanUnloadNow( void )
  95. //
  96. // Description:
  97. // Called periodically by Ole in order to determine if the
  98. // DLL can be freed.
  99. //
  100. // Arguments:
  101. // None.
  102. //
  103. // Return Values:
  104. // S_OK if there are no objects in use and the class factory
  105. // isn't locked.
  106. //
  107. //--
  108. //////////////////////////////////////////////////////////////////////////////
  109. STDAPI DllCanUnloadNow( void )
  110. {
  111. SCODE sc;
  112. //It is OK to unload if there are no objects or locks on the
  113. // class factory.
  114. sc = ( 0L == g_cObj && 0L == g_cLock ) ? S_OK : S_FALSE;
  115. return sc;
  116. } //*** DllCanUnloadNow()
  117. //////////////////////////////////////////////////////////////////////////////
  118. //++
  119. //
  120. // STDAPI
  121. // DllRegisterServer( void )
  122. //
  123. // Description:
  124. // Called during setup or by regsvr32.
  125. //
  126. // Arguments:
  127. // None.
  128. //
  129. // Return Values:
  130. // NOERROR if registration successful, error otherwise.
  131. // SELFREG_E_CLASS
  132. //
  133. //--
  134. //////////////////////////////////////////////////////////////////////////////
  135. STDAPI DllRegisterServer( void )
  136. {
  137. WCHAR wszID[ g_cchGUID ];
  138. WCHAR wszCLSID[ g_cchRegkey ];
  139. WCHAR wszModule[ MAX_PATH ];
  140. INT idx;
  141. WCHAR * pwszModel = L"Both";
  142. HKEY hKey1 = NULL;
  143. HKEY hKey2 = NULL;
  144. DWORD dwRet = ERROR_SUCCESS;
  145. INT cArray = sizeof ( g_FactoryDataArray ) / sizeof ( FactoryData );
  146. // Create the path.
  147. try
  148. {
  149. for ( idx = 0 ; idx < cArray && dwRet == ERROR_SUCCESS ; idx++ )
  150. {
  151. LPCWSTR pwszName = g_FactoryDataArray[ idx ].m_pwszRegistryName;
  152. dwRet = StringFromGUID2(
  153. *g_FactoryDataArray[ idx ].m_pCLSID,
  154. wszID,
  155. g_cchGUID
  156. );
  157. if (dwRet == 0)
  158. {
  159. dwRet = ERROR_INSUFFICIENT_BUFFER;
  160. break;
  161. }
  162. if (FAILED(StringCchPrintf(wszCLSID, g_cchRegkey, L"Software\\Classes\\CLSID\\%lS", wszID)))
  163. {
  164. dwRet = ERROR_INSUFFICIENT_BUFFER;
  165. break;
  166. }
  167. wszCLSID[g_cchRegkey - 1] = L'\0';
  168. // Create entries under CLSID
  169. dwRet = RegCreateKeyW(
  170. HKEY_LOCAL_MACHINE,
  171. wszCLSID,
  172. &hKey1
  173. );
  174. if ( dwRet != ERROR_SUCCESS )
  175. {
  176. break;
  177. }
  178. dwRet = RegSetValueEx(
  179. hKey1,
  180. NULL,
  181. 0,
  182. REG_SZ,
  183. (BYTE *) pwszName,
  184. sizeof( WCHAR ) * (lstrlenW( pwszName ) + 1)
  185. );
  186. if ( dwRet != ERROR_SUCCESS )
  187. {
  188. break;
  189. }
  190. dwRet = RegCreateKeyW(
  191. hKey1,
  192. L"InprocServer32",
  193. & hKey2
  194. );
  195. if ( dwRet != ERROR_SUCCESS )
  196. {
  197. break;
  198. }
  199. GetModuleFileName( g_hModule, wszModule, MAX_PATH );
  200. dwRet = RegSetValueEx(
  201. hKey2,
  202. NULL,
  203. 0,
  204. REG_SZ,
  205. (BYTE *) wszModule,
  206. sizeof( WCHAR ) * (lstrlen( wszModule ) + 1)
  207. );
  208. if ( dwRet != ERROR_SUCCESS )
  209. {
  210. break;
  211. }
  212. dwRet = RegSetValueExW(
  213. hKey2,
  214. L"ThreadingModel",
  215. 0,
  216. REG_SZ,
  217. (BYTE *) pwszModel,
  218. sizeof( WCHAR ) * (lstrlen( pwszModel ) + 1)
  219. );
  220. if ( dwRet != ERROR_SUCCESS )
  221. {
  222. break;
  223. }
  224. RegCloseKey( hKey1 );
  225. hKey1 = NULL;
  226. RegCloseKey( hKey2 );
  227. hKey2 = NULL;
  228. } // for: each entry in factory entry array
  229. }
  230. catch ( ... )
  231. {
  232. dwRet = SELFREG_E_CLASS;
  233. }
  234. if (hKey1 != NULL)
  235. RegCloseKey( hKey1 );
  236. if (hKey2 != NULL)
  237. RegCloseKey( hKey2 );
  238. return dwRet;
  239. } //*** DllRegisterServer()
  240. //////////////////////////////////////////////////////////////////////////////
  241. //++
  242. //
  243. // STDAPI
  244. // DllUnregisterServer( void )
  245. //
  246. // Description:
  247. // Called when it is time to remove the registry entries.
  248. //
  249. // Arguments:
  250. // None.
  251. //
  252. // Return Values:
  253. // NOERROR if registration successful, error otherwise.
  254. // SELFREG_E_CLASS
  255. //
  256. //--
  257. //////////////////////////////////////////////////////////////////////////////
  258. STDAPI DllUnregisterServer( void )
  259. {
  260. WCHAR wszID[ g_cchGUID ];
  261. WCHAR wszCLSID[ g_cchRegkey ];
  262. HKEY hKey;
  263. INT idx;
  264. DWORD dwRet = ERROR_SUCCESS;
  265. INT cArray = sizeof ( g_FactoryDataArray ) / sizeof ( FactoryData );
  266. for ( idx = 0 ; idx < cArray && dwRet == ERROR_SUCCESS ; idx++ )
  267. {
  268. dwRet = StringFromGUID2(
  269. *g_FactoryDataArray[ idx ].m_pCLSID,
  270. wszID,
  271. g_cchGUID
  272. );
  273. if (dwRet == 0)
  274. {
  275. dwRet = ERROR_INSUFFICIENT_BUFFER;
  276. break;
  277. }
  278. if (FAILED(StringCchPrintf(wszCLSID, g_cchRegkey, L"Software\\Classes\\CLSID\\%lS", wszID)))
  279. {
  280. dwRet = ERROR_INSUFFICIENT_BUFFER;
  281. break;
  282. }
  283. wszCLSID[g_cchRegkey - 1] = L'\0';
  284. // First delete the InProcServer subkey.
  285. dwRet = RegOpenKeyW(
  286. HKEY_LOCAL_MACHINE,
  287. wszCLSID,
  288. &hKey
  289. );
  290. if ( dwRet != ERROR_SUCCESS )
  291. {
  292. continue;
  293. }
  294. dwRet = RegDeleteKeyW( hKey, L"InProcServer32" );
  295. RegCloseKey( hKey );
  296. if ( dwRet != ERROR_SUCCESS )
  297. {
  298. break;
  299. }
  300. dwRet = RegOpenKeyW(
  301. HKEY_LOCAL_MACHINE,
  302. L"Software\\Classes\\CLSID",
  303. &hKey
  304. );
  305. if ( dwRet != ERROR_SUCCESS )
  306. {
  307. break;
  308. }
  309. dwRet = RegDeleteKeyW( hKey,wszID );
  310. RegCloseKey( hKey );
  311. if ( dwRet != ERROR_SUCCESS )
  312. {
  313. break;
  314. }
  315. } // for: each object
  316. //if ( dwRet != ERROR_SUCCESS )
  317. //{
  318. // dwRet = SELFREG_E_CLASS;
  319. //}
  320. return S_OK;
  321. } //*** DllUnregisterServer()
  322. //////////////////////////////////////////////////////////////////////////////
  323. //++
  324. //
  325. // STDAPI
  326. // DllGetClassObject(
  327. // REFCLSID rclsidIn,
  328. // REFIID riidIn,
  329. // PPVOID ppvOut
  330. // )
  331. //
  332. // Description:
  333. // Called by Ole when some client wants a class factory. Return
  334. // one only if it is the sort of class this DLL supports.
  335. //
  336. // Arguments:
  337. // rclsidIn --
  338. // riidIn --
  339. // ppvOut --
  340. //
  341. // Return Values:
  342. // NOERROR if registration successful, error otherwise.
  343. // E_OUTOFMEMORY
  344. // E_FAIL
  345. //
  346. //--
  347. //////////////////////////////////////////////////////////////////////////////
  348. STDAPI
  349. DllGetClassObject(
  350. REFCLSID rclsidIn,
  351. REFIID riidIn,
  352. PPVOID ppvOut
  353. )
  354. {
  355. HRESULT hr;
  356. CProvFactory * pObj = NULL;
  357. UINT idx;
  358. UINT cDataArray = sizeof ( g_FactoryDataArray ) / sizeof ( FactoryData );
  359. for ( idx = 0 ; idx < cDataArray ; idx++ )
  360. {
  361. if ( IsEqualCLSID(rclsidIn, *g_FactoryDataArray[ idx ].m_pCLSID) )
  362. {
  363. pObj= new CProvFactory( &g_FactoryDataArray[ idx ] );
  364. if ( NULL == pObj )
  365. {
  366. return E_OUTOFMEMORY;
  367. }
  368. hr = pObj->QueryInterface( riidIn, ppvOut );
  369. if ( FAILED( hr ) )
  370. {
  371. delete pObj;
  372. }
  373. return hr;
  374. }
  375. }
  376. return E_FAIL;
  377. } //*** DllGetClassObject()