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.

491 lines
11 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WBEM VIEW PROVIDER
  6. //
  7. // Purpose: Contains the global dll functions
  8. //
  9. // Copyright (c) 1998-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include <provtempl.h>
  14. #include <provmt.h>
  15. #include <typeinfo.h>
  16. #include <process.h>
  17. #include <objbase.h>
  18. #include <olectl.h>
  19. #include <dsgetdc.h>
  20. #include <lmcons.h>
  21. #include <initguid.h>
  22. #ifndef INITGUID
  23. #define INITGUID
  24. #endif
  25. #include <windows.h>
  26. #include <stdio.h>
  27. #include <provexpt.h>
  28. #include <tchar.h>
  29. #include <wbemidl.h>
  30. #include <provcoll.h>
  31. #include <provcont.h>
  32. #include <provevt.h>
  33. #include <provthrd.h>
  34. #include <provlog.h>
  35. #include <instpath.h>
  36. #include <genlex.h>
  37. #include <sql_1.h>
  38. #include <objpath.h>
  39. #include <vpdefs.h>
  40. #include <vpcfac.h>
  41. #include <vpquals.h>
  42. #include <vpserv.h>
  43. #include <vptasks.h>
  44. //OK we need these globals
  45. HINSTANCE g_hInst = NULL;
  46. ProvDebugLog* CViewProvServ::sm_debugLog = NULL;
  47. IUnsecuredApartment* CViewProvServ::sm_UnsecApp = NULL;
  48. CRITICAL_SECTION g_CriticalSection;
  49. //***************************************************************************
  50. //
  51. // LibMain32
  52. //
  53. // Purpose: Entry point for DLL. Good place for initialization.
  54. // Return: TRUE if OK.
  55. //***************************************************************************
  56. BOOL APIENTRY DllMain (
  57. HINSTANCE hInstance,
  58. ULONG ulReason ,
  59. LPVOID pvReserved
  60. )
  61. {
  62. BOOL status = TRUE ;
  63. if ( DLL_PROCESS_DETACH == ulReason )
  64. {
  65. //ProvThreadObject :: ProcessDetach (TRUE) ;
  66. DeleteCriticalSection(&g_CriticalSection);
  67. }
  68. else if ( DLL_PROCESS_ATTACH == ulReason )
  69. {
  70. g_hInst=hInstance;
  71. //ProvThreadObject :: ProcessAttach () ;
  72. InitializeCriticalSection(&g_CriticalSection);
  73. DisableThreadLibraryCalls(hInstance);
  74. }
  75. else if ( DLL_THREAD_DETACH == ulReason )
  76. {
  77. }
  78. else if ( DLL_THREAD_ATTACH == ulReason )
  79. {
  80. }
  81. return status;
  82. }
  83. //***************************************************************************
  84. //
  85. // DllGetClassObject
  86. //
  87. // Purpose: Called by Ole when some client wants a a class factory. Return
  88. // one only if it is the sort of class this DLL supports.
  89. //
  90. //***************************************************************************
  91. STDAPI DllGetClassObject (
  92. REFCLSID rclsid ,
  93. REFIID riid,
  94. void **ppv
  95. )
  96. {
  97. HRESULT status = S_OK ;
  98. SetStructuredExceptionHandler seh;
  99. try
  100. {
  101. EnterCriticalSection(&g_CriticalSection);
  102. if (CViewProvServ::sm_debugLog == NULL)
  103. {
  104. ProvDebugLog::Startup();
  105. CViewProvServ::sm_debugLog = new ProvDebugLog(_T("ViewProvider"));;
  106. }
  107. if ( rclsid == CLSID_CViewProviderClassFactory )
  108. {
  109. CViewProvClassFactory *lpunk = new CViewProvClassFactory;
  110. if ( lpunk == NULL )
  111. {
  112. status = E_OUTOFMEMORY ;
  113. }
  114. else
  115. {
  116. status = lpunk->QueryInterface ( riid , ppv ) ;
  117. if ( FAILED ( status ) )
  118. {
  119. delete lpunk ;
  120. }
  121. }
  122. }
  123. else
  124. {
  125. status = CLASS_E_CLASSNOTAVAILABLE ;
  126. }
  127. LeaveCriticalSection(&g_CriticalSection);
  128. }
  129. catch(Structured_Exception e_SE)
  130. {
  131. status = E_UNEXPECTED;
  132. }
  133. catch(Heap_Exception e_HE)
  134. {
  135. status = E_OUTOFMEMORY;
  136. }
  137. catch(...)
  138. {
  139. status = E_UNEXPECTED;
  140. }
  141. return status ;
  142. }
  143. //***************************************************************************
  144. //
  145. // DllCanUnloadNow
  146. //
  147. // Purpose: Called periodically by Ole in order to determine if the
  148. // DLL can be freed.//
  149. // Return: TRUE if there are no objects in use and the class factory
  150. // isn't locked.
  151. //***************************************************************************
  152. STDAPI DllCanUnloadNow ()
  153. {
  154. /*
  155. * Place code in critical section
  156. */
  157. BOOL unload = FALSE;
  158. SetStructuredExceptionHandler seh;
  159. try
  160. {
  161. EnterCriticalSection(&g_CriticalSection);
  162. unload = (0 == CViewProvClassFactory :: locksInProgress)
  163. && (0 == CViewProvClassFactory :: objectsInProgress);
  164. if (unload)
  165. {
  166. if (CViewProvServ::sm_debugLog != NULL)
  167. {
  168. delete CViewProvServ::sm_debugLog;
  169. CViewProvServ::sm_debugLog = NULL;
  170. ProvDebugLog::Closedown();
  171. }
  172. if (NULL != CViewProvServ::sm_UnsecApp)
  173. {
  174. CViewProvServ::sm_UnsecApp->Release();
  175. CViewProvServ::sm_UnsecApp = NULL;
  176. }
  177. }
  178. LeaveCriticalSection(&g_CriticalSection);
  179. }
  180. catch(Structured_Exception e_SE)
  181. {
  182. unload = FALSE;
  183. }
  184. catch(Heap_Exception e_HE)
  185. {
  186. unload = FALSE;
  187. }
  188. catch(...)
  189. {
  190. unload = FALSE;
  191. }
  192. return unload ? ResultFromScode ( S_OK ) : ResultFromScode ( S_FALSE ) ;
  193. }
  194. //Strings used during self registeration
  195. #define REG_FORMAT2_STR _T("%s%s")
  196. #define REG_FORMAT3_STR _T("%s%s\\%s")
  197. #define VER_IND_STR _T("VersionIndependentProgID")
  198. #define NOT_INTERT_STR _T("NotInsertable")
  199. #define INPROC32_STR _T("InprocServer32")
  200. #define PROGID_STR _T("ProgID")
  201. #define THREADING_MODULE_STR _T("ThreadingModel")
  202. #define APARTMENT_STR _T("Both")
  203. #define CLSID_STR _T("CLSID\\")
  204. #define PROVIDER_NAME_STR _T("Microsoft WBEM View Provider")
  205. #define PROVIDER_STR _T("WBEM.VIEW.PROVIDER")
  206. #define PROVIDER_CVER_STR _T("WBEM.VIEW.PROVIDER\\CurVer")
  207. #define PROVIDER_CLSID_STR _T("WBEM.VIEW.PROVIDER\\CLSID")
  208. #define PROVIDER_VER_CLSID_STR _T("WBEM.VIEW.PROVIDER.0\\CLSID")
  209. #define PROVIDER_VER_STR _T("WBEM.VIEW.PROVIDER.0")
  210. /***************************************************************************
  211. * SetKeyAndValue
  212. *
  213. * Purpose:
  214. * Private helper function for DllRegisterServer that creates
  215. * a key, sets a value, and closes that key.
  216. *
  217. * Parameters:
  218. * pszKey LPTSTR to the ame of the key
  219. * pszSubkey LPTSTR ro the name of a subkey
  220. * pszValue LPTSTR to the value to store
  221. *
  222. * Return Value:
  223. * BOOL TRUE if successful, FALSE otherwise.
  224. ***************************************************************************/
  225. BOOL SetKeyAndValue(TCHAR* pszKey, TCHAR* pszSubkey, TCHAR* pszValueName, TCHAR* pszValue)
  226. {
  227. HKEY hKey;
  228. TCHAR szKey[256];
  229. _tcscpy(szKey, HKEYCLASSES);
  230. _tcscat(szKey, pszKey);
  231. if (NULL!=pszSubkey)
  232. {
  233. _tcscat(szKey, _T("\\"));
  234. _tcscat(szKey, pszSubkey);
  235. }
  236. if (ERROR_SUCCESS!=RegCreateKeyEx(HKEY_LOCAL_MACHINE
  237. , szKey, 0, NULL, REG_OPTION_NON_VOLATILE
  238. , KEY_ALL_ACCESS, NULL, &hKey, NULL))
  239. return FALSE;
  240. if (NULL!=pszValue)
  241. {
  242. if (ERROR_SUCCESS != RegSetValueEx(hKey, pszValueName, 0, REG_SZ, (BYTE *)pszValue
  243. , (_tcslen(pszValue)+1)*sizeof(TCHAR)))
  244. return FALSE;
  245. }
  246. RegCloseKey(hKey);
  247. return TRUE;
  248. }
  249. /***************************************************************************
  250. * DllRegisterServer
  251. *
  252. * Purpose:
  253. * Instructs the server to create its own registry entries
  254. *
  255. * Parameters:
  256. * None
  257. *
  258. * Return Value:
  259. * HRESULT NOERROR if registration successful, error
  260. * otherwise.
  261. ***************************************************************************/
  262. STDAPI DllRegisterServer()
  263. {
  264. SetStructuredExceptionHandler seh;
  265. try
  266. {
  267. TCHAR szModule[MAX_PATH + 1];
  268. GetModuleFileName(g_hInst,(TCHAR*)szModule, MAX_PATH + 1);
  269. TCHAR szProviderClassID[128];
  270. TCHAR szProviderCLSIDClassID[128];
  271. #ifndef UNICODE
  272. wchar_t t_strGUID[128];
  273. if (0 == StringFromGUID2(CLSID_CViewProviderClassFactory, t_strGUID, 128))
  274. {
  275. return SELFREG_E_CLASS;
  276. }
  277. if (0 == WideCharToMultiByte(CP_ACP,
  278. 0,
  279. t_strGUID,
  280. -1,
  281. szProviderClassID,
  282. 128,
  283. NULL,
  284. NULL))
  285. {
  286. return SELFREG_E_CLASS;
  287. }
  288. #else
  289. if (0 == StringFromGUID2(CLSID_CViewProviderClassFactory, szProviderClassID, 128))
  290. {
  291. return SELFREG_E_CLASS;
  292. }
  293. #endif
  294. _tcscpy(szProviderCLSIDClassID,CLSID_STR);
  295. _tcscat(szProviderCLSIDClassID,szProviderClassID);
  296. //Create entries under CLSID
  297. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NULL, NULL, PROVIDER_NAME_STR))
  298. return SELFREG_E_CLASS;
  299. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, PROGID_STR, NULL, PROVIDER_VER_STR))
  300. return SELFREG_E_CLASS;
  301. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, VER_IND_STR, NULL, PROVIDER_STR))
  302. return SELFREG_E_CLASS;
  303. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NOT_INTERT_STR, NULL, NULL))
  304. return SELFREG_E_CLASS;
  305. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR, NULL,szModule))
  306. return SELFREG_E_CLASS;
  307. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  308. return SELFREG_E_CLASS;
  309. }
  310. catch(Structured_Exception e_SE)
  311. {
  312. return E_UNEXPECTED;
  313. }
  314. catch(Heap_Exception e_HE)
  315. {
  316. return E_OUTOFMEMORY;
  317. }
  318. catch(...)
  319. {
  320. return E_UNEXPECTED;
  321. }
  322. return S_OK;
  323. }
  324. /***************************************************************************
  325. * DllUnregisterServer
  326. *
  327. * Purpose:
  328. * Instructs the server to remove its own registry entries
  329. *
  330. * Parameters:
  331. * None
  332. *
  333. * Return Value:
  334. * HRESULT NOERROR if registration successful, error
  335. * otherwise.
  336. ***************************************************************************/
  337. STDAPI DllUnregisterServer(void)
  338. {
  339. HRESULT hr = S_OK;
  340. SetStructuredExceptionHandler seh;
  341. try
  342. {
  343. TCHAR szTemp[128];
  344. TCHAR szProviderClassID[128];
  345. TCHAR szProviderCLSIDClassID[128];
  346. #ifndef UNICODE
  347. wchar_t t_strGUID[128];
  348. if (0 == StringFromGUID2(CLSID_CViewProviderClassFactory, t_strGUID, 128))
  349. {
  350. return SELFREG_E_CLASS;
  351. }
  352. if (0 == WideCharToMultiByte(CP_ACP,
  353. 0,
  354. t_strGUID,
  355. -1,
  356. szProviderClassID,
  357. 128,
  358. NULL,
  359. NULL))
  360. {
  361. return SELFREG_E_CLASS;
  362. }
  363. #else
  364. if (0 == StringFromGUID2(CLSID_CViewProviderClassFactory, szProviderClassID, 128))
  365. {
  366. return SELFREG_E_CLASS;
  367. }
  368. #endif
  369. _tcscpy(szProviderCLSIDClassID,CLSID_STR);
  370. _tcscat(szProviderCLSIDClassID,szProviderClassID);
  371. //Delete entries under CLSID
  372. _stprintf(szTemp, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, PROGID_STR);
  373. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  374. {
  375. hr = SELFREG_E_CLASS;
  376. }
  377. _stprintf(szTemp, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, VER_IND_STR);
  378. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  379. {
  380. hr = SELFREG_E_CLASS;
  381. }
  382. _stprintf(szTemp, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, NOT_INTERT_STR);
  383. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  384. {
  385. hr = SELFREG_E_CLASS;
  386. }
  387. _stprintf(szTemp, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, INPROC32_STR);
  388. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  389. {
  390. hr = SELFREG_E_CLASS;
  391. }
  392. _stprintf(szTemp, REG_FORMAT2_STR, HKEYCLASSES, szProviderCLSIDClassID);
  393. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  394. {
  395. hr = SELFREG_E_CLASS;
  396. }
  397. }
  398. catch(Structured_Exception e_SE)
  399. {
  400. hr = E_UNEXPECTED;
  401. }
  402. catch(Heap_Exception e_HE)
  403. {
  404. hr = E_OUTOFMEMORY;
  405. }
  406. catch(...)
  407. {
  408. hr = E_UNEXPECTED;
  409. }
  410. return hr;
  411. }