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