Source code of Windows XP (NT5)
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.

907 lines
31 KiB

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft WMI OLE DB Provider
  4. // (C) Copyright 1999 Microsoft Corporation. All Rights Reserved.
  5. //
  6. // The module contains the DLL Entry and Exit points
  7. //
  8. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. #define DECLARE_GLOBALS
  10. //===============================================================================
  11. // Don't include everything from windows.h, but always bring in OLE 2 support
  12. //===============================================================================
  13. //#define WIN32_LEAN_AND_MEAN
  14. #define INC_OLE2
  15. //===============================================================================
  16. // Make sure constants get initialized
  17. //===============================================================================
  18. #define INITGUID
  19. #define DBINITCONSTANTS
  20. //===============================================================================
  21. // Basic Windows and OLE everything
  22. //===============================================================================
  23. #include <windows.h>
  24. //===============================================================================
  25. // OLE DB headers
  26. //===============================================================================
  27. #include "oledb.h"
  28. #include "oledberr.h"
  29. //===============================================================================
  30. // Data conversion library header
  31. //===============================================================================
  32. #include "msdadc.h"
  33. //===============================================================================
  34. // Guids for data conversion library
  35. //===============================================================================
  36. #include "msdaguid.h"
  37. //===============================================================================
  38. // GUIDs
  39. //===============================================================================
  40. #include "guids.h"
  41. //===============================================================================
  42. // OLEDB RootBinder
  43. //===============================================================================
  44. #include "msdasc.h"
  45. //===============================================================================
  46. // Common project stuff
  47. //===============================================================================
  48. #include "headers.h"
  49. #include "classfac.h"
  50. #include "binderclassfac.h"
  51. #include "binder.h"
  52. //===============================================================================
  53. // Globals
  54. //===============================================================================
  55. LONG g_cObj; // # of outstanding objects
  56. LONG g_cLock; // # of explicit locks set
  57. DWORD g_cAttachedProcesses; // # of attached processes
  58. DWORD g_dwPageSize; // System page size
  59. long glGlobalErrorInit = 0;
  60. BOOL CGlobals::m_bInitialized = FALSE;
  61. STDAPI DllUnregisterServer( void );
  62. //===============================================================================
  63. // Static vars
  64. //===============================================================================
  65. /*
  66. static const WCHAR * s_strDllName = L"WMIOLEDB";
  67. static const struct{
  68. PWSTR strRegKey;
  69. PWSTR strValueName;
  70. DWORD dwType;
  71. PWSTR strValue;
  72. } s_rgRegInfo[] =
  73. {
  74. { L"WMIOLEDB", NULL, REG_SZ, L"Microsoft WMI OLE DB Provider" },
  75. { L"WMIOLEDB\\Clsid", NULL, REG_SZ, L"{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}" },
  76. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}", NULL, REG_SZ, L"WMIOLEDB" },
  77. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}", L"OLEDB_SERVICES", REG_DWORD, L"-1" },
  78. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}\\ProgID", NULL, REG_SZ, L"WMIOLEDB" },
  79. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}\\VersionIndependentProgID", NULL, REG_SZ, L"WMIOLEDB" },
  80. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}\\InprocServer32", NULL, REG_SZ, L"%s" },
  81. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}\\InprocServer32", L"ThreadingModel", REG_SZ, L"Both" },
  82. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}\\OLE DB Provider", NULL, REG_SZ, L"Microsoft WMI OLE DB Provider" },
  83. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}\\ExtendedErrors", NULL, REG_SZ, L"Extended Error Service" },
  84. { L"CLSID\\{FD8D9C02-265E-11d2-98D9-00A0C9B7CBFE}\\ExtendedErrors\\{80C4A61D-CB78-46fd-BD8F-8BF45BE46A4C}", NULL, REG_SZ, L"WMIOLEDB Error Lookup" },
  85. { L"WMIOLEDB.ErrorLookup", NULL, REG_SZ, L"WMIOLEDB Error Lookup" },
  86. { L"WMIOLEDB.ErrorLookup\\Clsid", NULL, REG_SZ, L"{80C4A61D-CB78-46fd-BD8F-8BF45BE46A4C}" },
  87. { L"CLSID\\{80C4A61D-CB78-46fd-BD8F-8BF45BE46A4C}", NULL, REG_SZ, L"WMIOLEDB.ErrorLookup" },
  88. { L"CLSID\\{80C4A61D-CB78-46fd-BD8F-8BF45BE46A4C}\\ProgID", NULL, REG_SZ, L"WMIOLEDB.ErrorLookup" },
  89. { L"CLSID\\{80C4A61D-CB78-46fd-BD8F-8BF45BE46A4C}\\VersionIndependentProgID", NULL, REG_SZ, L"WMIOLEDB.ErrorLookup" },
  90. { L"CLSID\\{80C4A61D-CB78-46fd-BD8F-8BF45BE46A4C}\\InprocServer32", NULL, REG_SZ, L"%s" },
  91. { L"CLSID\\{80C4A61D-CB78-46fd-BD8F-8BF45BE46A4C}\\InprocServer32", L"ThreadingModel", REG_SZ, L"Both" },
  92. { L"WMIOLEDB.RootBinder", NULL, REG_SZ, L"Microsoft WMI OLE DB Root Binder" },
  93. { L"WMIOLEDB.RootBinder\\Clsid", NULL, REG_SZ, L"{CDCEDB81-5FEC-11d3-9D1C-00C04F5F1164}" },
  94. { L"CLSID\\{CDCEDB81-5FEC-11d3-9D1C-00C04F5F1164}", NULL, REG_SZ, L"WMIOLEDB.RootBinder" },
  95. { L"CLSID\\{CDCEDB81-5FEC-11d3-9D1C-00C04F5F1164}\\ProgID", NULL, REG_SZ, L"WMIOLEDB.RootBinder" },
  96. { L"CLSID\\{CDCEDB81-5FEC-11d3-9D1C-00C04F5F1164}\\VersionIndependentProgID", NULL, REG_SZ, L"WMIOLEDB.RootBinder" },
  97. { L"CLSID\\{CDCEDB81-5FEC-11d3-9D1C-00C04F5F1164}\\InprocServer32", NULL, REG_SZ, L"%s" },
  98. { L"CLSID\\{CDCEDB81-5FEC-11d3-9D1C-00C04F5F1164}\\InprocServer32", L"ThreadingModel", REG_SZ, L"Both" },
  99. { L"CLSID\\{CDCEDB81-5FEC-11d3-9D1C-00C04F5F1164}\\OLE DB Binder", NULL, REG_SZ, L"Microsoft WMI OLE DB Root Binder" },
  100. { L"WMIOLEDB.Enumerator", NULL, REG_SZ, L"Microsoft WMI OLE DB Enumerator" },
  101. { L"WMIOLEDB.Enumerator\\Clsid", NULL, REG_SZ, L"{E14321B2-67C0-11d3-B3B4-00104BCC48C4}" },
  102. { L"CLSID\\{E14321B2-67C0-11d3-B3B4-00104BCC48C4}", NULL, REG_SZ, L"WMIOLEDB.Enumerator" },
  103. { L"CLSID\\{E14321B2-67C0-11d3-B3B4-00104BCC48C4}\\ProgID", NULL, REG_SZ, L"WMIOLEDB.Enumerator" },
  104. { L"CLSID\\{E14321B2-67C0-11d3-B3B4-00104BCC48C4}\\VersionIndependentProgID", NULL, REG_SZ, L"WMIOLEDB.Enumerator" },
  105. { L"CLSID\\{E14321B2-67C0-11d3-B3B4-00104BCC48C4}\\InprocServer32", NULL, REG_SZ, L"%s" },
  106. { L"CLSID\\{E14321B2-67C0-11d3-B3B4-00104BCC48C4}\\InprocServer32", L"ThreadingModel", REG_SZ, L"Both" },
  107. { L"CLSID\\{E14321B2-67C0-11d3-B3B4-00104BCC48C4}\\OLE DB Enumerator", NULL, REG_SZ, L"Microsoft WMI OLE DB Enumerator" },
  108. };
  109. */
  110. static CTString s_strDllName;
  111. static const struct{
  112. UINT uRegKey;
  113. UINT uValueName;
  114. DWORD dwType;
  115. UINT uValue;
  116. } s_rgRegInfo[] =
  117. {
  118. { IDS_PROGID, 0, REG_SZ, IDS_DESCRIPTION },
  119. { IDS_PROGCLSID, 0, REG_SZ, IDS_CLSID },
  120. { IDS_CLSIDKEY, 0, REG_SZ, IDS_PROGID},
  121. { IDS_CLSIDKEY, IDS_OLEDBSER, REG_DWORD, -1 },
  122. { IDS_PROGIDKEY, 0, REG_SZ, IDS_PROGID },
  123. { IDS_VIPROGIDKEY, 0, REG_SZ, IDS_PROGID },
  124. { IDS_INPROCSER, 0, REG_SZ, IDS_STRFORMAT },
  125. { IDS_INPROCSER, IDS_THREADMODEL,REG_SZ, IDS_BOTHTHREADMODEL },
  126. { IDS_OLEDBPROVKEY, 0, REG_SZ, IDS_DESCRIPTION},
  127. { IDS_EXTERROR, 0, REG_SZ, IDS_EXTERRDESC },
  128. { IDS_EXTERRORCLSIDKEY, 0, REG_SZ, IDS_ERRLOOKUPDESC },
  129. { IDS_EL_PROGID, 0, REG_SZ, IDS_ERRLOOKUPDESC },
  130. { IDS_EL_PROGCLSID, 0, REG_SZ, IDS_EL_CLSID },
  131. { IDS_EL_CLSIDKEY, 0, REG_SZ, IDS_EL_PROGID },
  132. { IDS_EL_CLSPROGID, 0, REG_SZ, IDS_EL_PROGID },
  133. { IDS_EL_CLSVIPROGID, 0, REG_SZ, IDS_EL_PROGID },
  134. { IDS_EL_INPROCSERKEY, 0, REG_SZ, IDS_STRFORMAT },
  135. { IDS_EL_INPROCSERKEY, IDS_THREADMODEL,REG_SZ, IDS_BOTHTHREADMODEL},
  136. { IDS_RB_PROGID, 0, REG_SZ, IDS_RB_DESC },
  137. { IDS_RB_PROGCLSID, 0, REG_SZ, IDS_RB_CLSID },
  138. { IDS_RB_CLSIDKEY, 0, REG_SZ, IDS_RB_PROGID },
  139. { IDS_RB_CLSPROGID, 0, REG_SZ, IDS_RB_PROGID },
  140. { IDS_RB_CLSVIPROGID, 0, REG_SZ, IDS_RB_PROGID },
  141. { IDS_RB_INPROCSERKEY, 0, REG_SZ, IDS_STRFORMAT},
  142. { IDS_RB_INPROCSERKEY, IDS_THREADMODEL,REG_SZ, IDS_BOTHTHREADMODEL },
  143. { IDS_RB_ROOTBINDER, 0, REG_SZ, IDS_RB_DESC},
  144. { IDS_EN_PROGID, 0, REG_SZ, IDS_EN_DESC },
  145. { IDS_EN_PROGCLSID, 0, REG_SZ, IDS_EN_CLSID },
  146. { IDS_EN_CLSIDKEY, 0, REG_SZ, IDS_EN_PROGID },
  147. { IDS_EN_CLSPROGID, 0, REG_SZ, IDS_EN_PROGID },
  148. { IDS_EN_CLSVIPROGID, 0, REG_SZ, IDS_EN_PROGID },
  149. { IDS_EN_INPROCSERKEY, 0, REG_SZ, IDS_STRFORMAT },
  150. { IDS_EN_INPROCSERKEY, IDS_THREADMODEL,REG_SZ, IDS_BOTHTHREADMODEL },
  151. { IDS_EN_ENUMERATOR, 0, REG_SZ, IDS_EN_DESC },
  152. };
  153. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154. // Verify and create the objects if requiered
  155. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  156. HRESULT VerifyGlobalErrorHandling()
  157. {
  158. HRESULT hr = S_OK;
  159. if( InterlockedIncrement(&glGlobalErrorInit) == 1 ){
  160. g_pCError = new CError;
  161. if( g_pCError ){
  162. hr = g_pCError->FInit();
  163. if( hr == S_OK ){
  164. hr = CoGetClassObject(CLSID_EXTENDEDERRORINFO, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory,(void **) & g_pErrClassFact);
  165. }
  166. }
  167. else{
  168. LogMessage("Could not instantiate internal error handling object\n");
  169. hr = E_FAIL;
  170. }
  171. }
  172. if( hr != S_OK ){
  173. glGlobalErrorInit = 0;
  174. }
  175. else
  176. {
  177. g_pErrClassFact->LockServer(TRUE);
  178. }
  179. return hr;
  180. }
  181. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  182. // Release the error releated interfaces and object
  183. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  184. HRESULT ReleaseGlobalErrorHandling()
  185. {
  186. HRESULT hr = E_FAIL;
  187. if( InterlockedDecrement(&glGlobalErrorInit) == 0 ){
  188. if( g_pCError ){
  189. if(g_pErrClassFact)
  190. {
  191. g_pErrClassFact->LockServer(FALSE);
  192. }
  193. SAFE_RELEASE_PTR(g_pErrClassFact);
  194. SAFE_DELETE_PTR(g_pCError);
  195. hr = S_OK;
  196. }
  197. }
  198. return hr;
  199. }
  200. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  201. // Registering the Root Binder object of the Provider
  202. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203. HRESULT RegisterRootBinder()
  204. {
  205. HRESULT hr = S_OK;
  206. IRegisterProvider *pRegisterProvider = NULL;
  207. if(SUCCEEDED(hr = CoCreateInstance(CLSID_RootBinder, NULL,CLSCTX_INPROC_SERVER, IID_IRegisterProvider ,(void **)&pRegisterProvider)))
  208. {
  209. hr = pRegisterProvider->SetURLMapping(UMIURLPREFIX,0,CLSID_WMIOLEDB_ROOTBINDER);
  210. hr = pRegisterProvider->SetURLMapping(WMIURLPREFIX,0,CLSID_WMIOLEDB_ROOTBINDER);
  211. pRegisterProvider->Release();
  212. }
  213. return hr;
  214. }
  215. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  216. // Unregistering the Root Binder object of the Provider
  217. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  218. HRESULT UnRegisterRootBinder()
  219. {
  220. HRESULT hr = S_OK;
  221. IRegisterProvider *pRegisterProvider = NULL;
  222. if(SUCCEEDED(hr = CoCreateInstance(CLSID_RootBinder, NULL, CLSCTX_INPROC_SERVER, IID_IRegisterProvider ,(void **)&pRegisterProvider)))
  223. {
  224. hr = pRegisterProvider->UnregisterProvider(UMIURLPREFIX,0,CLSID_WMIOLEDB_ROOTBINDER);
  225. hr = pRegisterProvider->UnregisterProvider(WMIURLPREFIX,0,CLSID_WMIOLEDB_ROOTBINDER);
  226. pRegisterProvider->Release();
  227. }
  228. return hr;
  229. }
  230. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  231. //
  232. // DLL Entry point where Instance and Thread attach/detach notifications takes place.
  233. // OLE is initialized and the IMalloc Interface pointer is obtained.
  234. //
  235. // Boolean Flag
  236. // TRUE Successful initialization
  237. // FALSE Failure to intialize
  238. //
  239. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  240. BOOL WINAPI DllMain( HINSTANCE hInstDLL, // IN Application Instance Handle
  241. DWORD fdwReason, // IN Indicated Process or Thread activity
  242. LPVOID lpvReserved // IN Reserved...
  243. )
  244. {
  245. BOOL fRetVal = FALSE;
  246. CSetStructuredExceptionHandler seh;
  247. // TRY_BLOCK;
  248. switch (fdwReason)
  249. {
  250. LogMessage("Inside DllMain");
  251. case DLL_PROCESS_ATTACH:
  252. {
  253. //==================================================================
  254. // Assume successfully initialized
  255. //==================================================================
  256. fRetVal = TRUE;
  257. //==================================================================
  258. // Do one-time initialization when first process attaches
  259. //==================================================================
  260. if (!g_cAttachedProcesses)
  261. {
  262. g_hInstance = hInstDLL;
  263. // Initialize the global variables
  264. g_cgGlobals.Init();
  265. }
  266. //==============================================================
  267. // Do per-process initialization here...
  268. // Remember that another process successfully attached
  269. //==============================================================
  270. g_cAttachedProcesses++;
  271. break;
  272. }
  273. case DLL_PROCESS_DETACH:
  274. {
  275. //==============================================================
  276. // Do per-process clean up here...
  277. // Remember that a process has detached
  278. //==============================================================
  279. g_cAttachedProcesses--;
  280. break;
  281. }
  282. case DLL_THREAD_ATTACH:
  283. case DLL_THREAD_DETACH:
  284. {
  285. fRetVal = TRUE;
  286. break;
  287. }
  288. } // switch
  289. // CATCH_BLOCK_BOOL(fRetVal,L"DllMain");
  290. return fRetVal;
  291. }
  292. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  293. //
  294. // This function is exposed to OLE so that the classfactory can be obtained.
  295. //
  296. // HRESULT indicating status of routine
  297. // S_OK The object was retrieved successfully.
  298. // CLASS_E_CLASSNOTAVAILABLE DLL does not support class.
  299. // E_OUTOFMEMORY Out of memory.
  300. // E_INVALIDARG One or more arguments are invalid.
  301. // E_UNEXPECTED An unexpected error occurred.
  302. // OTHER Other HRESULTs returned by called functions
  303. //
  304. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  305. HRESULT CALLBACK DllGetClassObject( REFCLSID rclsid, // IN CLSID of the object class to be loaded
  306. REFIID riid, // IN Interface on object to be instantiated
  307. LPVOID * ppvObj // OUT Pointer to interface that was instantiated
  308. )
  309. {
  310. // CClassFactory * pClassFactory;
  311. IClassFactory * pClassFactory = NULL;
  312. HRESULT hr = S_OK;
  313. CSetStructuredExceptionHandler seh;
  314. TRY_BLOCK;
  315. //=======================================================================
  316. // Check for valid ppvObj pointer
  317. //=======================================================================
  318. if (!ppvObj){
  319. hr = E_INVALIDARG;
  320. LogMessage("DllGetClassObject failed with invalid argument");
  321. }
  322. else
  323. {
  324. //===================================================================
  325. // In case we fail, we need to zero output arguments
  326. //===================================================================
  327. *ppvObj = NULL;
  328. //===================================================================
  329. // We only service CLSID_WMIOLEDB
  330. //===================================================================
  331. if (!(rclsid == CLSID_WMIOLEDB || rclsid == CLSID_WMIOLEDB_ROOTBINDER ||
  332. rclsid == CLSID_WMIOLEDB_ENUMERATOR || rclsid == CLSID_WMIOLEDB_ERRORLOOOKUP))
  333. {
  334. hr = CLASS_E_CLASSNOTAVAILABLE;
  335. LogMessage("DllGetClassObject failed with CLSID_WMIOLEDB not available - Not registered?");
  336. }
  337. else{
  338. //===============================================================
  339. // We only support the IUnknown and IClassFactory interfaces
  340. //===============================================================
  341. if (riid != IID_IUnknown && riid != IID_IClassFactory){
  342. hr = E_NOINTERFACE;
  343. LogMessage("DllGetClassObject failed with no interface");
  344. }
  345. else{
  346. try
  347. {
  348. //===========================================================
  349. // Create our ClassFactory object
  350. //===========================================================
  351. if( rclsid == CLSID_WMIOLEDB ){
  352. pClassFactory = new CDataSourceClassFactory();
  353. }
  354. else if( rclsid == CLSID_WMIOLEDB_ENUMERATOR ){
  355. pClassFactory = new CEnumeratorClassFactory();
  356. }
  357. else if( rclsid == CLSID_WMIOLEDB_ROOTBINDER ){
  358. pClassFactory = new CBinderClassFactory();
  359. }
  360. else if( rclsid == CLSID_WMIOLEDB_ERRORLOOOKUP ){
  361. pClassFactory = new CErrorLookupClassFactory();
  362. }
  363. }
  364. catch(...)
  365. {
  366. SAFE_DELETE_PTR(pClassFactory);
  367. throw;
  368. }
  369. if (pClassFactory == NULL){
  370. hr = E_OUTOFMEMORY;
  371. LogMessage("DllGetClassObject failed - out of memory");
  372. }
  373. else{
  374. //=======================================================
  375. // Get the desired interface on this object
  376. //=======================================================
  377. hr = pClassFactory->QueryInterface( riid, ppvObj );
  378. if (!SUCCEEDED( hr )){
  379. SAFE_DELETE_PTR( pClassFactory );
  380. LogMessage("DllGetClassObject failed - return code:",hr);
  381. }
  382. }
  383. }
  384. }
  385. }
  386. CATCH_BLOCK_HRESULT(hr,L"DllGetClassObject");
  387. return hr;
  388. }
  389. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  390. //
  391. // Indicates whether the DLL is no longer in use and can be unloaded.
  392. //
  393. // HRESULT indicating status of routine
  394. // S_OK DLL can be unloaded now.
  395. // S_FALSE DLL cannot be unloaded now.
  396. //
  397. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  398. STDAPI DllCanUnloadNow( void )
  399. {
  400. HRESULT hr = S_OK;
  401. CSetStructuredExceptionHandler seh;
  402. TRY_BLOCK;
  403. // if(!(!g_cObj && !g_cLock))
  404. if(g_cObj || g_cLock)
  405. {
  406. hr = S_FALSE;
  407. }
  408. CATCH_BLOCK_HRESULT(hr,L"DllCanUnloadNow");
  409. return hr;
  410. }
  411. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  412. //
  413. // Adds necessary keys to the registry.
  414. //
  415. // Returns one of the following
  416. // NOERROR Registration succeeded
  417. // E_FAIL Something didn't work
  418. //
  419. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  420. STDAPI DllRegisterServer( void )
  421. {
  422. HKEY hk;
  423. HMODULE hModule = 0;
  424. DWORD dwDisposition;
  425. LONG stat;
  426. TCHAR strFileName[MAX_PATH+1];
  427. TCHAR strOutBuff[300+1];
  428. HRESULT hr = S_OK;
  429. CTString strKey,strValueName,strValue;
  430. DWORD dwValue;
  431. CSetStructuredExceptionHandler seh;
  432. TRY_BLOCK;
  433. hModule = GetModuleHandle(s_strDllName );
  434. //================================================================
  435. // Get the full path name for this DLL.
  436. //================================================================
  437. if (hModule == NULL)
  438. {
  439. hr = E_FAIL;
  440. LogMessage("DllRegisterServer: GetModuleHandle failed");
  441. }
  442. else
  443. if (0 == GetModuleFileName( hModule, strFileName, sizeof( strFileName ) / sizeof(TCHAR)))
  444. {
  445. hr = E_FAIL;
  446. LogMessage("DllRegisterServer: GetModuleFileName failed");
  447. }
  448. else
  449. {
  450. //============================================================
  451. // Make a clean start
  452. //============================================================
  453. //============================================================
  454. // Loop through s_rgRegInfo, and put everything in it.
  455. // Every entry is based on HKEY_CLASSES_ROOT.
  456. //============================================================
  457. for (ULONG i=0; i < NUMELEM( s_rgRegInfo ); i++)
  458. {
  459. // NTRaid: 136432 , 136436
  460. // 07/05/00
  461. if(SUCCEEDED(hr =strValue.LoadStr(s_rgRegInfo[i].uValue)) &&
  462. SUCCEEDED(hr =strKey.LoadStr(s_rgRegInfo[i].uRegKey)) &&
  463. SUCCEEDED(hr =strValueName.LoadStr(s_rgRegInfo[i].uValueName)))
  464. {
  465. //========================================================
  466. // Fill in any "%s" arguments with the name of this DLL.
  467. //========================================================
  468. if (s_rgRegInfo[i].dwType == REG_DWORD)
  469. {
  470. dwValue = (DWORD)s_rgRegInfo[i].uValue;
  471. }
  472. else
  473. {
  474. _stprintf( strOutBuff, strValue, strFileName );
  475. }
  476. //========================================================
  477. // Create the Key. If it exists, we open it.
  478. // Thus we can still change the value below.
  479. //========================================================
  480. stat = RegCreateKeyEx( HKEY_CLASSES_ROOT,
  481. strKey,
  482. 0,
  483. NULL,
  484. REG_OPTION_NON_VOLATILE,
  485. KEY_ALL_ACCESS,
  486. NULL,
  487. &hk,
  488. &dwDisposition );
  489. if (stat != ERROR_SUCCESS){
  490. hr = E_FAIL ;
  491. LogMessage("DllRegisterServer: failed to create key");
  492. LogMessage(strKey);
  493. }
  494. else{
  495. stat = RegSetValueEx( hk,
  496. strValueName,
  497. 0,
  498. s_rgRegInfo[i].dwType,
  499. s_rgRegInfo[i].dwType == REG_DWORD ? (BYTE *) &dwValue :(BYTE *) strOutBuff,
  500. s_rgRegInfo[i].dwType == REG_SZ ? (_tcslen( strOutBuff ) + 1) * sizeof(TCHAR) :sizeof(DWORD));
  501. RegCloseKey( hk );
  502. if (stat != ERROR_SUCCESS){
  503. LogMessage("DllRegisterServer: failed to set value");
  504. LogMessage(strValueName);
  505. hr = E_FAIL;
  506. }
  507. }
  508. }
  509. else
  510. {
  511. // remove all the registry entries
  512. DllUnregisterServer();
  513. break;
  514. }
  515. }
  516. }
  517. if(SUCCEEDED(hr))
  518. {
  519. //=========================================================
  520. // Register the root binder object of the provider
  521. //=========================================================
  522. hr = RegisterRootBinder();
  523. }
  524. CATCH_BLOCK_HRESULT(hr,L"DllUnregisterServer");
  525. return hr;
  526. }
  527. STDAPI DllUnregisterServer( void )
  528. {
  529. int i;
  530. int iNumErrors = 0;
  531. LONG stat;
  532. HRESULT hr = S_OK;
  533. CSetStructuredExceptionHandler seh;
  534. CTString strKey;
  535. TRY_BLOCK;
  536. //==========================================================
  537. // UnRegister the root binder object of the provider
  538. //==========================================================
  539. UnRegisterRootBinder();
  540. //=========================================================================
  541. // Delete all table entries. Loop in reverse order, since they
  542. // are entered in a basic-to-complex order.
  543. // We cannot delete a key that has subkeys.
  544. // Ignore errors.
  545. //=========================================================================
  546. for (i=NUMELEM( s_rgRegInfo ) - 1; i >= 0; i--)
  547. {
  548. // NTRaid: 136432 , 136436
  549. // 07/05/00
  550. if(SUCCEEDED(hr = strKey.LoadStr(s_rgRegInfo[i].uRegKey)))
  551. {
  552. stat = RegDeleteKey( HKEY_CLASSES_ROOT, strKey );
  553. if ((stat != ERROR_SUCCESS) && (stat != ERROR_FILE_NOT_FOUND) ){
  554. iNumErrors++;
  555. hr = E_FAIL;
  556. LogMessage("DllUnregisterServer failed");
  557. }
  558. }
  559. else
  560. {
  561. break;
  562. }
  563. }
  564. CATCH_BLOCK_HRESULT(hr,L"DllUnregisterServer");
  565. return hr;
  566. }
  567. /*
  568. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  569. //
  570. // Removes keys to the registry.
  571. //
  572. // Returns NOERROR
  573. //
  574. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  575. STDAPI DllUnregisterServer( void )
  576. {
  577. int i;
  578. int iNumErrors = 0;
  579. LONG stat;
  580. HRESULT hr = S_OK;
  581. CSetStructuredExceptionHandler seh;
  582. TRY_BLOCK;
  583. //==========================================================
  584. // UnRegister the root binder object of the provider
  585. //==========================================================
  586. UnRegisterRootBinder();
  587. //=========================================================================
  588. // Delete all table entries. Loop in reverse order, since they
  589. // are entered in a basic-to-complex order.
  590. // We cannot delete a key that has subkeys.
  591. // Ignore errors.
  592. //=========================================================================
  593. for (i=NUMELEM( s_rgRegInfo ) - 1; i >= 0; i--) {
  594. stat = RegDeleteKeyW( HKEY_CLASSES_ROOT, s_rgRegInfo[i].strRegKey );
  595. if ((stat != ERROR_SUCCESS) && (stat != ERROR_FILE_NOT_FOUND) ){
  596. iNumErrors++;
  597. hr = E_FAIL;
  598. LogMessage("DllUnregisterServer failed");
  599. }
  600. }
  601. CATCH_BLOCK_HRESULT(hr,L"DllUnregisterServer");
  602. return hr;
  603. }
  604. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  605. //
  606. // Adds necessary keys to the registry.
  607. //
  608. // Returns one of the following
  609. // NOERROR Registration succeeded
  610. // E_FAIL Something didn't work
  611. //
  612. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  613. STDAPI DllRegisterServer( void )
  614. {
  615. HKEY hk;
  616. HMODULE hModule;
  617. DWORD dwDisposition;
  618. LONG stat;
  619. WCHAR strFileName[MAX_PATH+1];
  620. WCHAR strOutBuff[300+1];
  621. HRESULT hr = S_OK;
  622. CSetStructuredExceptionHandler seh;
  623. TRY_BLOCK;
  624. //================================================================
  625. // Get the full path name for this DLL.
  626. //================================================================
  627. if (NULL == (hModule = GetModuleHandleW( s_strDllName ))){
  628. hr = E_FAIL;
  629. LogMessage("DllRegisterServer: GetModuleHandle failed");
  630. }
  631. else if (0 == GetModuleFileNameW( hModule, strFileName, sizeof( strFileName ) / sizeof( char ))){
  632. hr = E_FAIL;
  633. LogMessage("DllRegisterServer: GetModuleFileName failed");
  634. }
  635. else{
  636. //============================================================
  637. // Make a clean start
  638. //============================================================
  639. // DllUnregisterServer();
  640. DWORD dwType = REG_DWORD;
  641. //============================================================
  642. // Loop through s_rgRegInfo, and put everything in it.
  643. // Every entry is based on HKEY_CLASSES_ROOT.
  644. //============================================================
  645. for (ULONG i=0; i < NUMELEM( s_rgRegInfo ); i++){
  646. //========================================================
  647. // Fill in any "%s" arguments with the name of this DLL.
  648. //========================================================
  649. if (s_rgRegInfo[i].dwType == REG_DWORD)
  650. {
  651. *(DWORD*)strOutBuff = _wtol( s_rgRegInfo[i].strValue );
  652. }
  653. else
  654. {
  655. wsprintfW( strOutBuff, s_rgRegInfo[i].strValue, strFileName );
  656. }
  657. //========================================================
  658. // Create the Key. If it exists, we open it.
  659. // Thus we can still change the value below.
  660. //========================================================
  661. stat = RegCreateKeyExW( HKEY_CLASSES_ROOT, s_rgRegInfo[i].strRegKey,
  662. 0, // dwReserved
  663. NULL, // lpszClass
  664. REG_OPTION_NON_VOLATILE,
  665. KEY_ALL_ACCESS, // security access mask
  666. NULL, // lpSecurityAttributes
  667. &hk, // phkResult
  668. &dwDisposition );
  669. if (stat != ERROR_SUCCESS){
  670. hr = E_FAIL ;
  671. LogMessage("DllRegisterServer: failed to create key");
  672. LogMessage(s_rgRegInfo[i].strRegKey);
  673. }
  674. else{
  675. stat = RegSetValueExW(hk,s_rgRegInfo[i].strValueName, // lpszValueName
  676. 0, // dwReserved
  677. s_rgRegInfo[i].dwType, // fdwType
  678. (BYTE *) strOutBuff, // value
  679. s_rgRegInfo[i].dwType == REG_SZ ?
  680. (wcslen( strOutBuff ) + 1) * sizeof(WCHAR) : // cbData, including null terminator
  681. sizeof(DWORD));
  682. RegCloseKey( hk );
  683. if (stat != ERROR_SUCCESS){
  684. LogMessage("DllRegisterServer: failed to set value");
  685. LogMessage(s_rgRegInfo[i].strValueName);
  686. hr = E_FAIL;
  687. }
  688. }
  689. }
  690. }
  691. //=========================================================
  692. // Register the root binder object of the provider
  693. //=========================================================
  694. hr = RegisterRootBinder();
  695. CATCH_BLOCK_HRESULT(hr,L"DllUnregisterServer");
  696. return hr;
  697. }
  698. */
  699. /////////////////////////////////////////////////////////////////////////////
  700. // Constructor
  701. /////////////////////////////////////////////////////////////////////////////
  702. CGlobals::CGlobals()
  703. {
  704. g_pIMalloc = NULL; // OLE2 task memory allocator
  705. g_pIDataConvert = NULL; // IDataConvert pointer
  706. g_pCError = NULL;
  707. g_pErrClassFact = NULL;
  708. g_bIsAnsiOS = FALSE;
  709. g_pIWbemPathParser = FALSE; // class factory pointer for Parser object
  710. g_pIWbemCtxClassFac = NULL;
  711. }
  712. /////////////////////////////////////////////////////////////////////////////
  713. // Destructor
  714. /////////////////////////////////////////////////////////////////////////////
  715. CGlobals::~CGlobals()
  716. {
  717. //==================================================================
  718. // Clean up the global error handling
  719. //==================================================================
  720. ReleaseGlobalErrorHandling();
  721. //==============================================================
  722. // Clean up when the last process is going away
  723. //==============================================================
  724. m_CsGlobalError.Delete();
  725. //==============================================================
  726. // Delete internal error handler
  727. //==============================================================
  728. SAFE_DELETE_PTR( g_pCError )
  729. SAFE_RELEASE_PTR(g_pIDataConvert);
  730. if(g_pIWbemPathParser)
  731. {
  732. g_pIWbemPathParser->LockServer(FALSE);
  733. }
  734. SAFE_RELEASE_PTR(g_pIWbemPathParser);
  735. SAFE_RELEASE_PTR(g_pIWbemCtxClassFac);
  736. //==========================================================
  737. // Release the memory allocator object.
  738. //==========================================================
  739. SAFE_RELEASE_PTR(g_pIMalloc)
  740. }
  741. /////////////////////////////////////////////////////////////////////////////
  742. // Initialize global variables
  743. /////////////////////////////////////////////////////////////////////////////
  744. HRESULT CGlobals::Init()
  745. {
  746. HRESULT hr = S_OK;
  747. SYSTEM_INFO SystemInformation;
  748. if(!m_bInitialized)
  749. {
  750. s_strDllName.LoadStr(IDS_WMIOLEDBDLLNAME);
  751. //==================================================================
  752. // Initialize the global error handling
  753. //==================================================================
  754. VerifyGlobalErrorHandling();
  755. m_CsGlobalError.Init();
  756. //==============================================================
  757. // Get the OLE task memory allocator; we'll use it to allocate
  758. // all memory that we return to the client
  759. //==============================================================
  760. hr = CoGetMalloc( MEMCTX_TASK, &g_pIMalloc );
  761. if (!g_pIMalloc || !SUCCEEDED( hr )){
  762. LogMessage("CoGetMalloc failed in CGlobals::Init");
  763. }
  764. else
  765. {
  766. //==============================================================
  767. // Get the system page size
  768. //==============================================================
  769. if (!g_dwPageSize)
  770. {
  771. GetSystemInfo( &SystemInformation );
  772. g_dwPageSize = SystemInformation.dwPageSize;
  773. }
  774. //========================================================
  775. // Determine if we are a unicode or non-unicode OS
  776. //========================================================
  777. g_bIsAnsiOS = !OnUnicodeSystem();
  778. m_bInitialized = TRUE;
  779. }
  780. }
  781. return hr;
  782. }