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.

819 lines
24 KiB

  1. // This is a part of the Microsoft Management Console.
  2. // Copyright (C) 1995-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Management Console and related
  7. // electronic documentation provided with the interfaces.
  8. // You will need the NT SUR Beta 2 SDK or VC 4.2 in order to build this
  9. // project. This is because you will need MIDL 3.00.15 or higher and new
  10. // headers and libs. If you have VC 4.2 installed, then everything should
  11. // already be configured correctly.
  12. // Note: Proxy/Stub Information
  13. // To build a separate proxy/stub DLL,
  14. // run nmake -f Snapinps.mak in the project directory.
  15. #include "stdafx.h"
  16. #include "resource.h"
  17. #include "initguid.h"
  18. #include "cookie.h"
  19. #include <scesvc.h>
  20. #include "Snapmgr.h"
  21. #include "wrapper.h"
  22. #include "sceattch.h"
  23. #include "about.h"
  24. CComModule _Module;
  25. BEGIN_OBJECT_MAP(ObjectMap)
  26. OBJECT_ENTRY(CLSID_Snapin, CComponentDataExtensionImpl)
  27. OBJECT_ENTRY(CLSID_SCESnapin, CComponentDataSCEImpl)
  28. OBJECT_ENTRY(CLSID_SAVSnapin, CComponentDataSAVImpl)
  29. OBJECT_ENTRY(CLSID_LSSnapin, CComponentDataLSImpl)
  30. OBJECT_ENTRY(CLSID_RSOPSnapin, CComponentDataRSOPImpl)
  31. OBJECT_ENTRY(CLSID_SCEAbout, CSCEAbout)
  32. OBJECT_ENTRY(CLSID_SCMAbout, CSCMAbout)
  33. OBJECT_ENTRY(CLSID_SSAbout, CSSAbout)
  34. OBJECT_ENTRY(CLSID_LSAbout, CLSAbout)
  35. OBJECT_ENTRY(CLSID_RSOPAbout, CRSOPAbout)
  36. END_OBJECT_MAP()
  37. #ifdef _DEBUG
  38. #define new DEBUG_NEW
  39. #undef THIS_FILE
  40. static char THIS_FILE[] = __FILE__;
  41. #endif
  42. HRESULT RegisterSnapin(const GUID* pSnapinCLSID,
  43. const GUID* pStaticNodeGUID,
  44. const GUID* pSnapinAboutGUID,
  45. const int nNameResource,
  46. // LPCTSTR lpszNameStringNoValueName,
  47. LPCTSTR lpszVersion,
  48. BOOL bExtension);
  49. HRESULT RegisterSnapin(LPCTSTR lpszSnapinClassID,
  50. LPCTSTR lpszStaticNodeGuid,
  51. LPCTSTR lpszSnapingAboutGuid,
  52. const int nNameResource,
  53. LPCTSTR lpszVersion,
  54. BOOL bExtension);
  55. HRESULT UnregisterSnapin(const GUID* pSnapinCLSID);
  56. HRESULT UnregisterSnapin(LPCTSTR lpszSnapinClassID);
  57. HRESULT RegisterNodeType(const GUID* pGuid, LPCTSTR lpszNodeDescription);
  58. HRESULT RegisterNodeType(LPCTSTR lpszNodeGuid, LPCTSTR lpszNodeDescription);
  59. HRESULT RegisterNodeType(LPCTSTR lpszNodeType, const GUID* pGuid, LPCTSTR lpszNodeDescription);
  60. HRESULT RegisterNodeType(LPCTSTR lpszNodeType, LPCTSTR lpszNodeGuid, LPCTSTR lpszNodeDescription);
  61. HRESULT UnregisterNodeType(const GUID* pGuid);
  62. HRESULT UnregisterNodeType(LPCTSTR lpszNodeGuid);
  63. HRESULT UnregisterNodeType(LPCTSTR lpszNodeType, const GUID* pGuid);
  64. HRESULT UnregisterNodeType(LPCTSTR lpszNodeType, LPCTSTR lpszNodeGuid);
  65. HRESULT RegisterDefaultTemplate(LPCTSTR lpszTemplateDir);
  66. HRESULT RegisterEnvVarsToExpand();
  67. class CSnapinApp : public CWinApp
  68. {
  69. public:
  70. virtual BOOL InitInstance();
  71. virtual int ExitInstance();
  72. private:
  73. BOOL m_bCriticalSet;
  74. };
  75. CSnapinApp theApp;
  76. const int iStrGuidLen = 128;
  77. BOOL CSnapinApp::InitInstance()
  78. {
  79. _Module.Init(ObjectMap, m_hInstance);
  80. if (!CComponentDataImpl::LoadResources())
  81. return FALSE;
  82. //This is not a safe usage. consider using InitializeCriticalSectionAndSpinCount. Raid #555887, yanggao.
  83. m_bCriticalSet = FALSE;
  84. try
  85. {
  86. InitializeCriticalSection(&csOpenDatabase);
  87. m_bCriticalSet = TRUE;
  88. }
  89. catch(...)
  90. {
  91. return FALSE;
  92. }
  93. SHFusionInitializeFromModuleID (m_hInstance, 2);
  94. return CWinApp::InitInstance();
  95. }
  96. int CSnapinApp::ExitInstance()
  97. {
  98. SHFusionUninitialize();
  99. if( m_bCriticalSet ) //Raid #555887, yanggao, 4/5/2002.
  100. DeleteCriticalSection(&csOpenDatabase); //Raid #379167, 4/27/2001
  101. _Module.Term();
  102. DEBUG_VERIFY_INSTANCE_COUNT(CSnapin);
  103. DEBUG_VERIFY_INSTANCE_COUNT(CComponentDataImpl);
  104. return CWinApp::ExitInstance();
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. // Used to determine whether the DLL can be unloaded by OLE
  108. STDAPI DllCanUnloadNow(void)
  109. {
  110. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  111. return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  112. }
  113. /////////////////////////////////////////////////////////////////////////////
  114. // Returns a class factory to create an object of the requested type
  115. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  116. {
  117. return _Module.GetClassObject(rclsid, riid, ppv);
  118. }
  119. STDAPI DllRegisterServer(void)
  120. {
  121. // registers object, but not typelib and all interfaces in typelib
  122. HRESULT hr = _Module.RegisterServer(FALSE);
  123. ASSERT(SUCCEEDED(hr));
  124. if (FAILED(hr))
  125. return hr;
  126. CString str;
  127. AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
  128. //
  129. // unregister some nodes then re-register
  130. // because some are changed here
  131. //
  132. hr = UnregisterSnapin(&CLSID_Snapin);
  133. // not an extension of computer management
  134. hr = UnregisterNodeType(TEXT(struuidNodetypeSystemTools), &CLSID_Snapin);
  135. //
  136. // register the snapin into the console snapin list as SCE
  137. hr = RegisterSnapin(&CLSID_SCESnapin, &cSCENodeType, &CLSID_SCEAbout,
  138. IDS_TEMPLATE_EDITOR_NAME, _T("1.0"), FALSE);
  139. ASSERT(SUCCEEDED(hr));
  140. if (FAILED(hr))
  141. return hr;
  142. //
  143. // register the snapin into the console snapin list as SAV
  144. hr = RegisterSnapin(&CLSID_SAVSnapin, &cSAVNodeType, &CLSID_SCMAbout,
  145. IDS_ANALYSIS_VIEWER_NAME, _T("1.0"), FALSE);
  146. ASSERT(SUCCEEDED(hr));
  147. if (FAILED(hr))
  148. return hr;
  149. hr = RegisterSnapin(&CLSID_Snapin, &cNodeType, &CLSID_SSAbout,
  150. IDS_EXTENSION_NAME, _T("1.0"), TRUE );
  151. ASSERT(SUCCEEDED(hr));
  152. if (FAILED(hr))
  153. return hr;
  154. hr = RegisterSnapin(&CLSID_RSOPSnapin, &cRSOPNodeType, &CLSID_RSOPAbout,
  155. IDS_EXTENSION_NAME, _T("1.0"), TRUE );
  156. ASSERT(SUCCEEDED(hr));
  157. if (FAILED(hr))
  158. return hr;
  159. #ifdef USE_SEPARATE_LOCALSEC
  160. hr = RegisterSnapin(&CLSID_LSSnapin, &cLSNodeType, &CLSID_LSAbout,
  161. IDS_LOCAL_SECURITY_NAME, _T("1.0"), FALSE );
  162. ASSERT(SUCCEEDED(hr));
  163. if (FAILED(hr))
  164. return hr;
  165. #endif
  166. // no need to register as extension of computer management snapin
  167. // str.LoadString(IDS_ANALYSIS_VIEWER_NAME);
  168. // hr = RegisterNodeType(TEXT(struuidNodetypeSystemTools), &CLSID_Snapin, (LPCTSTR)str);
  169. //
  170. // register GPE extension
  171. // register the snapin as an extension of GPT's Machine node
  172. //
  173. OLECHAR szGuid[iStrGuidLen];
  174. if (0 != ::StringFromGUID2(NODEID_Machine,szGuid,iStrGuidLen))
  175. {
  176. str.LoadString(IDS_EXTENSION_NAME);
  177. hr = RegisterNodeType(szGuid, &CLSID_Snapin, (LPCTSTR)str);
  178. if (FAILED(hr))
  179. return hr;
  180. }
  181. // register the snapin as an extension of GPT's User node
  182. if (0 != ::StringFromGUID2(NODEID_User,szGuid,iStrGuidLen))
  183. {
  184. hr = RegisterNodeType(szGuid, &CLSID_Snapin, (LPCTSTR)str);
  185. if (FAILED(hr))
  186. return hr;
  187. }
  188. if (0 != ::StringFromGUID2(NODEID_RSOPMachine,szGuid,iStrGuidLen))
  189. {
  190. str.LoadString(IDS_EXTENSION_NAME);
  191. hr = RegisterNodeType(szGuid, &CLSID_RSOPSnapin, (LPCTSTR)str);
  192. if (FAILED(hr))
  193. return hr;
  194. }
  195. // register the snapin as an extension of GPT's User node
  196. if (0 != ::StringFromGUID2(NODEID_RSOPUser,szGuid,iStrGuidLen))
  197. {
  198. hr = RegisterNodeType(szGuid, &CLSID_RSOPSnapin, (LPCTSTR)str);
  199. if (FAILED(hr))
  200. return hr;
  201. }
  202. //
  203. // register the default template path
  204. //
  205. CString str2;
  206. LPTSTR sz;
  207. sz = str.GetBuffer(MAX_PATH+1); //Raid #533113, yanggao
  208. if ( 0 == GetWindowsDirectory(sz,MAX_PATH) ) sz[0] = L'\0';
  209. str.ReleaseBuffer();
  210. str2.LoadString(IDS_DEFAULT_TEMPLATE_DIR);
  211. str += str2;
  212. sz=str.GetBuffer(str.GetLength());
  213. // Can't put '\' in the registry, so convert to '/'
  214. while(sz = wcschr(sz,L'\\')) {
  215. *sz = L'/';
  216. }
  217. str.ReleaseBuffer();
  218. str2.LoadString(IDS_TEMPLATE_LOCATION_KEY);
  219. str2 += L"\\";
  220. str2 += str;
  221. hr = RegisterDefaultTemplate(str2);
  222. if (FAILED(hr)) {
  223. return hr;
  224. }
  225. hr = RegisterEnvVarsToExpand();
  226. return hr;
  227. }
  228. /////////////////////////////////////////////////////////////////////////////
  229. // DllUnregisterServer - Removes entries from the system registry
  230. STDAPI DllUnregisterServer(void)
  231. {
  232. OLECHAR szGuid[iStrGuidLen];
  233. HRESULT hr = _Module.UnregisterServer();
  234. ASSERT(SUCCEEDED(hr));
  235. if (FAILED(hr))
  236. return hr;
  237. // unregister the snapin extension nodes
  238. // un register the snapin
  239. hr = UnregisterSnapin(&CLSID_SCESnapin);
  240. // un register the snapin
  241. hr = UnregisterSnapin(&CLSID_SAVSnapin);
  242. // un register the snapin
  243. hr = UnregisterSnapin(&CLSID_Snapin);
  244. // un register the snapin
  245. hr = UnregisterSnapin(&CLSID_LSSnapin);
  246. // un register the snapin
  247. hr = UnregisterSnapin(&CLSID_RSOPSnapin);
  248. // unregister the SCE snapin nodes
  249. hr = UnregisterNodeType(lstruuidNodetypeSceTemplateServices);
  250. // unregister the SAV snapin nodes
  251. hr = UnregisterNodeType(lstruuidNodetypeSceAnalysisServices);
  252. // unregister the snapin nodes
  253. hr = UnregisterNodeType(lstruuidNodetypeSceTemplate);
  254. // not an extension of computer management
  255. // hr = UnregisterNodeType(TEXT(struuidNodetypeSystemTools), &CLSID_Snapin);
  256. ::StringFromGUID2(NODEID_Machine,szGuid,iStrGuidLen);
  257. hr = UnregisterNodeType(szGuid, &CLSID_Snapin);
  258. ::StringFromGUID2(NODEID_User,szGuid,iStrGuidLen);
  259. hr = UnregisterNodeType(szGuid, &CLSID_Snapin);
  260. ::StringFromGUID2(NODEID_RSOPMachine,szGuid,iStrGuidLen);
  261. hr = UnregisterNodeType(szGuid, &CLSID_RSOPSnapin);
  262. ::StringFromGUID2(NODEID_RSOPUser,szGuid,iStrGuidLen);
  263. hr = UnregisterNodeType(szGuid, &CLSID_RSOPSnapin);
  264. /*
  265. /*
  266. // unregister the SCE snapin nodes
  267. hr = UnregisterNodeType(&cSCENodeType);
  268. ASSERT(SUCCEEDED(hr));
  269. // unregister the SAV snapin nodes
  270. hr = UnregisterNodeType(&cSAVNodeType);
  271. ASSERT(SUCCEEDED(hr));
  272. // unregister the snapin nodes
  273. hr = UnregisterNodeType(&cNodeType);
  274. ASSERT(SUCCEEDED(hr));
  275. */
  276. return S_OK;
  277. }
  278. HRESULT RegisterSnapin(const GUID* pSnapinCLSID, const GUID* pStaticNodeGUID,
  279. const GUID* pSnapinAboutGUID,
  280. const int nNameResource,
  281. // LPCTSTR lpszNameString,
  282. LPCTSTR lpszVersion, BOOL bExtension)
  283. {
  284. USES_CONVERSION;
  285. OLECHAR szSnapinClassID[iStrGuidLen], szStaticNodeGuid[iStrGuidLen], szSnapinAboutGuid[iStrGuidLen];
  286. if (0 != ::StringFromGUID2(*pSnapinCLSID, szSnapinClassID, iStrGuidLen) &&
  287. 0 != ::StringFromGUID2(*pStaticNodeGUID, szStaticNodeGuid, iStrGuidLen) &&
  288. 0 != ::StringFromGUID2(*pSnapinAboutGUID, szSnapinAboutGuid, iStrGuidLen) )
  289. {
  290. return RegisterSnapin(szSnapinClassID,
  291. szStaticNodeGuid,
  292. szSnapinAboutGuid,
  293. nNameResource,
  294. // lpszNameString,
  295. lpszVersion,
  296. bExtension);
  297. }
  298. else
  299. return E_OUTOFMEMORY;
  300. }
  301. ////////////////////////////////////////////////////////////////////////
  302. //
  303. // Registry SCE related reg keys under MMC snapins key
  304. //
  305. //
  306. ////////////////////////////////////////////////////////////////////////
  307. HRESULT
  308. RegisterSnapin(LPCTSTR lpszSnapinClassID,
  309. LPCTSTR lpszStaticNodeGuid,
  310. LPCTSTR lpszSnapinAboutGuid,
  311. const int nNameResource,
  312. LPCTSTR lpszVersion,
  313. BOOL bExtension)
  314. {
  315. //
  316. // open the MMC Snapins root key
  317. //
  318. CRegKey regkeySnapins;
  319. LONG lRes = regkeySnapins.Open(HKEY_LOCAL_MACHINE,
  320. SNAPINS_KEY);
  321. if (lRes != ERROR_SUCCESS) {
  322. return HRESULT_FROM_WIN32(lRes); // failed to open
  323. }
  324. //
  325. // create SCE subkey, if already exist, just open it
  326. //
  327. CRegKey regkeyThisSnapin;
  328. lRes = regkeyThisSnapin.Create(regkeySnapins,
  329. lpszSnapinClassID);
  330. if (lRes == ERROR_SUCCESS) {
  331. //
  332. // set values for SCE root key
  333. //
  334. //
  335. // 97068 MUI:MMC:Security:Security Configuration and Analysis Snap-in store its information in the registry
  336. // 99392 MUI:MMC:Security:Security Templates Snap-in store its information in the registry
  337. // 97167 MUI:GPE:GPE Extension: Group policy Secuirty snap-in ext name string is stored in the registry
  338. //
  339. // MMC now supports NameStringIndirect
  340. //
  341. TCHAR achModuleFileName[MAX_PATH+20];
  342. if (0 < ::GetModuleFileName(
  343. AfxGetInstanceHandle(),
  344. achModuleFileName,
  345. sizeof(achModuleFileName)/sizeof(TCHAR) ))
  346. {
  347. CString strNameIndirect;
  348. strNameIndirect.Format( _T("@%s,-%d"),
  349. achModuleFileName,
  350. nNameResource);
  351. lRes = regkeyThisSnapin.SetValue(strNameIndirect,
  352. TEXT("NameStringIndirect"));
  353. }
  354. lRes = regkeyThisSnapin.SetValue(lpszStaticNodeGuid,
  355. TEXT("NodeType"));
  356. lRes = regkeyThisSnapin.SetValue(lpszSnapinAboutGuid,
  357. TEXT("About"));
  358. lRes = regkeyThisSnapin.SetValue(_T("Microsoft"),
  359. _T("Provider"));
  360. lRes = regkeyThisSnapin.SetValue(lpszVersion,
  361. _T("Version"));
  362. //
  363. // create "NodeTypes" subkey
  364. //
  365. CRegKey regkeyNodeTypes;
  366. lRes = regkeyNodeTypes.Create(regkeyThisSnapin,
  367. TEXT("NodeTypes"));
  368. if (lRes == ERROR_SUCCESS) {
  369. //
  370. // create subkeys for all node types supported by SCE
  371. //
  372. // including: services under configuration,
  373. // services under analysis
  374. // GPT extensions
  375. //
  376. lRes = regkeyNodeTypes.SetKeyValue(lstruuidNodetypeSceTemplateServices,
  377. TEXT("SCE Service Template Extensions"));
  378. if (lRes == ERROR_SUCCESS) {
  379. lRes = regkeyNodeTypes.SetKeyValue(lstruuidNodetypeSceAnalysisServices,
  380. TEXT("SCE Service Inspection Extensions"));
  381. }
  382. if ( bExtension &&
  383. lRes == ERROR_SUCCESS ) {
  384. //
  385. // NOTE: standalone snapin do not support public key extensions
  386. //
  387. // node type for one template in SCE standalone mode,
  388. // or the root node of SCE under GPE
  389. //
  390. CString str;
  391. str.LoadString(IDS_EXTENSION_NAME);
  392. lRes = regkeyNodeTypes.SetKeyValue(lstruuidNodetypeSceTemplate,
  393. (LPCTSTR)str);
  394. if (lRes == ERROR_SUCCESS) {
  395. lRes = RegisterNodeType(lstruuidNodetypeSceTemplate,
  396. (LPCTSTR)str);
  397. }
  398. } else if (lRes == ERROR_SUCCESS) {
  399. //
  400. // create "Standalone" subkey
  401. //
  402. CRegKey regkeyStandalone;
  403. lRes = regkeyStandalone.Create(regkeyThisSnapin,
  404. TEXT("Standalone"));
  405. if ( lRes == ERROR_SUCCESS ) {
  406. regkeyStandalone.Close();
  407. }
  408. }
  409. //
  410. // register supported node types to MMC NodeTypes key
  411. // including all the above node types
  412. //
  413. if ( lRes == ERROR_SUCCESS ) {
  414. lRes = RegisterNodeType(lstruuidNodetypeSceTemplateServices,
  415. TEXT("SCE Service Template Extensions"));
  416. if (lRes == ERROR_SUCCESS) {
  417. lRes = RegisterNodeType(lstruuidNodetypeSceAnalysisServices,
  418. TEXT("SCE Service Analysis Extensions"));
  419. }
  420. }
  421. regkeyNodeTypes.Close();
  422. }
  423. regkeyThisSnapin.Close();
  424. }
  425. regkeySnapins.Close();
  426. return HRESULT_FROM_WIN32(lRes);
  427. }
  428. HRESULT UnregisterSnapin(const GUID* pSnapinCLSID)
  429. {
  430. USES_CONVERSION;
  431. OLECHAR szSnapinClassID[iStrGuidLen];
  432. if (0 != ::StringFromGUID2(*pSnapinCLSID,szSnapinClassID,iStrGuidLen))
  433. return UnregisterSnapin(szSnapinClassID);
  434. else
  435. return E_INVALIDARG;
  436. }
  437. HRESULT UnregisterSnapin(LPCTSTR lpszSnapinClassID)
  438. {
  439. //
  440. // open MMC Snapins key
  441. //
  442. CRegKey regkeySnapins;
  443. LONG lRes = regkeySnapins.Open(HKEY_LOCAL_MACHINE,
  444. SNAPINS_KEY);
  445. if (lRes != ERROR_SUCCESS)
  446. return HRESULT_FROM_WIN32(lRes); // failed to open
  447. //
  448. // delete SCE sub key (and all related subkeys under SCE)
  449. //
  450. lRes = regkeySnapins.RecurseDeleteKey(lpszSnapinClassID);
  451. regkeySnapins.Close();
  452. if ( lRes == ERROR_FILE_NOT_FOUND )
  453. return S_OK;
  454. return HRESULT_FROM_WIN32(lRes);
  455. }
  456. HRESULT RegisterNodeType(const GUID* pGuid, LPCTSTR lpszNodeDescription)
  457. {
  458. USES_CONVERSION;
  459. OLECHAR szGuid[iStrGuidLen];
  460. if (0 != ::StringFromGUID2(*pGuid,szGuid,iStrGuidLen))
  461. return RegisterNodeType(OLE2T(szGuid), lpszNodeDescription);
  462. else
  463. return E_INVALIDARG;
  464. }
  465. HRESULT RegisterNodeType(LPCTSTR lpszNodeGuid, LPCTSTR lpszNodeDescription)
  466. {
  467. CRegKey regkeyNodeTypes;
  468. LONG lRes = regkeyNodeTypes.Open(HKEY_LOCAL_MACHINE, NODE_TYPES_KEY);
  469. ASSERT(lRes == ERROR_SUCCESS);
  470. if (lRes != ERROR_SUCCESS)
  471. return HRESULT_FROM_WIN32(lRes); // failed to open
  472. CRegKey regkeyThisNodeType;
  473. lRes = regkeyThisNodeType.Create(regkeyNodeTypes, lpszNodeGuid);
  474. ASSERT(lRes == ERROR_SUCCESS);
  475. if (lRes == ERROR_SUCCESS) {
  476. lRes = regkeyThisNodeType.SetValue(lpszNodeDescription);
  477. regkeyThisNodeType.Close();
  478. }
  479. return HRESULT_FROM_WIN32(lRes);
  480. }
  481. HRESULT RegisterNodeType(LPCTSTR lpszNodeType, const GUID* pGuid, LPCTSTR lpszNodeDescription)
  482. {
  483. USES_CONVERSION;
  484. OLECHAR szGuid[iStrGuidLen];
  485. if (0 != ::StringFromGUID2(*pGuid,szGuid,iStrGuidLen))
  486. return RegisterNodeType(lpszNodeType, OLE2T(szGuid), lpszNodeDescription);
  487. else
  488. return E_INVALIDARG;
  489. }
  490. HRESULT RegisterNodeType(LPCTSTR lpszNodeType, LPCTSTR lpszNodeGuid, LPCTSTR lpszNodeDescription)
  491. {
  492. CRegKey regkeyNodeTypes;
  493. LONG lRes = regkeyNodeTypes.Open(HKEY_LOCAL_MACHINE, NODE_TYPES_KEY);
  494. if (lRes == ERROR_SUCCESS) {
  495. CRegKey regkeyThisNodeType;
  496. lRes = regkeyThisNodeType.Create(regkeyNodeTypes, lpszNodeType );
  497. if (lRes == ERROR_SUCCESS) {
  498. CRegKey regkeyExtensions;
  499. lRes = regkeyExtensions.Create(regkeyThisNodeType, g_szExtensions);
  500. if ( lRes == ERROR_SUCCESS ) {
  501. CRegKey regkeyNameSpace;
  502. lRes = regkeyNameSpace.Create(regkeyExtensions, g_szNameSpace);
  503. if ( lRes == ERROR_SUCCESS ) {
  504. lRes = regkeyNameSpace.SetValue( lpszNodeDescription, lpszNodeGuid );
  505. regkeyNameSpace.Close();
  506. }
  507. regkeyExtensions.Close();
  508. }
  509. regkeyThisNodeType.Close();
  510. }
  511. regkeyNodeTypes.Close();
  512. }
  513. ASSERT(lRes == ERROR_SUCCESS);
  514. return HRESULT_FROM_WIN32(lRes);
  515. }
  516. HRESULT UnregisterNodeType(const GUID* pGuid)
  517. {
  518. USES_CONVERSION;
  519. OLECHAR szGuid[iStrGuidLen];
  520. if (0 != ::StringFromGUID2(*pGuid,szGuid,iStrGuidLen))
  521. return UnregisterNodeType(OLE2T(szGuid));
  522. else
  523. return E_INVALIDARG;
  524. }
  525. HRESULT UnregisterNodeType(LPCTSTR lpszNodeGuid)
  526. {
  527. CRegKey regkeyNodeTypes;
  528. LONG lRes = regkeyNodeTypes.Open(HKEY_LOCAL_MACHINE, NODE_TYPES_KEY);
  529. if (lRes != ERROR_SUCCESS)
  530. return HRESULT_FROM_WIN32(lRes); // failed to open
  531. lRes = regkeyNodeTypes.RecurseDeleteKey(lpszNodeGuid);
  532. regkeyNodeTypes.Close();
  533. if ( lRes == ERROR_FILE_NOT_FOUND )
  534. return S_OK;
  535. return HRESULT_FROM_WIN32(lRes);
  536. }
  537. HRESULT RegisterDefaultTemplate(LPCTSTR lpszTemplateDir)
  538. {
  539. CRegKey regkeyTemplates;
  540. CString strKey;
  541. LONG lRes;
  542. strKey.LoadString(IDS_TEMPLATE_LOCATION_KEY);
  543. /*
  544. lRes = regkeyTemplates.Open(HKEY_LOCAL_MACHINE, strKey);
  545. ASSERT(lRes == ERROR_SUCCESS);
  546. if (lRes != ERROR_SUCCESS)
  547. return HRESULT_FROM_WIN32(lRes); // failed to open
  548. */
  549. lRes = regkeyTemplates.Create(HKEY_LOCAL_MACHINE,lpszTemplateDir);
  550. ASSERT(lRes == ERROR_SUCCESS);
  551. return HRESULT_FROM_WIN32(lRes);
  552. }
  553. HRESULT UnregisterNodeType(LPCTSTR lpszNodeType, const GUID* pGuid)
  554. {
  555. USES_CONVERSION;
  556. OLECHAR szGuid[iStrGuidLen];
  557. if (0 != ::StringFromGUID2(*pGuid,szGuid,iStrGuidLen))
  558. return UnregisterNodeType(lpszNodeType, OLE2T(szGuid));
  559. else
  560. return E_INVALIDARG;
  561. }
  562. HRESULT UnregisterNodeType(LPCTSTR lpszNodeType, LPCTSTR lpszNodeGuid)
  563. {
  564. CRegKey regkeyNodeTypes;
  565. LONG lRes = regkeyNodeTypes.Open(HKEY_LOCAL_MACHINE, NODE_TYPES_KEY );
  566. if (lRes == ERROR_SUCCESS) {
  567. CRegKey regkeyThisNodeType;
  568. lRes = regkeyThisNodeType.Open(regkeyNodeTypes, lpszNodeType );
  569. if (lRes == ERROR_SUCCESS) {
  570. CRegKey regkeyExtensions;
  571. lRes = regkeyExtensions.Open(regkeyThisNodeType, g_szExtensions);
  572. if ( lRes == ERROR_SUCCESS ) {
  573. CRegKey regkeyNameSpace;
  574. lRes = regkeyNameSpace.Open(regkeyExtensions, g_szNameSpace);
  575. if ( lRes == ERROR_SUCCESS ) {
  576. lRes = regkeyNameSpace.DeleteValue( lpszNodeGuid );
  577. regkeyNameSpace.Close();
  578. }
  579. regkeyExtensions.Close();
  580. }
  581. regkeyThisNodeType.Close();
  582. }
  583. regkeyNodeTypes.Close();
  584. }
  585. if ( lRes == ERROR_FILE_NOT_FOUND ) {
  586. return S_OK;
  587. } else
  588. return HRESULT_FROM_WIN32(lRes);
  589. }
  590. HRESULT
  591. RegisterEnvVarsToExpand() {
  592. CString strKey;
  593. CString strValue;
  594. CString strEnvVars;
  595. TCHAR *pch;
  596. HRESULT hr;
  597. HKEY hKey;
  598. LONG status;
  599. if (!strKey.LoadString(IDS_SECEDIT_KEY)) {
  600. return E_FAIL;
  601. }
  602. if (!strValue.LoadString(IDS_ENV_VARS_REG_VALUE)) {
  603. return E_FAIL;
  604. }
  605. if (!strEnvVars.LoadString(IDS_DEF_ENV_VARS)) {
  606. return E_FAIL;
  607. }
  608. //
  609. // Convert strEnvVars' | to '\0' to be a proper multi-sz
  610. //
  611. for (int i = 0; i < strEnvVars.GetLength(); i++)
  612. {
  613. if (strEnvVars[i] == L'|')
  614. strEnvVars.SetAt(i, L'\0');
  615. }
  616. //
  617. // Open up the key we keep our Environment Variables in
  618. //
  619. status = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  620. strKey,
  621. 0, // Reserved
  622. NULL, // Class
  623. 0, // Options
  624. KEY_WRITE,
  625. NULL, // Security
  626. &hKey,
  627. NULL);
  628. if (ERROR_SUCCESS != status) {
  629. return HRESULT_FROM_WIN32(status);
  630. }
  631. int iLenth = strEnvVars.GetLength();
  632. BYTE* pbBufEnvVars = (BYTE*)(strEnvVars.GetBuffer(iLenth));
  633. status = RegSetValueEx(hKey,
  634. strValue,
  635. NULL,
  636. REG_MULTI_SZ,
  637. pbBufEnvVars,
  638. iLenth * sizeof(WCHAR));
  639. strEnvVars.ReleaseBuffer();
  640. if (ERROR_SUCCESS != status) {
  641. return HRESULT_FROM_WIN32(status);
  642. }
  643. return S_OK;
  644. }