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.

506 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. DeleteCriticalSection(&g_CriticalSection);
  66. }
  67. else if ( DLL_PROCESS_ATTACH == ulReason )
  68. {
  69. g_hInst=hInstance;
  70. InitializeCriticalSection(&g_CriticalSection);
  71. DisableThreadLibraryCalls(hInstance);
  72. CViewProvServ::sm_debugLog = ProvDebugLog::GetProvDebugLog(LOG_VIEWPROV);
  73. }
  74. else if ( DLL_THREAD_DETACH == ulReason )
  75. {
  76. }
  77. else if ( DLL_THREAD_ATTACH == ulReason )
  78. {
  79. }
  80. return status;
  81. }
  82. //***************************************************************************
  83. //
  84. // DllGetClassObject
  85. //
  86. // Purpose: Called by Ole when some client wants a a class factory. Return
  87. // one only if it is the sort of class this DLL supports.
  88. //
  89. //***************************************************************************
  90. STDAPI DllGetClassObject (
  91. REFCLSID rclsid ,
  92. REFIID riid,
  93. void **ppv
  94. )
  95. {
  96. HRESULT status = S_OK ;
  97. SetStructuredExceptionHandler seh;
  98. BOOL bEnteredCritSec = FALSE;
  99. BOOL bAllocate = FALSE;
  100. BOOL bDebugLog = FALSE;
  101. BOOL bClear = FALSE;
  102. try
  103. {
  104. EnterCriticalSection(&g_CriticalSection);
  105. bEnteredCritSec = TRUE;
  106. if ( rclsid == CLSID_CViewProviderClassFactory )
  107. {
  108. CViewProvClassFactory *lpunk = new CViewProvClassFactory;
  109. if ( lpunk == NULL )
  110. {
  111. status = E_OUTOFMEMORY ;
  112. }
  113. else
  114. {
  115. status = lpunk->QueryInterface ( riid , ppv ) ;
  116. if ( FAILED ( status ) )
  117. {
  118. delete lpunk ;
  119. }
  120. }
  121. }
  122. else
  123. {
  124. status = CLASS_E_CLASSNOTAVAILABLE ;
  125. }
  126. LeaveCriticalSection(&g_CriticalSection);
  127. bEnteredCritSec = FALSE;
  128. }
  129. catch(Structured_Exception e_SE)
  130. {
  131. bClear = TRUE;
  132. status = E_UNEXPECTED;
  133. }
  134. catch(Heap_Exception e_HE)
  135. {
  136. bClear = TRUE;
  137. status = E_OUTOFMEMORY;
  138. }
  139. catch(...)
  140. {
  141. bClear = TRUE;
  142. status = E_UNEXPECTED;
  143. }
  144. if ( bEnteredCritSec )
  145. {
  146. LeaveCriticalSection(&g_CriticalSection);
  147. }
  148. return status ;
  149. }
  150. //***************************************************************************
  151. //
  152. // DllCanUnloadNow
  153. //
  154. // Purpose: Called periodically by Ole in order to determine if the
  155. // DLL can be freed.//
  156. // Return: TRUE if there are no objects in use and the class factory
  157. // isn't locked.
  158. //***************************************************************************
  159. STDAPI DllCanUnloadNow ()
  160. {
  161. /*
  162. * Place code in critical section
  163. */
  164. BOOL unload = FALSE;
  165. SetStructuredExceptionHandler seh;
  166. BOOL bEnteredCritSec = FALSE;
  167. try
  168. {
  169. EnterCriticalSection(&g_CriticalSection);
  170. bEnteredCritSec = TRUE;
  171. unload = (0 == CViewProvClassFactory :: locksInProgress)
  172. && (0 == CViewProvClassFactory :: objectsInProgress);
  173. if (unload)
  174. {
  175. if (NULL != CViewProvServ::sm_UnsecApp)
  176. {
  177. CViewProvServ::sm_UnsecApp->Release();
  178. CViewProvServ::sm_UnsecApp = NULL;
  179. }
  180. }
  181. LeaveCriticalSection(&g_CriticalSection);
  182. bEnteredCritSec = FALSE;
  183. }
  184. catch(Structured_Exception e_SE)
  185. {
  186. unload = FALSE;
  187. }
  188. catch(Heap_Exception e_HE)
  189. {
  190. unload = FALSE;
  191. }
  192. catch(...)
  193. {
  194. unload = FALSE;
  195. }
  196. if ( bEnteredCritSec )
  197. {
  198. LeaveCriticalSection(&g_CriticalSection);
  199. }
  200. return unload ? ResultFromScode ( S_OK ) : ResultFromScode ( S_FALSE ) ;
  201. }
  202. //Strings used during self registeration
  203. #define REG_FORMAT2_STR _T("%s%s")
  204. #define REG_FORMAT3_STR _T("%s%s\\%s")
  205. #define VER_IND_STR _T("VersionIndependentProgID")
  206. #define NOT_INTERT_STR _T("NotInsertable")
  207. #define INPROC32_STR _T("InprocServer32")
  208. #define PROGID_STR _T("ProgID")
  209. #define THREADING_MODULE_STR _T("ThreadingModel")
  210. #define APARTMENT_STR _T("Both")
  211. #define CLSID_STR _T("CLSID\\")
  212. #define PROVIDER_NAME_STR _T("Microsoft WBEM View Provider")
  213. #define PROVIDER_STR _T("WBEM.VIEW.PROVIDER")
  214. #define PROVIDER_CVER_STR _T("WBEM.VIEW.PROVIDER\\CurVer")
  215. #define PROVIDER_CLSID_STR _T("WBEM.VIEW.PROVIDER\\CLSID")
  216. #define PROVIDER_VER_CLSID_STR _T("WBEM.VIEW.PROVIDER.0\\CLSID")
  217. #define PROVIDER_VER_STR _T("WBEM.VIEW.PROVIDER.0")
  218. /***************************************************************************
  219. * SetKeyAndValue
  220. *
  221. * Purpose:
  222. * Private helper function for DllRegisterServer that creates
  223. * a key, sets a value, and closes that key.
  224. *
  225. * Parameters:
  226. * pszKey LPTSTR to the ame of the key
  227. * pszSubkey LPTSTR ro the name of a subkey
  228. * pszValue LPTSTR to the value to store
  229. *
  230. * Return Value:
  231. * BOOL TRUE if successful, FALSE otherwise.
  232. ***************************************************************************/
  233. BOOL SetKeyAndValue(TCHAR* pszKey, TCHAR* pszSubkey, TCHAR* pszValueName, TCHAR* pszValue)
  234. {
  235. HKEY hKey;
  236. TCHAR szKey[256];
  237. _tcscpy(szKey, HKEYCLASSES);
  238. _tcscat(szKey, pszKey);
  239. if (NULL!=pszSubkey)
  240. {
  241. _tcscat(szKey, _T("\\"));
  242. _tcscat(szKey, pszSubkey);
  243. }
  244. if (ERROR_SUCCESS!=RegCreateKeyEx(HKEY_LOCAL_MACHINE
  245. , szKey, 0, NULL, REG_OPTION_NON_VOLATILE
  246. , KEY_ALL_ACCESS, NULL, &hKey, NULL))
  247. return FALSE;
  248. if (NULL!=pszValue)
  249. {
  250. if (ERROR_SUCCESS != RegSetValueEx(hKey, pszValueName, 0, REG_SZ, (BYTE *)pszValue
  251. , (_tcslen(pszValue)+1)*sizeof(TCHAR)))
  252. return FALSE;
  253. }
  254. RegCloseKey(hKey);
  255. return TRUE;
  256. }
  257. /***************************************************************************
  258. * DllRegisterServer
  259. *
  260. * Purpose:
  261. * Instructs the server to create its own registry entries
  262. *
  263. * Parameters:
  264. * None
  265. *
  266. * Return Value:
  267. * HRESULT NOERROR if registration successful, error
  268. * otherwise.
  269. ***************************************************************************/
  270. STDAPI DllRegisterServer()
  271. {
  272. SetStructuredExceptionHandler seh;
  273. try
  274. {
  275. TCHAR szModule[MAX_PATH + 1];
  276. GetModuleFileName(g_hInst,(TCHAR*)szModule, MAX_PATH + 1);
  277. TCHAR szProviderClassID[128];
  278. TCHAR szProviderCLSIDClassID[128];
  279. #ifndef UNICODE
  280. wchar_t t_strGUID[128];
  281. if (0 == StringFromGUID2(CLSID_CViewProviderClassFactory, t_strGUID, 128))
  282. {
  283. return SELFREG_E_CLASS;
  284. }
  285. if (0 == WideCharToMultiByte(CP_ACP,
  286. 0,
  287. t_strGUID,
  288. -1,
  289. szProviderClassID,
  290. 128,
  291. NULL,
  292. NULL))
  293. {
  294. return SELFREG_E_CLASS;
  295. }
  296. #else
  297. if (0 == StringFromGUID2(CLSID_CViewProviderClassFactory, szProviderClassID, 128))
  298. {
  299. return SELFREG_E_CLASS;
  300. }
  301. #endif
  302. _tcscpy(szProviderCLSIDClassID,CLSID_STR);
  303. _tcscat(szProviderCLSIDClassID,szProviderClassID);
  304. //Create entries under CLSID
  305. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NULL, NULL, PROVIDER_NAME_STR))
  306. return SELFREG_E_CLASS;
  307. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, PROGID_STR, NULL, PROVIDER_VER_STR))
  308. return SELFREG_E_CLASS;
  309. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, VER_IND_STR, NULL, PROVIDER_STR))
  310. return SELFREG_E_CLASS;
  311. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NOT_INTERT_STR, NULL, NULL))
  312. return SELFREG_E_CLASS;
  313. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR, NULL,szModule))
  314. return SELFREG_E_CLASS;
  315. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  316. return SELFREG_E_CLASS;
  317. }
  318. catch(Structured_Exception e_SE)
  319. {
  320. return E_UNEXPECTED;
  321. }
  322. catch(Heap_Exception e_HE)
  323. {
  324. return E_OUTOFMEMORY;
  325. }
  326. catch(...)
  327. {
  328. return E_UNEXPECTED;
  329. }
  330. return S_OK;
  331. }
  332. /***************************************************************************
  333. * DllUnregisterServer
  334. *
  335. * Purpose:
  336. * Instructs the server to remove its own registry entries
  337. *
  338. * Parameters:
  339. * None
  340. *
  341. * Return Value:
  342. * HRESULT NOERROR if registration successful, error
  343. * otherwise.
  344. ***************************************************************************/
  345. STDAPI DllUnregisterServer(void)
  346. {
  347. HRESULT hr = S_OK;
  348. SetStructuredExceptionHandler seh;
  349. try
  350. {
  351. TCHAR szTemp[128];
  352. TCHAR szProviderClassID[128];
  353. TCHAR szProviderCLSIDClassID[128];
  354. #ifndef UNICODE
  355. wchar_t t_strGUID[128];
  356. if (0 == StringFromGUID2(CLSID_CViewProviderClassFactory, t_strGUID, 128))
  357. {
  358. return SELFREG_E_CLASS;
  359. }
  360. if (0 == WideCharToMultiByte(CP_ACP,
  361. 0,
  362. t_strGUID,
  363. -1,
  364. szProviderClassID,
  365. 128,
  366. NULL,
  367. NULL))
  368. {
  369. return SELFREG_E_CLASS;
  370. }
  371. #else
  372. if (0 == StringFromGUID2(CLSID_CViewProviderClassFactory, szProviderClassID, 128))
  373. {
  374. return SELFREG_E_CLASS;
  375. }
  376. #endif
  377. _tcscpy(szProviderCLSIDClassID,CLSID_STR);
  378. _tcscat(szProviderCLSIDClassID,szProviderClassID);
  379. //Delete entries under CLSID
  380. _stprintf(szTemp, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, PROGID_STR);
  381. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  382. {
  383. hr = SELFREG_E_CLASS;
  384. }
  385. _stprintf(szTemp, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, VER_IND_STR);
  386. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  387. {
  388. hr = SELFREG_E_CLASS;
  389. }
  390. _stprintf(szTemp, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, NOT_INTERT_STR);
  391. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  392. {
  393. hr = SELFREG_E_CLASS;
  394. }
  395. _stprintf(szTemp, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, INPROC32_STR);
  396. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  397. {
  398. hr = SELFREG_E_CLASS;
  399. }
  400. _stprintf(szTemp, REG_FORMAT2_STR, HKEYCLASSES, szProviderCLSIDClassID);
  401. if (ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp))
  402. {
  403. hr = SELFREG_E_CLASS;
  404. }
  405. }
  406. catch(Structured_Exception e_SE)
  407. {
  408. hr = E_UNEXPECTED;
  409. }
  410. catch(Heap_Exception e_HE)
  411. {
  412. hr = E_OUTOFMEMORY;
  413. }
  414. catch(...)
  415. {
  416. hr = E_UNEXPECTED;
  417. }
  418. return hr;
  419. }