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.

800 lines
23 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1998-2000 Microsoft Corporation
  4. //
  5. // MAINDLL.CPP
  6. //
  7. // alanbos 13-Feb-98 Created.
  8. //
  9. // Contains DLL entry points.
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include "objsink.h"
  14. #include "initguid.h"
  15. // SWbemLocator registry strings
  16. #define WBEMS_LOC_DESCRIPTION _T("WBEM Scripting Locator")
  17. #define WBEMS_LOC_PROGID _T("WbemScripting.SWbemLocator")
  18. #define WBEMS_LOC_PROGIDVER _T("WbemScripting.SWbemLocator.1")
  19. #define WBEMS_LOC_VERSION _T("1.0")
  20. #define WBEMS_LOC_VERDESC _T("WBEM Scripting Locator 1.0")
  21. // SWbemNamedValueSet registry strings
  22. #define WBEMS_CON_DESCRIPTION _T("WBEM Scripting Named Value Collection")
  23. #define WBEMS_CON_PROGID _T("WbemScripting.SWbemNamedValueSet")
  24. #define WBEMS_CON_PROGIDVER _T("WbemScripting.SWbemNamedValueSet.1")
  25. #define WBEMS_CON_VERSION _T("1.0")
  26. #define WBEMS_CON_VERDESC _T("WBEM Scripting Named Value Collection 1.0")
  27. // SWbemObjectPath registry settings
  28. #define WBEMS_OBP_DESCRIPTION _T("WBEM Scripting Object Path")
  29. #define WBEMS_OBP_PROGID _T("WbemScripting.SWbemObjectPath")
  30. #define WBEMS_OBP_PROGIDVER _T("WbemScripting.SWbemObjectPath.1")
  31. #define WBEMS_OBP_VERSION _T("1.0")
  32. #define WBEMS_OBP_VERDESC _T("WBEM Scripting Object Path 1.0")
  33. // SWbemParseDN registry settings
  34. #define WBEMS_PDN_DESCRIPTION _T("Wbem Scripting Object Path")
  35. #define WBEMS_PDN_PROGID _T("WINMGMTS")
  36. #define WBEMS_PDN_PROGIDVER _T("WINMGMTS.1")
  37. #define WBEMS_PDN_VERSION _T("1.0")
  38. #define WBEMS_PDN_VERDESC _T("Wbem Object Path 1.0")
  39. // SWbemLastError registry settings
  40. #define WBEMS_LER_DESCRIPTION _T("Wbem Scripting Last Error")
  41. #define WBEMS_LER_PROGID _T("WbemScripting.SWbemLastError")
  42. #define WBEMS_LER_PROGIDVER _T("WbemScripting.SWbemLastError.1")
  43. #define WBEMS_LER_VERSION _T("1.0")
  44. #define WBEMS_LER_VERDESC _T("Wbem Last Error 1.0")
  45. // SWbemSink registry strings
  46. #define WBEMS_SINK_DESCRIPTION _T("WBEM Scripting Sink")
  47. #define WBEMS_SINK_PROGID _T("WbemScripting.SWbemSink")
  48. #define WBEMS_SINK_PROGIDVER _T("WbemScripting.SWbemSink.1")
  49. #define WBEMS_SINK_VERSION _T("1.0")
  50. #define WBEMS_SINK_VERDESC _T("WBEM Scripting Sink 1.0")
  51. // SWbemDateTime registry settings
  52. #define WBEMS_DTIME_DESCRIPTION _T("WBEM Scripting DateTime")
  53. #define WBEMS_DTIME_PROGID _T("WbemScripting.SWbemDateTime")
  54. #define WBEMS_DTIME_PROGIDVER _T("WbemScripting.SWbemDateTime.1")
  55. #define WBEMS_DTIME_VERSION _T("1.0")
  56. #define WBEMS_DTIME_VERDESC _T("WBEM Scripting DateTime 1.0")
  57. // SWbemRefresher registry settings
  58. #define WBEMS_REF_DESCRIPTION _T("WBEM Scripting Refresher")
  59. #define WBEMS_REF_PROGID _T("WbemScripting.SWbemRefresher")
  60. #define WBEMS_REF_PROGIDVER _T("WbemScripting.SWbemRefresher.1")
  61. #define WBEMS_REF_VERSION _T("1.0")
  62. #define WBEMS_REF_VERDESC _T("WBEM Scripting Refresher 1.0")
  63. // Standard registry key/value names
  64. #define WBEMS_RK_SCC _T("SOFTWARE\\CLASSES\\CLSID\\")
  65. #define WBEMS_RK_SC _T("SOFTWARE\\CLASSES\\")
  66. #define WBEMS_RK_THRDMODEL _T("ThreadingModel")
  67. #define WBEMS_RV_APARTMENT _T("Apartment")
  68. #define WBEMS_RK_PROGID _T("ProgID")
  69. #define WBEMS_RK_VERPROGID _T("VersionIndependentProgID")
  70. #define WBEMS_RK_TYPELIB _T("TypeLib")
  71. #define WBEMS_RK_VERSION _T("Version")
  72. #define WBEMS_RK_INPROC32 _T("InProcServer32")
  73. #define WBEMS_RK_CLSID _T("CLSID")
  74. #define WBEMS_RK_CURVER _T("CurVer")
  75. #define WBEMS_RK_PROGRAMMABLE _T("Programmable")
  76. // Other values
  77. #define WBEMS_RK_WBEM _T("Software\\Microsoft\\Wbem")
  78. #define WBEMS_SK_SCRIPTING _T("Scripting")
  79. #define GUIDSIZE 128
  80. // Count number of objects and number of locks.
  81. long g_cObj = 0 ;
  82. ULONG g_cLock = 0 ;
  83. HMODULE ghModule = NULL;
  84. // Used for error object storage
  85. CWbemErrorCache *g_pErrorCache = NULL;
  86. /*
  87. * This object is used to protect the global pointer:
  88. *
  89. * - g_pErrorCache
  90. *
  91. * Note that it is the pointer variables that are protected by
  92. * this CS, rather than the addressed objects.
  93. */
  94. CRITICAL_SECTION g_csErrorCache;
  95. // Used to protect security calls
  96. CRITICAL_SECTION g_csSecurity;
  97. // CLSID for our implementation of IParseDisplayName
  98. // {172BDDF8-CEEA-11d1-8B05-00600806D9B6}
  99. DEFINE_GUID(CLSID_SWbemParseDN,
  100. 0x172bddf8, 0xceea, 0x11d1, 0x8b, 0x5, 0x0, 0x60, 0x8, 0x6, 0xd9, 0xb6);
  101. // Forward defs
  102. static void UnregisterTypeLibrary (unsigned short wVerMajor, unsigned short wVerMinor);
  103. //***************************************************************************
  104. //
  105. // BOOL WINAPI DllMain
  106. //
  107. // DESCRIPTION:
  108. //
  109. // Entry point for DLL. Good place for initialization.
  110. //
  111. // PARAMETERS:
  112. //
  113. // hInstance instance handle
  114. // ulReason why we are being called
  115. // pvReserved reserved
  116. //
  117. // RETURN VALUE:
  118. //
  119. // TRUE if OK.
  120. //
  121. //***************************************************************************
  122. BOOL WINAPI DllMain (
  123. IN HINSTANCE hInstance,
  124. IN ULONG ulReason,
  125. LPVOID pvReserved
  126. )
  127. {
  128. _RD(static char *me = "DllMain";)
  129. switch (ulReason)
  130. {
  131. case DLL_PROCESS_DETACH:
  132. {
  133. _RPrint(me, "DLL_PROCESS_DETACH", 0, "");
  134. DeleteCriticalSection (&g_csErrorCache);
  135. DeleteCriticalSection (&g_csSecurity);
  136. CSWbemLocator::Shutdown ();
  137. CIWbemObjectSinkMethodCache::TidyUp ();
  138. }
  139. return TRUE;
  140. case DLL_THREAD_DETACH:
  141. {
  142. _RPrint(me, "DLL_THREAD_DETACH", 0, "");
  143. }
  144. return TRUE;
  145. case DLL_PROCESS_ATTACH:
  146. {
  147. _RPrint(me, "DLL_PROCESS_DETACH", 0, "");
  148. if(ghModule == NULL)
  149. ghModule = hInstance;
  150. InitializeCriticalSection (&g_csErrorCache);
  151. InitializeCriticalSection (&g_csSecurity);
  152. CIWbemObjectSinkMethodCache::Initialize ();
  153. }
  154. return TRUE;
  155. case DLL_THREAD_ATTACH:
  156. {
  157. _RPrint(me, "DLL_THREAD_ATTACH", 0, "");
  158. }
  159. return TRUE;
  160. }
  161. return TRUE;
  162. }
  163. //***************************************************************************
  164. //
  165. // STDAPI DllGetClassObject
  166. //
  167. // DESCRIPTION:
  168. //
  169. // Called when Ole wants a class factory. Return one only if it is the sort
  170. // of class this DLL supports.
  171. //
  172. // PARAMETERS:
  173. //
  174. // rclsid CLSID of the object that is desired.
  175. // riid ID of the desired interface.
  176. // ppv Set to the class factory.
  177. //
  178. // RETURN VALUE:
  179. //
  180. // S_OK all is well
  181. // E_FAILED not something we support
  182. //
  183. //***************************************************************************
  184. STDAPI DllGetClassObject(
  185. IN REFCLSID rclsid,
  186. IN REFIID riid,
  187. OUT LPVOID *ppv
  188. )
  189. {
  190. HRESULT hr;
  191. CSWbemFactory *pObj = NULL;
  192. if (CLSID_SWbemLocator == rclsid)
  193. pObj=new CSWbemFactory(CSWbemFactory::LOCATOR);
  194. else if (CLSID_SWbemSink == rclsid)
  195. pObj=new CSWbemFactory(CSWbemFactory::SINK);
  196. else if (CLSID_SWbemNamedValueSet == rclsid)
  197. pObj=new CSWbemFactory(CSWbemFactory::CONTEXT);
  198. else if (CLSID_SWbemObjectPath == rclsid)
  199. pObj=new CSWbemFactory(CSWbemFactory::OBJECTPATH);
  200. else if (CLSID_SWbemParseDN == rclsid)
  201. pObj = new CSWbemFactory(CSWbemFactory::PARSEDN);
  202. else if (CLSID_SWbemLastError == rclsid)
  203. pObj = new CSWbemFactory(CSWbemFactory::LASTERROR);
  204. else if (CLSID_SWbemDateTime == rclsid)
  205. pObj = new CSWbemFactory(CSWbemFactory::DATETIME);
  206. else if (CLSID_SWbemRefresher == rclsid)
  207. pObj = new CSWbemFactory(CSWbemFactory::REFRESHER);
  208. if(NULL == pObj)
  209. return E_FAIL;
  210. hr=pObj->QueryInterface(riid, ppv);
  211. if (FAILED(hr))
  212. delete pObj;
  213. return hr ;
  214. }
  215. //***************************************************************************
  216. //
  217. // STDAPI DllCanUnloadNow
  218. //
  219. // DESCRIPTION:
  220. //
  221. // Answers if the DLL can be freed, that is, if there are no
  222. // references to anything this DLL provides.
  223. //
  224. // RETURN VALUE:
  225. //
  226. // S_OK if it is OK to unload
  227. // S_FALSE if still in use
  228. //
  229. //***************************************************************************
  230. STDAPI DllCanUnloadNow ()
  231. {
  232. // It is OK to unload if there are no objects or locks on the
  233. // class factory.
  234. HRESULT status = S_FALSE;
  235. _RD(static char *me = "DllCanUnloadNow";)
  236. _RPrint(me, "Called", 0, "");
  237. if (0L==g_cObj && 0L==g_cLock)
  238. {
  239. _RPrint(me, "Unloading", 0, "");
  240. /*
  241. * Release the error object on this thread, if any
  242. */
  243. status = S_OK;
  244. EnterCriticalSection (&g_csErrorCache);
  245. if (g_pErrorCache)
  246. {
  247. delete g_pErrorCache;
  248. g_pErrorCache = NULL;
  249. }
  250. LeaveCriticalSection (&g_csErrorCache);
  251. CSWbemSecurity::Uninitialize ();
  252. }
  253. return status;
  254. }
  255. //***************************************************************************
  256. //
  257. // STDAPI RegisterProgID
  258. // STDAPI RegisterCoClass
  259. // STDAPI RegisterTypeLibrary
  260. // STDAPI RegisterDefaultNamespace
  261. //
  262. // DESCRIPTION:
  263. //
  264. // Helpers for the tiresome business of registry setup
  265. //
  266. // RETURN VALUE:
  267. //
  268. // ERROR alas
  269. // NOERROR rejoice
  270. //
  271. //***************************************************************************
  272. STDAPI RegisterProgID (LPCTSTR wcID, LPCTSTR desc, LPCTSTR progid,
  273. LPCTSTR descVer, LPCTSTR progidVer)
  274. {
  275. HKEY hKey1 = NULL;
  276. HKEY hKey2 = NULL;
  277. TCHAR *szProgID = new TCHAR [_tcslen (WBEMS_RK_SC) +
  278. _tcslen (progid) + 1];
  279. if (!szProgID)
  280. return E_OUTOFMEMORY;
  281. TCHAR *szProgIDVer = new TCHAR [_tcslen (WBEMS_RK_SC) + _tcslen (progidVer) + 1];
  282. if (!szProgIDVer)
  283. {
  284. delete [] szProgID;
  285. return E_OUTOFMEMORY;
  286. }
  287. _tcscpy (szProgID, WBEMS_RK_SC);
  288. _tcscat (szProgID, progid);
  289. _tcscpy (szProgIDVer, WBEMS_RK_SC);
  290. _tcscat (szProgIDVer, progidVer);
  291. // Add the ProgID (Version independent)
  292. if(ERROR_SUCCESS == RegCreateKey(HKEY_LOCAL_MACHINE, szProgID, &hKey1))
  293. {
  294. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)desc, (_tcslen(desc)+1) * sizeof(TCHAR));
  295. if(ERROR_SUCCESS == RegCreateKey(hKey1,WBEMS_RK_CLSID, &hKey2))
  296. {
  297. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)wcID,
  298. (_tcslen(wcID)+1) * sizeof(TCHAR));
  299. RegCloseKey(hKey2);
  300. hKey2 = NULL;
  301. }
  302. if(ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_CURVER, &hKey2))
  303. {
  304. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)progidVer,
  305. (_tcslen(progidVer)+1) * sizeof(TCHAR));
  306. RegCloseKey(hKey2);
  307. hKey2 = NULL;
  308. }
  309. RegCloseKey(hKey1);
  310. }
  311. // Add the ProgID (Versioned)
  312. if(ERROR_SUCCESS == RegCreateKey(HKEY_LOCAL_MACHINE, szProgIDVer, &hKey1))
  313. {
  314. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)descVer, (_tcslen(descVer)+1) * sizeof(TCHAR));
  315. if(ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_CLSID, &hKey2))
  316. {
  317. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)wcID,
  318. (_tcslen(wcID)+1) * sizeof(TCHAR));
  319. RegCloseKey(hKey2);
  320. hKey2 = NULL;
  321. }
  322. RegCloseKey(hKey1);
  323. }
  324. delete [] szProgID;
  325. delete [] szProgIDVer;
  326. return NOERROR;
  327. }
  328. STDAPI RegisterCoClass (REFGUID clsid, LPCTSTR desc, LPCTSTR progid, LPCTSTR progidVer,
  329. LPCTSTR ver, LPCTSTR descVer)
  330. {
  331. HRESULT hr = S_OK;
  332. OLECHAR wcID[GUIDSIZE];
  333. OLECHAR tlID[GUIDSIZE];
  334. TCHAR nwcID[GUIDSIZE];
  335. TCHAR ntlID[GUIDSIZE];
  336. TCHAR szModule[MAX_PATH];
  337. HKEY hKey1 = NULL, hKey2 = NULL;
  338. TCHAR *szCLSID = new TCHAR [_tcslen (WBEMS_RK_SCC) + GUIDSIZE + 1];
  339. if (!szCLSID)
  340. return E_OUTOFMEMORY;
  341. // Create the path.
  342. if(0 ==StringFromGUID2(clsid, wcID, GUIDSIZE))
  343. {
  344. delete [] szCLSID;
  345. return ERROR;
  346. }
  347. _tcscpy (szCLSID, WBEMS_RK_SCC);
  348. #ifndef UNICODE
  349. wcstombs(nwcID, wcID, GUIDSIZE);
  350. #else
  351. _tcscpy (nwcID, wcID);
  352. #endif
  353. _tcscat (szCLSID, nwcID);
  354. if (0 == StringFromGUID2 (LIBID_WbemScripting, tlID, GUIDSIZE))
  355. {
  356. delete [] szCLSID;
  357. return ERROR;
  358. }
  359. #ifndef UNICODE
  360. wcstombs (ntlID, tlID, GUIDSIZE);
  361. #else
  362. _tcscpy (ntlID, tlID);
  363. #endif
  364. if(0 == GetModuleFileName(ghModule, szModule, MAX_PATH))
  365. {
  366. delete [] szCLSID;
  367. return ERROR;
  368. }
  369. // Create entries under CLSID
  370. if(ERROR_SUCCESS == RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1))
  371. {
  372. // Description (on main key)
  373. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)desc, (_tcslen(desc)+1) * sizeof(TCHAR));
  374. // Register as inproc server
  375. if (ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_INPROC32 ,&hKey2))
  376. {
  377. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  378. (_tcslen(szModule)+1) * sizeof(TCHAR));
  379. RegSetValueEx(hKey2, WBEMS_RK_THRDMODEL, 0, REG_SZ, (BYTE *)WBEMS_RV_APARTMENT,
  380. (_tcslen(WBEMS_RV_APARTMENT)+1) * sizeof(TCHAR));
  381. RegCloseKey(hKey2);
  382. }
  383. // Give a link to the type library (useful for statement completion in scripting tools)
  384. if (ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_TYPELIB, &hKey2))
  385. {
  386. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)ntlID, (_tcslen(ntlID)+1) * sizeof(TCHAR));
  387. RegCloseKey(hKey2);
  388. }
  389. // Register the ProgID
  390. if (ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_PROGID ,&hKey2))
  391. {
  392. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)progidVer,
  393. (_tcslen(progidVer)+1) * sizeof(TCHAR));
  394. RegCloseKey(hKey2);
  395. }
  396. // Register the version-independent ProgID
  397. if (ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_VERPROGID, &hKey2))
  398. {
  399. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)progid,
  400. (_tcslen(progid)+1) * sizeof(TCHAR));
  401. RegCloseKey(hKey2);
  402. }
  403. // Register the version
  404. if (ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_VERSION, &hKey2))
  405. {
  406. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)ver, (_tcslen(ver)+1) * sizeof(TCHAR));
  407. RegCloseKey(hKey2);
  408. }
  409. // Register this control as programmable
  410. if (ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_PROGRAMMABLE ,&hKey2))
  411. {
  412. RegCloseKey(hKey2);
  413. }
  414. RegCloseKey(hKey1);
  415. }
  416. else
  417. {
  418. delete [] szCLSID;
  419. return ERROR;
  420. }
  421. delete [] szCLSID;
  422. return RegisterProgID (nwcID, desc, progid, descVer, progidVer);
  423. }
  424. STDAPI RegisterTypeLibrary ()
  425. {
  426. // AUTOMATION. register type library
  427. TCHAR cPath[MAX_PATH+1];
  428. cPath[MAX_PATH] = 0;
  429. if(GetModuleFileName(ghModule,cPath,MAX_PATH))
  430. {
  431. // Replace final 3 characters "DLL" by "TLB"
  432. TCHAR *pExt = _tcsrchr (cPath, _T('.'));
  433. if (pExt && (0 == _tcsicmp (pExt, _T(".DLL"))))
  434. {
  435. _tcscpy (pExt + 1, _T("TLB"));
  436. OLECHAR wPath [MAX_PATH];
  437. #ifndef UNICODE
  438. mbstowcs (wPath, cPath, MAX_PATH-1);
  439. #else
  440. _tcsncpy (wPath, cPath, MAX_PATH-1);
  441. #endif
  442. ITypeLib FAR* ptlib = NULL;
  443. SCODE sc = LoadTypeLib(wPath, &ptlib);
  444. if(sc == 0 && ptlib)
  445. {
  446. sc = RegisterTypeLib(ptlib,wPath,NULL);
  447. ptlib->Release();
  448. // Unregister the previous library version(s)
  449. UnregisterTypeLibrary (1, 1);
  450. UnregisterTypeLibrary (1, 0);
  451. }
  452. }
  453. }
  454. return NOERROR;
  455. }
  456. STDAPI RegisterScriptSettings ()
  457. {
  458. HKEY hKey;
  459. if(ERROR_SUCCESS != RegCreateKey(HKEY_LOCAL_MACHINE, WBEMS_RK_SCRIPTING, &hKey))
  460. return ERROR;
  461. // Need to know what O/S we are to set up the right registry keys
  462. OSVERSIONINFO osVersionInfo;
  463. osVersionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
  464. GetVersionEx (&osVersionInfo);
  465. bool bIsNT = (VER_PLATFORM_WIN32_NT == osVersionInfo.dwPlatformId);
  466. DWORD dwNTMajorVersion = osVersionInfo.dwMajorVersion;
  467. // Default namespace value - exists on all platforms
  468. RegSetValueEx(hKey, WBEMS_RV_DEFNS, 0, REG_SZ, (BYTE *)WBEMS_DEFNS,
  469. (_tcslen(WBEMS_DEFNS)+1) * sizeof(TCHAR));
  470. // Enable for ASP - on NT 4.0 or less only
  471. if (bIsNT && (dwNTMajorVersion <= 4))
  472. {
  473. DWORD defaultEnableForAsp = 0;
  474. RegSetValueEx(hKey, WBEMS_RV_ENABLEFORASP, 0, REG_DWORD, (BYTE *)&defaultEnableForAsp,
  475. sizeof (defaultEnableForAsp));
  476. }
  477. // Default impersonation level - NT only
  478. if (bIsNT)
  479. {
  480. DWORD defaultImpersonationLevel = (DWORD) wbemImpersonationLevelImpersonate;
  481. RegSetValueEx(hKey, WBEMS_RV_DEFAULTIMPLEVEL, 0, REG_DWORD, (BYTE *)&defaultImpersonationLevel,
  482. sizeof (defaultImpersonationLevel));
  483. }
  484. RegCloseKey(hKey);
  485. return NOERROR;
  486. }
  487. //***************************************************************************
  488. //
  489. // DllRegisterServer
  490. //
  491. // Purpose: Called during setup or by regsvr32.
  492. //
  493. // Return: NOERROR if registration successful, error otherwise.
  494. //***************************************************************************
  495. STDAPI DllRegisterServer(void)
  496. {
  497. HRESULT hr;
  498. if (
  499. (NOERROR == (hr = RegisterScriptSettings ())) &&
  500. (NOERROR == (hr = RegisterCoClass (CLSID_SWbemLocator, WBEMS_LOC_DESCRIPTION,
  501. WBEMS_LOC_PROGID, WBEMS_LOC_PROGIDVER, WBEMS_LOC_VERSION,
  502. WBEMS_LOC_VERDESC))) &&
  503. (NOERROR == (hr = RegisterCoClass (CLSID_SWbemSink,
  504. WBEMS_SINK_DESCRIPTION, WBEMS_SINK_PROGID, WBEMS_SINK_PROGIDVER,
  505. WBEMS_SINK_VERSION, WBEMS_SINK_VERDESC))) &&
  506. (NOERROR == (hr = RegisterCoClass (CLSID_SWbemNamedValueSet,
  507. WBEMS_CON_DESCRIPTION, WBEMS_CON_PROGID, WBEMS_CON_PROGIDVER,
  508. WBEMS_CON_VERSION, WBEMS_CON_VERDESC))) &&
  509. (NOERROR == (hr = RegisterCoClass (CLSID_SWbemParseDN,
  510. WBEMS_PDN_DESCRIPTION, WBEMS_PDN_PROGID, WBEMS_PDN_PROGIDVER,
  511. WBEMS_PDN_VERSION, WBEMS_PDN_VERDESC))) &&
  512. (NOERROR == (hr = RegisterCoClass (CLSID_SWbemObjectPath,
  513. WBEMS_OBP_DESCRIPTION, WBEMS_OBP_PROGID, WBEMS_OBP_PROGIDVER,
  514. WBEMS_OBP_VERSION, WBEMS_OBP_VERDESC))) &&
  515. (NOERROR == (hr = RegisterCoClass (CLSID_SWbemLastError,
  516. WBEMS_LER_DESCRIPTION, WBEMS_LER_PROGID, WBEMS_LER_PROGIDVER,
  517. WBEMS_LER_VERSION, WBEMS_LER_VERDESC))) &&
  518. (NOERROR == (hr = RegisterCoClass (CLSID_SWbemDateTime,
  519. WBEMS_DTIME_DESCRIPTION, WBEMS_DTIME_PROGID, WBEMS_DTIME_PROGIDVER,
  520. WBEMS_DTIME_VERSION, WBEMS_DTIME_VERDESC))) &&
  521. (NOERROR == (hr = RegisterCoClass (CLSID_SWbemRefresher,
  522. WBEMS_REF_DESCRIPTION, WBEMS_REF_PROGID, WBEMS_REF_PROGIDVER,
  523. WBEMS_REF_VERSION, WBEMS_REF_VERDESC)))
  524. )
  525. hr = RegisterTypeLibrary ();
  526. return hr;
  527. }
  528. //***************************************************************************
  529. //
  530. // STDAPI UnregisterProgID
  531. // STDAPI UnregisterCoClass
  532. // STDAPI UnregisterTypeLibrary
  533. // STDAPI UnregisterDefaultNamespace
  534. //
  535. // DESCRIPTION:
  536. //
  537. // Helpers for the tiresome business of registry cleanup
  538. //
  539. // RETURN VALUE:
  540. //
  541. // ERROR alas
  542. // NOERROR rejoice
  543. //
  544. //***************************************************************************
  545. void UnregisterProgID (LPCTSTR progid, LPCTSTR progidVer)
  546. {
  547. HKEY hKey = NULL;
  548. TCHAR *szProgID = new TCHAR [_tcslen (WBEMS_RK_SC) + _tcslen (progid) + 1];
  549. TCHAR *szProgIDVer = new TCHAR [_tcslen (WBEMS_RK_SC) + _tcslen (progidVer) + 1];
  550. if (szProgID && szProgIDVer)
  551. {
  552. _tcscpy (szProgID, WBEMS_RK_SC);
  553. _tcscat (szProgID, progid);
  554. _tcscpy (szProgIDVer, WBEMS_RK_SC);
  555. _tcscat (szProgIDVer, progidVer);
  556. // Delete the subkeys of the versioned HKCR\ProgID entry
  557. if (NO_ERROR == RegOpenKey(HKEY_LOCAL_MACHINE, szProgIDVer, &hKey))
  558. {
  559. RegDeleteKey(hKey, WBEMS_RK_CLSID);
  560. RegCloseKey(hKey);
  561. }
  562. // Delete the versioned HKCR\ProgID entry
  563. RegDeleteKey (HKEY_LOCAL_MACHINE, szProgIDVer);
  564. // Delete the subkeys of the HKCR\VersionIndependentProgID entry
  565. if (NO_ERROR == RegOpenKey(HKEY_LOCAL_MACHINE, szProgID, &hKey))
  566. {
  567. RegDeleteKey(hKey, WBEMS_RK_CLSID);
  568. DWORD dwRet = RegDeleteKey(hKey, WBEMS_RK_CURVER);
  569. RegCloseKey(hKey);
  570. }
  571. // Delete the HKCR\VersionIndependentProgID entry
  572. RegDeleteKey (HKEY_LOCAL_MACHINE, szProgID);
  573. }
  574. if (szProgID)
  575. delete [] szProgID;
  576. if (szProgIDVer)
  577. delete [] szProgIDVer;
  578. }
  579. void UnregisterCoClass (REFGUID clsid, LPCTSTR progid, LPCTSTR progidVer)
  580. {
  581. OLECHAR wcID[GUIDSIZE];
  582. TCHAR nwcID[GUIDSIZE];
  583. HKEY hKey = NULL;
  584. TCHAR *szCLSID = new TCHAR [_tcslen (WBEMS_RK_SCC) + GUIDSIZE + 1];
  585. if (szCLSID)
  586. {
  587. // Create the path using the CLSID
  588. if(0 != StringFromGUID2(clsid, wcID, GUIDSIZE))
  589. {
  590. #ifndef UNICODE
  591. wcstombs(nwcID, wcID, GUIDSIZE);
  592. #else
  593. _tcscpy (nwcID, wcID);
  594. #endif
  595. _tcscpy (szCLSID, WBEMS_RK_SCC);
  596. _tcscat (szCLSID, nwcID);
  597. // First delete the subkeys of the HKLM\Software\Classes\CLSID\{GUID} entry
  598. if(NO_ERROR == RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey))
  599. {
  600. RegDeleteKey(hKey, WBEMS_RK_INPROC32);
  601. RegDeleteKey(hKey, WBEMS_RK_TYPELIB);
  602. RegDeleteKey(hKey, WBEMS_RK_PROGID);
  603. RegDeleteKey(hKey, WBEMS_RK_VERPROGID);
  604. RegDeleteKey(hKey, WBEMS_RK_VERSION);
  605. RegDeleteKey(hKey, WBEMS_RK_PROGRAMMABLE);
  606. RegCloseKey(hKey);
  607. }
  608. // Delete the HKLM\Software\Classes\CLSID\{GUID} key
  609. if(NO_ERROR == RegOpenKey(HKEY_LOCAL_MACHINE, WBEMS_RK_SCC, &hKey))
  610. {
  611. RegDeleteKey(hKey, nwcID);
  612. RegCloseKey(hKey);
  613. }
  614. }
  615. delete [] szCLSID;
  616. }
  617. UnregisterProgID (progid, progidVer);
  618. }
  619. static void UnregisterTypeLibrary (unsigned short wVerMajor, unsigned short wVerMinor)
  620. {
  621. // Unregister the type library. The UnRegTypeLib function is not available in
  622. // in some of the older version of the ole dlls and so it must be loaded
  623. // dynamically
  624. HRESULT (STDAPICALLTYPE *pfnUnReg)(REFGUID, WORD,
  625. WORD , LCID , SYSKIND);
  626. TCHAR path[ MAX_PATH+20 ];
  627. GetSystemDirectory(path, MAX_PATH);
  628. _tcscat(path, _T("\\oleaut32.dll"));
  629. HMODULE g_hOle32 = LoadLibraryEx(path, NULL, 0);
  630. if(g_hOle32 != NULL)
  631. {
  632. (FARPROC&)pfnUnReg = GetProcAddress(g_hOle32, "UnRegisterTypeLib");
  633. if(pfnUnReg)
  634. pfnUnReg (LIBID_WbemScripting, wVerMajor, wVerMinor, 0, SYS_WIN32);
  635. FreeLibrary(g_hOle32);
  636. }
  637. }
  638. void UnregisterScriptSettings ()
  639. {
  640. HKEY hKey;
  641. if(NO_ERROR == RegOpenKey(HKEY_LOCAL_MACHINE, WBEMS_RK_WBEM, &hKey))
  642. {
  643. RegDeleteKey(hKey, WBEMS_SK_SCRIPTING);
  644. RegCloseKey (hKey);
  645. }
  646. }
  647. //***************************************************************************
  648. //
  649. // DllUnregisterServer
  650. //
  651. // Purpose: Called when it is time to remove the registry entries.
  652. //
  653. // Return: NOERROR if registration successful, error otherwise.
  654. //***************************************************************************
  655. STDAPI DllUnregisterServer(void)
  656. {
  657. UnregisterScriptSettings ();
  658. UnregisterCoClass (CLSID_SWbemLocator, WBEMS_LOC_PROGID, WBEMS_LOC_PROGIDVER);
  659. UnregisterCoClass (CLSID_SWbemSink, WBEMS_SINK_PROGID, WBEMS_SINK_PROGIDVER);
  660. UnregisterCoClass (CLSID_SWbemNamedValueSet, WBEMS_CON_PROGID, WBEMS_CON_PROGIDVER);
  661. UnregisterCoClass (CLSID_SWbemLastError, WBEMS_LER_PROGID, WBEMS_LER_PROGIDVER);
  662. UnregisterCoClass (CLSID_SWbemObjectPath, WBEMS_OBP_PROGID, WBEMS_OBP_PROGIDVER);
  663. UnregisterCoClass (CLSID_SWbemParseDN, WBEMS_PDN_PROGID, WBEMS_PDN_PROGIDVER);
  664. UnregisterCoClass (CLSID_SWbemDateTime, WBEMS_DTIME_PROGID, WBEMS_DTIME_PROGIDVER);
  665. UnregisterCoClass (CLSID_SWbemRefresher, WBEMS_REF_PROGID, WBEMS_REF_PROGIDVER);
  666. UnregisterTypeLibrary (1, 2);
  667. return NOERROR;
  668. }