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.

1185 lines
39 KiB

  1. /*++
  2. Copyright (C) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. smlogcfg.cpp
  5. Abstract:
  6. Implementation of DLL exports.
  7. --*/
  8. // Note: Proxy/Stub Information
  9. // To build a separate proxy/stub DLL,
  10. // run nmake -f Smlogcfgps.mk in the project directory.
  11. #include "StdAfx.h"
  12. #include <strsafe.h>
  13. #include "InitGuid.h"
  14. #include "compdata.h"
  15. #include "smabout.h"
  16. #include "smlogcfg.h" // For CLSID_ComponentData
  17. #include "Smlogcfg_i.c" // For CLSID_ComponentData
  18. #include <ntverp.h>
  19. #include <wbemidl.h>
  20. #include <Sddl.h>
  21. USE_HANDLE_MACROS("SMLOGCFG(smlogcfg.cpp)")
  22. CComModule _Module;
  23. BEGIN_OBJECT_MAP(ObjectMap)
  24. OBJECT_ENTRY(CLSID_ComponentData, CSmLogSnapin)
  25. OBJECT_ENTRY(CLSID_ExtensionSnapin, CSmLogExtension)
  26. OBJECT_ENTRY(CLSID_PerformanceAbout, CSmLogAbout)
  27. END_OBJECT_MAP()
  28. LPCWSTR g_cszAllowedPathKey = L"System\\CurrentControlSet\\Control\\SecurePipeServers\\winreg\\AllowedPaths";
  29. LPCWSTR g_cszSysmonLogPath = L"System\\CurrentControlSet\\Services\\SysmonLog";
  30. LPCWSTR g_cszPerflibPath = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Perflib";
  31. LPCWSTR g_cszMachine = L"Machine";
  32. LPCWSTR g_cszBasePath = L"Software\\Microsoft\\MMC\\SnapIns";
  33. LPCWSTR g_cszBaseNodeTypes = L"Software\\Microsoft\\MMC\\NodeTypes";
  34. LPCWSTR g_cszNameString = L"NameString";
  35. LPCWSTR g_cszNameStringIndirect = L"NameStringIndirect";
  36. LPCWSTR g_cszProvider = L"Provider";
  37. LPCWSTR g_cszVersion = L"Version";
  38. LPCWSTR g_cszAbout = L"About";
  39. LPCWSTR g_cszStandAlone = L"StandAlone";
  40. LPCWSTR g_cszNodeType = L"NodeType";
  41. LPCWSTR g_cszNodeTypes = L"NodeTypes";
  42. LPCWSTR g_cszExtensions = L"Extensions";
  43. LPCWSTR g_cszNameSpace = L"NameSpace";
  44. LPCWSTR g_cszRootNode = L"Root Node";
  45. LPCWSTR g_cszCounterLogsChild = L"Performance Data Logs Child Under Root Node";
  46. LPCWSTR g_cszTraceLogsChild = L"System Trace Logs Child Under Root Node";
  47. LPCWSTR g_cszAlertsChild = L"Alerts Child Under Root Node";
  48. DWORD SetWbemSecurity( );
  49. class CSmLogCfgApp : public CWinApp
  50. {
  51. public:
  52. virtual BOOL InitInstance();
  53. virtual int ExitInstance();
  54. };
  55. CSmLogCfgApp theApp;
  56. BOOL CSmLogCfgApp::InitInstance()
  57. {
  58. g_hinst = m_hInstance; // Store global instance handle
  59. _Module.Init(ObjectMap, m_hInstance);
  60. SHFusionInitializeFromModuleID (m_hInstance, 2);
  61. InitializeCriticalSection ( &g_critsectInstallDefaultQueries );
  62. return CWinApp::InitInstance();
  63. }
  64. int CSmLogCfgApp::ExitInstance()
  65. {
  66. DeleteCriticalSection ( &g_critsectInstallDefaultQueries );
  67. SHFusionUninitialize();
  68. _Module.Term();
  69. return CWinApp::ExitInstance();
  70. }
  71. //
  72. // The function is here because of bug 611310 --hongg
  73. //
  74. DWORD
  75. LoadPerfUpdateWinRegAllowedPaths()
  76. {
  77. DWORD Status = ERROR_SUCCESS;
  78. HKEY hKey = NULL;
  79. DWORD dwType;
  80. DWORD dwCurrentSize = 0;
  81. DWORD dwBufSize = 0;
  82. DWORD dwPerflibPath = lstrlenW(g_cszPerflibPath) + 1;
  83. DWORD dwSysmonLogPath = lstrlenW(g_cszSysmonLogPath) + 1;
  84. LPWSTR pBuf = NULL;
  85. LPWSTR pNextPath;
  86. BOOL bPerfLibExists = FALSE;
  87. BOOL bSysmonLogExists = FALSE;
  88. HRESULT hr;
  89. //
  90. // Open AllowedPaths key
  91. //
  92. Status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, g_cszAllowedPathKey, 0L, KEY_READ | KEY_WRITE, & hKey);
  93. if (Status == ERROR_SUCCESS) {
  94. //
  95. // Read the Machine value under AllowedPaths key
  96. //
  97. dwType = REG_MULTI_SZ;
  98. Status = RegQueryValueExW(hKey, g_cszMachine, NULL, & dwType, NULL, & dwCurrentSize);
  99. if (Status == ERROR_SUCCESS) {
  100. if (dwType != REG_MULTI_SZ) {
  101. Status = ERROR_DATATYPE_MISMATCH;
  102. }
  103. }
  104. if (Status == ERROR_SUCCESS) {
  105. //
  106. // In case that PerfLibPath and SysmonLogPath don't exist,
  107. // preallocate memory for PerfLibPath and SysmonLogPath
  108. //
  109. dwBufSize = dwCurrentSize + (dwPerflibPath + dwSysmonLogPath + 1) * sizeof(WCHAR);
  110. pBuf = (LPWSTR)malloc(dwBufSize);
  111. if (pBuf == NULL) {
  112. Status = ERROR_OUTOFMEMORY;
  113. }
  114. else {
  115. *pBuf = L'\0';
  116. Status = RegQueryValueExW(hKey, g_cszMachine, NULL, & dwType, (LPBYTE) pBuf, & dwCurrentSize);
  117. }
  118. }
  119. }
  120. //
  121. // Scan the AllowedPaths to determine if we need to
  122. // update it.
  123. //
  124. if (Status == ERROR_SUCCESS && pBuf != NULL) {
  125. pNextPath = pBuf;
  126. while (* pNextPath != L'\0') {
  127. if (lstrcmpiW(pNextPath, g_cszPerflibPath) == 0) {
  128. bPerfLibExists = TRUE;
  129. }
  130. if (lstrcmpiW(pNextPath, g_cszSysmonLogPath) == 0) {
  131. bSysmonLogExists = TRUE;
  132. }
  133. pNextPath += lstrlenW(pNextPath) + 1;
  134. }
  135. if (! bPerfLibExists) {
  136. hr = StringCchCopyW(pNextPath, dwPerflibPath, g_cszPerflibPath);
  137. dwCurrentSize += dwPerflibPath * sizeof(WCHAR);
  138. pNextPath += dwPerflibPath;
  139. }
  140. if (! bSysmonLogExists) {
  141. hr = StringCchCopyW(pNextPath, dwSysmonLogPath, g_cszSysmonLogPath);
  142. dwCurrentSize += dwSysmonLogPath * sizeof(WCHAR);
  143. pNextPath += dwSysmonLogPath;
  144. }
  145. //
  146. // Add an extra L'\0' for MULTI_SZ
  147. //
  148. * pNextPath = L'\0';
  149. if (! (bPerfLibExists && bSysmonLogExists)) {
  150. Status = RegSetValueExW(hKey, g_cszMachine, 0L, dwType, (LPBYTE) pBuf, dwCurrentSize);
  151. }
  152. }
  153. if (hKey != NULL) {
  154. RegCloseKey(hKey);
  155. }
  156. if (pBuf) {
  157. free(pBuf);
  158. }
  159. return Status;
  160. }
  161. /////////////////////////////////////////////////////////////////////////////
  162. // Used to determine whether the DLL can be unloaded by OLE
  163. STDAPI DllCanUnloadNow(void)
  164. {
  165. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  166. return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  167. }
  168. /////////////////////////////////////////////////////////////////////////////
  169. // Returns a class factory to create an object of the requested type
  170. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  171. {
  172. // test for snap-in or extension snap-in guid and differentiate the
  173. // returned object here before returning (not implemented yet...)
  174. return _Module.GetClassObject(rclsid, riid, ppv);
  175. }
  176. /////////////////////////////////////////////////////////////////////////////
  177. // DllRegisterServer - Adds entries to the system registry
  178. //
  179. STDAPI DllRegisterServer(void)
  180. {
  181. HRESULT hr = S_OK;
  182. HKEY hMmcSnapinsKey = NULL;
  183. HKEY hMmcNodeTypesKey = NULL;
  184. HKEY hSmLogMgrParentKey = NULL;
  185. HKEY hStandAloneKey = NULL;
  186. HKEY hNodeTypesKey = NULL;
  187. HKEY hTempNodeKey = NULL;
  188. HKEY hNameSpaceKey = NULL;
  189. LONG nErr = 0;
  190. WCHAR pBuffer[_MAX_PATH+1]; // NOTE: Use for Provider, Version and module name strings
  191. size_t nLen;
  192. CString strName;
  193. LPWSTR szModule = NULL;
  194. UINT iModuleLen = 0;
  195. LPWSTR szSystemPath = NULL;
  196. UINT iSystemPathLen = 0;
  197. int iRetry;
  198. DWORD dwReturn;
  199. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  200. SetWbemSecurity( );
  201. #ifdef _X86_
  202. BOOL bWow64Process;
  203. #endif
  204. //
  205. // Get system directory
  206. //
  207. iSystemPathLen = MAX_PATH + 14;
  208. iRetry = 4;
  209. do {
  210. //
  211. // We also need to append "\smlogcfg.dll" to the system path
  212. // So allocate an extra 14 characters for it.
  213. //
  214. szSystemPath = (LPWSTR)malloc(iSystemPathLen * sizeof(WCHAR));
  215. if (szSystemPath == NULL) {
  216. hr = E_OUTOFMEMORY;
  217. break;
  218. }
  219. dwReturn = GetSystemDirectory(szSystemPath, iSystemPathLen);
  220. if (dwReturn == 0) {
  221. hr = E_UNEXPECTED;
  222. break;
  223. }
  224. //
  225. // The buffer is not big enough, try to allocate a biggers one
  226. // and retry
  227. //
  228. if (dwReturn >= iSystemPathLen - 14) {
  229. iSystemPathLen = dwReturn + 14;
  230. free(szSystemPath);
  231. szSystemPath = NULL;
  232. hr = E_UNEXPECTED;
  233. }
  234. else {
  235. hr = S_OK;
  236. break;
  237. }
  238. } while (iRetry--);
  239. //
  240. // Get module file name
  241. //
  242. if (SUCCEEDED(hr)) {
  243. iRetry = 4;
  244. //
  245. // The length initialized to iModuleLen must be longer
  246. // than the length of "%systemroot%\\system32\\smlogcfg.dll"
  247. //
  248. iModuleLen = MAX_PATH + 1;
  249. do {
  250. szModule = (LPWSTR) malloc(iModuleLen * sizeof(WCHAR));
  251. if (szModule == NULL) {
  252. hr = E_OUTOFMEMORY;
  253. break;
  254. }
  255. dwReturn = GetModuleFileName(AfxGetInstanceHandle(), szModule, iModuleLen);
  256. if (dwReturn == 0) {
  257. hr = E_UNEXPECTED;
  258. break;
  259. }
  260. //
  261. // The buffer is not big enough, try to allocate a biggers one
  262. // and retry
  263. //
  264. if (dwReturn >= iModuleLen) {
  265. iModuleLen *= 2;
  266. free(szModule);
  267. szModule = NULL;
  268. hr = E_UNEXPECTED;
  269. }
  270. else {
  271. hr = S_OK;
  272. break;
  273. }
  274. } while (iRetry--);
  275. }
  276. if (FAILED(hr)) {
  277. goto CleanUp;
  278. }
  279. //
  280. // Check if we are in system directory, the control can be
  281. // registered iff when it is system directory
  282. //
  283. StringCchCat(szSystemPath, iSystemPathLen, L"\\smlogcfg.dll");
  284. if (lstrcmpi(szSystemPath, szModule) != 0) {
  285. #ifdef _X86_
  286. //
  287. // Lets try to see if this is a Wow64 process
  288. //
  289. if ((IsWow64Process (GetCurrentProcess(), &bWow64Process) == TRUE) &&
  290. (bWow64Process == TRUE))
  291. {
  292. int iLength = GetSystemWow64Directory (szSystemPath, iSystemPathLen);
  293. if (iLength > 0) {
  294. szSystemPath [iLength] = L'\\';
  295. if (lstrcmpi(szSystemPath, szModule) == 0) {
  296. goto done;
  297. }
  298. }
  299. }
  300. #endif
  301. hr = E_UNEXPECTED;
  302. goto CleanUp;
  303. }
  304. #ifdef _X86_
  305. done:
  306. #endif
  307. if (ERROR_SUCCESS != LoadPerfUpdateWinRegAllowedPaths()) {
  308. hr = E_UNEXPECTED;
  309. goto CleanUp;
  310. }
  311. //DebugBreak(); // Uncomment this to step through registration
  312. // Open the MMC Parent keys
  313. nErr = RegOpenKey( HKEY_LOCAL_MACHINE,
  314. g_cszBasePath,
  315. &hMmcSnapinsKey
  316. );
  317. if( ERROR_SUCCESS != nErr )
  318. DisplayError( GetLastError(), L"Open MMC Snapins Key Failed" );
  319. // Create the ID for our ICompnentData Interface
  320. // The ID was generated for us, because we used a Wizard to create the app.
  321. // Take the ID for CComponentData in the IDL file.
  322. //
  323. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  324. // Make sure you change this if you use this code as a starting point!
  325. // Change other IDs as well below!
  326. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  327. if (hMmcSnapinsKey) {
  328. nErr = RegCreateKey(
  329. hMmcSnapinsKey,
  330. GUIDSTR_ComponentData,
  331. &hSmLogMgrParentKey
  332. );
  333. if( ERROR_SUCCESS != nErr )
  334. DisplayError( GetLastError(), L"CComponentData Key Failed" );
  335. if (hSmLogMgrParentKey) {
  336. STANDARD_TRY
  337. strName.LoadString ( IDS_MMC_DEFAULT_NAME );
  338. MFC_CATCH_MINIMUM
  339. if ( strName.IsEmpty() ) {
  340. DisplayError ( ERROR_OUTOFMEMORY,
  341. L"Unable to load snap-in name string." );
  342. }
  343. // This is the name we see when we add the Snap-In to the console
  344. nErr = RegSetValueEx( hSmLogMgrParentKey,
  345. g_cszNameString,
  346. 0,
  347. REG_SZ,
  348. (LPBYTE)strName.GetBufferSetLength( strName.GetLength() ),
  349. strName.GetLength() * (DWORD)sizeof(WCHAR)
  350. );
  351. if( ERROR_SUCCESS != nErr )
  352. DisplayError( GetLastError(), L"Set NameString Failed" );
  353. // This is the indirect name we see when we add the Snap-In to the console.
  354. // Added for MUI support. Use the same name string as for NameString.
  355. STANDARD_TRY
  356. strName.Format (L"@%s,-%d", szModule, IDS_MMC_DEFAULT_NAME );
  357. MFC_CATCH_MINIMUM
  358. if ( strName.IsEmpty() ) {
  359. DisplayError ( ERROR_OUTOFMEMORY,
  360. L"Unable to load snap-in indirect name string." );
  361. }
  362. nErr = RegSetValueEx( hSmLogMgrParentKey,
  363. g_cszNameStringIndirect,
  364. 0,
  365. REG_SZ,
  366. (LPBYTE)strName.GetBufferSetLength( strName.GetLength() ),
  367. strName.GetLength() * (DWORD)sizeof(WCHAR)
  368. );
  369. if( ERROR_SUCCESS != nErr )
  370. DisplayError( GetLastError(), L"Set NameStringIndirect Failed" );
  371. // This is the primary node, or class which implements CComponentData
  372. nErr = RegSetValueEx( hSmLogMgrParentKey,
  373. g_cszNodeType,
  374. 0,
  375. REG_SZ,
  376. (LPBYTE)GUIDSTR_RootNode,
  377. (DWORD)((lstrlen(GUIDSTR_RootNode)+1) * sizeof(WCHAR))
  378. );
  379. if( ERROR_SUCCESS != nErr )
  380. DisplayError( GetLastError(), L"Set NodeType Failed" );
  381. // This is the About box information
  382. nErr = RegSetValueEx( hSmLogMgrParentKey,
  383. g_cszAbout,
  384. 0,
  385. REG_SZ,
  386. (LPBYTE)GUIDSTR_PerformanceAbout,
  387. (DWORD)((lstrlen(GUIDSTR_PerformanceAbout)+1) * sizeof(WCHAR))
  388. );
  389. if( ERROR_SUCCESS != nErr )
  390. DisplayError( GetLastError(), L"Set About Failed" );
  391. nLen = strlen(VER_COMPANYNAME_STR);
  392. #ifdef UNICODE
  393. nLen = mbstowcs(pBuffer, VER_COMPANYNAME_STR, nLen);
  394. pBuffer[nLen] = UNICODE_NULL;
  395. #else
  396. strcpy(pBuffer, VER_COMPANYNAME_STR);
  397. pBuffer[nLen] = ANSI_NULL;
  398. #endif
  399. nErr = RegSetValueEx( hSmLogMgrParentKey,
  400. g_cszProvider,
  401. 0,
  402. REG_SZ,
  403. (LPBYTE)pBuffer,
  404. (DWORD)((nLen+1) * sizeof(WCHAR))
  405. );
  406. if( ERROR_SUCCESS != nErr )
  407. DisplayError( GetLastError(), L"Set Provider Failed" );
  408. nLen = strlen(VER_PRODUCTVERSION_STR);
  409. #ifdef UNICODE
  410. nLen = mbstowcs(pBuffer, VER_PRODUCTVERSION_STR, nLen);
  411. pBuffer[nLen] = UNICODE_NULL;
  412. #else
  413. strcpy(pBuffer, VER_PRODUCTVERSION_STR);
  414. pBuffer[nLen] = ANSI_NULL;
  415. #endif
  416. nErr = RegSetValueEx( hSmLogMgrParentKey,
  417. g_cszVersion,
  418. 0,
  419. REG_SZ,
  420. (LPBYTE)pBuffer,
  421. (DWORD)((nLen+1) * sizeof(WCHAR))
  422. );
  423. if( ERROR_SUCCESS != nErr )
  424. DisplayError( GetLastError(), L"Set Version Failed" );
  425. // We are a stand alone snapin, so set the key for this
  426. nErr = RegCreateKey(
  427. hSmLogMgrParentKey,
  428. g_cszStandAlone,
  429. &hStandAloneKey);
  430. if( ERROR_SUCCESS != nErr )
  431. DisplayError( GetLastError(), L"Create StandAlone Key Failed" );
  432. if (hStandAloneKey) {
  433. // StandAlone has no children, so close it
  434. nErr = RegCloseKey( hStandAloneKey );
  435. if( ERROR_SUCCESS != nErr )
  436. DisplayError( GetLastError(), L"Close StandAlone Failed" );
  437. }
  438. // Set the node types that appear in our snapin
  439. nErr = RegCreateKey (
  440. hSmLogMgrParentKey,
  441. g_cszNodeTypes,
  442. &hNodeTypesKey );
  443. if( ERROR_SUCCESS != nErr )
  444. DisplayError( GetLastError(), L"Create NodeTypes Key Failed" );
  445. if (hNodeTypesKey) {
  446. // Here is our root node. Used uuidgen to get it
  447. nErr = RegCreateKey( hNodeTypesKey,
  448. GUIDSTR_RootNode,
  449. &hTempNodeKey
  450. );
  451. if( ERROR_SUCCESS != nErr )
  452. DisplayError( GetLastError(), L"Create RootNode Key Failed" );
  453. if (hTempNodeKey) {
  454. nErr = RegSetValueEx( hTempNodeKey,
  455. NULL,
  456. 0,
  457. REG_SZ,
  458. (LPBYTE)g_cszRootNode,
  459. (DWORD)((lstrlen(g_cszRootNode) + 1) * sizeof(WCHAR))
  460. );
  461. if( ERROR_SUCCESS != nErr )
  462. DisplayError( GetLastError(), L"Set Root Node String Failed" );
  463. nErr = RegCloseKey( hTempNodeKey ); // Close it for handle reuse
  464. if( ERROR_SUCCESS != nErr )
  465. DisplayError( GetLastError(), L"Close RootNode Failed" );
  466. }
  467. // Here are our child nodes under the root node. Used uuidgen
  468. // to get them for Counter Logs
  469. hTempNodeKey = NULL;
  470. nErr = RegCreateKey( hNodeTypesKey,
  471. GUIDSTR_CounterMainNode,
  472. &hTempNodeKey
  473. );
  474. if( ERROR_SUCCESS != nErr )
  475. DisplayError( GetLastError(),
  476. L"Create Child Performance Data Logs Node Key Failed" );
  477. if (hTempNodeKey) {
  478. nErr = RegSetValueEx( hTempNodeKey,
  479. NULL,
  480. 0,
  481. REG_SZ,
  482. (LPBYTE)g_cszCounterLogsChild,
  483. (DWORD)((lstrlen(g_cszCounterLogsChild) + 1) * sizeof(WCHAR))
  484. );
  485. if( ERROR_SUCCESS != nErr )
  486. DisplayError( GetLastError(),
  487. L"Set Performance Data Logs Child Node String Failed" );
  488. nErr = RegCloseKey( hTempNodeKey ); // Close it for handle reuse
  489. if( ERROR_SUCCESS != nErr )
  490. DisplayError( GetLastError(),
  491. L"Close Performance Data Logs Child Node Key Failed" );
  492. }
  493. // System Trace Logs
  494. hTempNodeKey = NULL;
  495. nErr = RegCreateKey( hNodeTypesKey,
  496. GUIDSTR_TraceMainNode,
  497. &hTempNodeKey
  498. );
  499. if( ERROR_SUCCESS != nErr )
  500. DisplayError( GetLastError(),
  501. L"Create Child System Trace Logs Node Key Failed" );
  502. if (hTempNodeKey) {
  503. nErr = RegSetValueEx( hTempNodeKey,
  504. NULL,
  505. 0,
  506. REG_SZ,
  507. (LPBYTE)g_cszTraceLogsChild,
  508. (DWORD)((lstrlen(g_cszTraceLogsChild) + 1) * sizeof(WCHAR))
  509. );
  510. if( ERROR_SUCCESS != nErr )
  511. DisplayError( GetLastError(),
  512. L"Set System Trace Logs Child Node String Failed" );
  513. nErr = RegCloseKey( hTempNodeKey ); // Close it for handle reuse
  514. if( ERROR_SUCCESS != nErr )
  515. DisplayError( GetLastError(),
  516. L"Close System Trace Logs Child Node Key Failed" );
  517. }
  518. // Alerts
  519. hTempNodeKey = NULL;
  520. nErr = RegCreateKey(hNodeTypesKey,
  521. GUIDSTR_AlertMainNode,
  522. &hTempNodeKey
  523. );
  524. if( ERROR_SUCCESS != nErr )
  525. DisplayError( GetLastError(),
  526. L"Create Child Alerts Node Key Failed" );
  527. if (hTempNodeKey) {
  528. nErr = RegSetValueEx( hTempNodeKey,
  529. NULL,
  530. 0,
  531. REG_SZ,
  532. (LPBYTE)g_cszAlertsChild,
  533. (DWORD)((lstrlen(g_cszAlertsChild) + 1) * sizeof(WCHAR))
  534. );
  535. if( ERROR_SUCCESS != nErr )
  536. DisplayError( GetLastError(),
  537. L"Set Alerts Child Node String Failed" );
  538. nErr = RegCloseKey( hTempNodeKey );
  539. if( ERROR_SUCCESS != nErr )
  540. DisplayError( GetLastError(),
  541. L"Close Alerts Child Node Key Failed" );
  542. }
  543. nErr = RegCloseKey( hNodeTypesKey );
  544. if( ERROR_SUCCESS != nErr )
  545. DisplayError( GetLastError(), L"Close Node Types Key Failed" );
  546. }
  547. // close the standalone snapin GUID key
  548. nErr = RegCloseKey( hSmLogMgrParentKey );
  549. if( ERROR_SUCCESS != nErr )
  550. DisplayError( GetLastError(), L"Close SmLogManager GUID Key Failed" );
  551. }
  552. // register the extension snap-in with the MMC
  553. hSmLogMgrParentKey = NULL;
  554. nErr = RegCreateKey( hMmcSnapinsKey,
  555. GUIDSTR_SnapInExt,
  556. &hSmLogMgrParentKey
  557. );
  558. if( ERROR_SUCCESS != nErr )
  559. DisplayError( GetLastError(), L"Snapin Extension Key creation Failed" );
  560. STANDARD_TRY
  561. strName.LoadString ( IDS_MMC_DEFAULT_EXT_NAME );
  562. MFC_CATCH_MINIMUM
  563. if ( strName.IsEmpty() ) {
  564. DisplayError ( ERROR_OUTOFMEMORY,
  565. L"Unable to load snap-in extension name string." );
  566. }
  567. if (hSmLogMgrParentKey) {
  568. // This is the name we see when we add the snap-in extension
  569. nErr = RegSetValueEx( hSmLogMgrParentKey,
  570. g_cszNameString,
  571. 0,
  572. REG_SZ,
  573. (LPBYTE)strName.GetBufferSetLength( strName.GetLength() ),
  574. strName.GetLength() * (DWORD)sizeof(WCHAR)
  575. );
  576. strName.ReleaseBuffer();
  577. if( ERROR_SUCCESS != nErr )
  578. DisplayError( GetLastError(), L"Set Extension NameString Failed" );
  579. // This is the name we see when we add the snap-in extension. MUI support.
  580. // Use the same name string as for NameString;
  581. STANDARD_TRY
  582. strName.Format (L"@%s,-%d", szModule, IDS_MMC_DEFAULT_EXT_NAME );
  583. MFC_CATCH_MINIMUM
  584. if ( strName.IsEmpty() ) {
  585. DisplayError ( ERROR_OUTOFMEMORY,
  586. L"Unable to load extension indirect name string." );
  587. }
  588. nErr = RegSetValueEx( hSmLogMgrParentKey,
  589. g_cszNameStringIndirect,
  590. 0,
  591. REG_SZ,
  592. (LPBYTE)strName.GetBufferSetLength( strName.GetLength() ),
  593. strName.GetLength() * (DWORD)sizeof(WCHAR)
  594. );
  595. strName.ReleaseBuffer();
  596. if( ERROR_SUCCESS != nErr )
  597. DisplayError( GetLastError(), L"Set Extension NameStringIndirect Failed" );
  598. // This is the Extension About box information
  599. nErr = RegSetValueEx(
  600. hSmLogMgrParentKey,
  601. g_cszAbout,
  602. 0,
  603. REG_SZ,
  604. (LPBYTE)GUIDSTR_PerformanceAbout,
  605. ((lstrlen(GUIDSTR_PerformanceAbout)+1) * (DWORD)sizeof(WCHAR))
  606. );
  607. if( ERROR_SUCCESS != nErr )
  608. DisplayError( GetLastError(), L"Set Extension About Failed" );
  609. nLen = strlen(VER_COMPANYNAME_STR);
  610. #ifdef UNICODE
  611. nLen = mbstowcs(pBuffer, VER_COMPANYNAME_STR, nLen);
  612. pBuffer[nLen] = UNICODE_NULL;
  613. #else
  614. strcpy(pBuffer, VER_COMPANYNAME_STR);
  615. pBuffer[nLen] = ANSI_NULL;
  616. #endif
  617. nErr = RegSetValueEx( hSmLogMgrParentKey,
  618. g_cszProvider,
  619. 0,
  620. REG_SZ,
  621. (LPBYTE)pBuffer,
  622. (DWORD)((nLen+1) * sizeof(WCHAR))
  623. );
  624. if( ERROR_SUCCESS != nErr )
  625. DisplayError( GetLastError(), L"Set Provider Failed" );
  626. nLen = strlen(VER_PRODUCTVERSION_STR);
  627. #ifdef UNICODE
  628. nLen = mbstowcs(pBuffer, VER_PRODUCTVERSION_STR, nLen);
  629. pBuffer[nLen] = UNICODE_NULL;
  630. #else
  631. strcpy(pBuffer, VER_PRODUCTVERSION_STR);
  632. pBuffer[nLen] = ANSI_NULL;
  633. #endif
  634. nErr = RegSetValueEx( hSmLogMgrParentKey,
  635. g_cszVersion,
  636. 0,
  637. REG_SZ,
  638. (LPBYTE)pBuffer,
  639. (DWORD)((nLen+1) * sizeof(WCHAR))
  640. );
  641. if( ERROR_SUCCESS != nErr )
  642. DisplayError( GetLastError(), L"Set Version Failed" );
  643. // close the main keys
  644. nErr = RegCloseKey( hSmLogMgrParentKey );
  645. if( ERROR_SUCCESS != nErr )
  646. DisplayError( GetLastError(), L"Close Snapin Extension Key Failed");
  647. }
  648. // register this as a "My Computer"-"System Tools" snapin extension
  649. nErr = RegOpenKey( HKEY_LOCAL_MACHINE,
  650. g_cszBaseNodeTypes,
  651. &hMmcNodeTypesKey
  652. );
  653. if( ERROR_SUCCESS != nErr )
  654. DisplayError( GetLastError(), L"Open MMC NodeTypes Key Failed" );
  655. // create/open the GUID of the System Tools Node of the My Computer snap-in
  656. if (hMmcNodeTypesKey) {
  657. nErr = RegCreateKey ( hMmcNodeTypesKey,
  658. lstruuidNodetypeSystemTools,
  659. &hNodeTypesKey
  660. );
  661. if( ERROR_SUCCESS != nErr )
  662. DisplayError( GetLastError(),
  663. L"Create/open System Tools GUID Key Failed" );
  664. if (hNodeTypesKey) {
  665. hTempNodeKey = NULL;
  666. nErr = RegCreateKey ( hNodeTypesKey,
  667. g_cszExtensions,
  668. &hTempNodeKey
  669. );
  670. if( ERROR_SUCCESS != nErr )
  671. DisplayError(
  672. GetLastError(),
  673. L"Create/open System Tools Extensions Key Failed" );
  674. if (hTempNodeKey) {
  675. nErr = RegCreateKey (
  676. hTempNodeKey,
  677. g_cszNameSpace,
  678. &hNameSpaceKey
  679. );
  680. if( ERROR_SUCCESS != nErr )
  681. DisplayError( GetLastError(),
  682. L"Create/open System Tools NameSpace Key Failed" );
  683. if (hNameSpaceKey) {
  684. nErr = RegSetValueEx( hNameSpaceKey,
  685. GUIDSTR_SnapInExt,
  686. 0,
  687. REG_SZ,
  688. (LPBYTE)strName.GetBufferSetLength( strName.GetLength() ),
  689. strName.GetLength() * (DWORD)sizeof(WCHAR)
  690. );
  691. strName.ReleaseBuffer();
  692. if( ERROR_SUCCESS != nErr ) {
  693. DisplayError( GetLastError(),
  694. L"Set Extension NameString Failed" );
  695. DisplayError( GetLastError(),
  696. L"Set Snapin Extension NameString Failed" );
  697. }
  698. nErr = RegCloseKey( hNameSpaceKey );
  699. if( ERROR_SUCCESS != nErr )
  700. DisplayError( GetLastError(),
  701. L"Close NameSpace Key Failed" );
  702. }
  703. nErr = RegCloseKey( hTempNodeKey );
  704. if( ERROR_SUCCESS != nErr )
  705. DisplayError( GetLastError(), L"Close Extension Key Failed" );
  706. }
  707. nErr = RegCloseKey( hNodeTypesKey );
  708. if( ERROR_SUCCESS != nErr )
  709. DisplayError( GetLastError(),
  710. L"Close My Computer System GUID Key Failed" );
  711. }
  712. nErr = RegCloseKey( hMmcNodeTypesKey );
  713. if( ERROR_SUCCESS != nErr )
  714. DisplayError( GetLastError(), L"Close MMC NodeTypes Key Failed" );
  715. }
  716. nErr = RegCloseKey( hMmcSnapinsKey );
  717. if( ERROR_SUCCESS != nErr )
  718. DisplayError( GetLastError(), L"Close MMC Snapins Key Failed" );
  719. }
  720. // Register extension Snap in
  721. nErr = _Module.UpdateRegistryFromResource(IDR_EXTENSION, TRUE);
  722. // Registers object, typelib and all interfaces in typelib
  723. return _Module.RegisterServer(TRUE);
  724. CleanUp:
  725. if (szSystemPath) {
  726. free(szSystemPath);
  727. }
  728. if (szModule) {
  729. free(szModule);
  730. }
  731. return hr;
  732. }
  733. /////////////////////////////////////////////////////////////////////////////
  734. // DllUnregisterServer - Removes entries from the system registry
  735. STDAPI DllUnregisterServer(void)
  736. {
  737. HKEY hMmcSnapinsKey = NULL; // MMC parent key
  738. HKEY hSmLogMgrParentKey = NULL; // Our Snap-In key - has children
  739. HKEY hNodeTypesKey = NULL; // Our NodeType key - has children
  740. HKEY hSysToolsNode = NULL;
  741. HKEY hExtension = NULL;
  742. HKEY hNameSpace = NULL;
  743. LONG nErr = 0;
  744. // DebugBreak(); // Uncomment this to step through UnRegister
  745. // Open the MMC parent key
  746. nErr = RegOpenKey( HKEY_LOCAL_MACHINE,
  747. g_cszBasePath,
  748. &hMmcSnapinsKey
  749. );
  750. if( ERROR_SUCCESS != nErr )
  751. DisplayError( GetLastError(), L"Open MMC Parent Key Failed" );
  752. // Open our Parent key
  753. nErr = RegOpenKey( hMmcSnapinsKey,
  754. GUIDSTR_ComponentData,
  755. &hSmLogMgrParentKey
  756. );
  757. if( ERROR_SUCCESS != nErr )
  758. DisplayError( GetLastError(), L"Open Disk Parent Key Failed" );
  759. // Now open the NodeTypes key
  760. nErr = RegOpenKey( hSmLogMgrParentKey, // Handle of parent key
  761. g_cszNodeTypes, // Name of key to open
  762. &hNodeTypesKey // Handle to newly opened key
  763. );
  764. if( ERROR_SUCCESS != nErr )
  765. DisplayError( GetLastError(), L"Open NodeTypes Key Failed" );
  766. if (hNodeTypesKey) {
  767. // Delete the root node key
  768. nErr = RegDeleteKey( hNodeTypesKey, GUIDSTR_RootNode );
  769. if( ERROR_SUCCESS != nErr )
  770. DisplayError( GetLastError(), L"Delete Root Node Key Failed" );
  771. // Delete the child node key
  772. // *** From Beta 2
  773. nErr = RegDeleteKey( hNodeTypesKey, GUIDSTR_MainNode );
  774. // Delete the child node keys
  775. // Counter logs
  776. nErr = RegDeleteKey( hNodeTypesKey, GUIDSTR_CounterMainNode );
  777. if( ERROR_SUCCESS != nErr )
  778. DisplayError( GetLastError(), L"Delete Performance Logs and Alerts Child Node Key Failed" );
  779. // System Trace Logs
  780. nErr = RegDeleteKey( hNodeTypesKey, GUIDSTR_TraceMainNode );
  781. if( ERROR_SUCCESS != nErr )
  782. DisplayError( GetLastError(), L"Delete System Trace Logs Child Node Key Failed" );
  783. // Alerts
  784. nErr = RegDeleteKey( hNodeTypesKey, GUIDSTR_AlertMainNode );
  785. if( ERROR_SUCCESS != nErr )
  786. DisplayError( GetLastError(), L"Delete Alerts Child Node Key Failed" );
  787. // Close the node type key so we can delete it
  788. nErr = RegCloseKey( hNodeTypesKey );
  789. if( ERROR_SUCCESS != nErr )
  790. DisplayError( GetLastError(), L"Close NodeTypes Key failed" );
  791. }
  792. // Delete the NodeTypes key
  793. if (hSmLogMgrParentKey) {
  794. nErr = RegDeleteKey( hSmLogMgrParentKey, L"NodeTypes" );
  795. if( ERROR_SUCCESS != nErr )
  796. DisplayError( GetLastError(), L"Delete NodeTypes Key failed" );
  797. // StandAlone key has no children so we can delete it now
  798. nErr = RegDeleteKey(
  799. hSmLogMgrParentKey,
  800. g_cszStandAlone );
  801. if( ERROR_SUCCESS != nErr )
  802. DisplayError( GetLastError(), L"Delete StandAlone Key Failed" );
  803. // Close our Parent Key
  804. nErr = RegCloseKey( hSmLogMgrParentKey );
  805. if( ERROR_SUCCESS != nErr )
  806. DisplayError( GetLastError(), L"Close Disk Parent Key Failed" );
  807. }
  808. if (hMmcSnapinsKey) {
  809. // Now we can delete our Snap-In key since the children are gone
  810. nErr = RegDeleteKey( hMmcSnapinsKey, GUIDSTR_ComponentData );
  811. if( ERROR_SUCCESS != nErr )
  812. DisplayError( GetLastError(), L"Delete Performance Logs and Alerts GUID Key Failed" );
  813. // Now we can delete our Snap-In Extension key
  814. nErr = RegDeleteKey( hMmcSnapinsKey, GUIDSTR_SnapInExt);
  815. if( ERROR_SUCCESS != nErr )
  816. DisplayError( GetLastError(), L"Delete Performance Logs and Alerts GUID Key Failed" );
  817. nErr = RegCloseKey( hMmcSnapinsKey );
  818. if( ERROR_SUCCESS != nErr )
  819. DisplayError( GetLastError(), L"Close MMC Parent Key Failed" );
  820. }
  821. // delete snap-in extension entry
  822. hNodeTypesKey = NULL;
  823. // Open the MMC parent key
  824. nErr = RegOpenKey( HKEY_LOCAL_MACHINE,
  825. g_cszBaseNodeTypes,
  826. &hNodeTypesKey
  827. );
  828. if( ERROR_SUCCESS != nErr )
  829. DisplayError( GetLastError(), L"Open of MMC NodeTypes Key Failed" );
  830. if (hNodeTypesKey) {
  831. hSysToolsNode = NULL;
  832. nErr = RegOpenKey (hNodeTypesKey,
  833. lstruuidNodetypeSystemTools,
  834. &hSysToolsNode
  835. );
  836. if( ERROR_SUCCESS != nErr )
  837. DisplayError( GetLastError(),
  838. L"Open of My Computer System Tools Key Failed" );
  839. if (hSysToolsNode) {
  840. hExtension = NULL;
  841. nErr = RegOpenKey (hSysToolsNode,
  842. g_cszExtensions,
  843. &hExtension
  844. );
  845. if( ERROR_SUCCESS != nErr )
  846. DisplayError( GetLastError(), L"Open of Extensions Key Failed" );
  847. if (hExtension) {
  848. hNameSpace = NULL;
  849. nErr = RegOpenKey (hExtension,
  850. g_cszNameSpace,
  851. &hNameSpace
  852. );
  853. if( ERROR_SUCCESS != nErr )
  854. DisplayError( GetLastError(),
  855. L"Open of Name Space Key Failed" );
  856. if (hNameSpace) {
  857. nErr = RegDeleteValue (hNameSpace, GUIDSTR_SnapInExt);
  858. if( ERROR_SUCCESS != nErr )
  859. DisplayError( GetLastError(),
  860. L"Unable to remove the Snap-in Ext. GUID" );
  861. // close keys
  862. nErr = RegCloseKey( hNameSpace );
  863. if( ERROR_SUCCESS != nErr )
  864. DisplayError( GetLastError(),
  865. L"Close NameSpace Key Failed" );
  866. }
  867. nErr = RegCloseKey( hExtension);
  868. if( ERROR_SUCCESS != nErr )
  869. DisplayError(GetLastError(), L"Close Extension Key Failed");
  870. }
  871. nErr = RegCloseKey( hSysToolsNode );
  872. if( ERROR_SUCCESS != nErr )
  873. DisplayError( GetLastError(),
  874. L"Close My Computer System Tools Key Failed" );
  875. }
  876. nErr = RegCloseKey( hNodeTypesKey);
  877. if( ERROR_SUCCESS != nErr )
  878. DisplayError( GetLastError(), L"Close MMC Node Types Key Failed" );
  879. }
  880. _Module.UnregisterServer();
  881. return S_OK;
  882. }
  883. DWORD
  884. SetWbemSecurity( )
  885. {
  886. HRESULT hr;
  887. IWbemLocator *pLocator = NULL;
  888. BSTR bszNamespace = SysAllocString( L"\\\\.\\root\\perfmon" );
  889. BSTR bszClass = SysAllocString( L"__SystemSecurity" );
  890. BSTR bszClassSingle = SysAllocString( L"__SystemSecurity=@" );
  891. BSTR bszMethodName = SysAllocString( L"SetSD" );
  892. IWbemClassObject* pWbemClass = NULL;
  893. IWbemServices* pWbemServices = NULL;
  894. IWbemClassObject* pInClass = NULL;
  895. IWbemClassObject* pInInst = NULL;
  896. BOOL bResult = TRUE;
  897. PSECURITY_DESCRIPTOR SD = NULL;
  898. DWORD dwAclSize;
  899. SAFEARRAYBOUND saBound;
  900. BYTE* pData;
  901. SAFEARRAY * pSa;
  902. VARIANT vArray;
  903. if( NULL == bszNamespace ||
  904. NULL == bszClass ||
  905. NULL == bszClassSingle ||
  906. NULL == bszMethodName ){
  907. hr = ERROR_OUTOFMEMORY;
  908. goto cleanup;
  909. }
  910. VariantInit( &vArray );
  911. hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
  912. if( S_FALSE == hr ){
  913. hr = ERROR_SUCCESS;
  914. }
  915. hr = CoCreateInstance(
  916. CLSID_WbemLocator,
  917. 0,
  918. CLSCTX_INPROC_SERVER,
  919. IID_IWbemLocator,
  920. (LPVOID*)&pLocator
  921. );
  922. if(FAILED(hr)){ goto cleanup; }
  923. hr = pLocator->ConnectServer(
  924. bszNamespace,
  925. NULL,
  926. NULL,
  927. NULL,
  928. 0,
  929. NULL,
  930. NULL,
  931. &pWbemServices
  932. );
  933. if(FAILED(hr)){ goto cleanup; }
  934. hr = pWbemServices->GetObject( bszClass, 0, NULL, &pWbemClass, NULL);
  935. if(FAILED(hr)){ goto cleanup; }
  936. hr = pWbemClass->GetMethod( bszMethodName, 0, &pInClass, NULL);
  937. if(FAILED(hr)){ goto cleanup; }
  938. hr = pInClass->SpawnInstance(0, &pInInst);
  939. if(FAILED(hr)){ goto cleanup; }
  940. LPWSTR pSSDLString = L"O:BAG:BAD:(A;;0x23;;;LU)";
  941. bResult = ConvertStringSecurityDescriptorToSecurityDescriptorW(
  942. pSSDLString,
  943. SDDL_REVISION_1,
  944. &SD,
  945. &dwAclSize);
  946. if( !bResult ){ goto cleanup; }
  947. saBound.lLbound = 0;
  948. saBound.cElements = dwAclSize;
  949. pSa = SafeArrayCreate(VT_UI1,1,&saBound);
  950. if( NULL == pSa ){
  951. hr = ERROR_OUTOFMEMORY;
  952. goto cleanup;
  953. }
  954. SafeArrayAccessData(pSa, (void**)&pData);
  955. memcpy(pData,SD,dwAclSize);
  956. SafeArrayUnaccessData( pSa );
  957. vArray.vt = VT_ARRAY | VT_UI1;
  958. vArray.parray = pSa;
  959. hr = pInInst->Put( L"SD", 0, &vArray, 0 );
  960. if(FAILED(hr)){ goto cleanup; }
  961. hr = pWbemServices->ExecMethod(
  962. bszClassSingle,
  963. bszMethodName,
  964. 0,
  965. NULL,
  966. pInInst,
  967. NULL,
  968. NULL);
  969. if(FAILED(hr)){ goto cleanup; }
  970. cleanup:
  971. if( !bResult ){
  972. hr = GetLastError();
  973. }
  974. VariantClear( &vArray );
  975. if( pLocator != NULL ){
  976. pLocator->Release();
  977. }
  978. if( pWbemClass != NULL ){
  979. pWbemClass->Release();
  980. }
  981. if( pWbemServices != NULL ){
  982. pWbemServices->Release();
  983. }
  984. if( pInInst != NULL ){
  985. pInInst->Release();
  986. }
  987. if( NULL != SD ){
  988. LocalFree( SD );
  989. }
  990. SysFreeString( bszNamespace );
  991. SysFreeString( bszClass );
  992. SysFreeString( bszClassSingle );
  993. SysFreeString( bszMethodName );
  994. return hr;
  995. }