Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2352 lines
91 KiB

  1. /*++
  2. Copyright (C) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. compdata.cpp
  5. Abstract:
  6. Implementation of the CComponentData class.
  7. This class is the interface to handle anything to do
  8. with the scope pane. MMC calls the IComponentData interfaces.
  9. This class keeps a few pointers to interfaces that MMC
  10. implements.
  11. --*/
  12. #include "stdafx.h"
  13. #include <shfolder.h>
  14. #include "smcfgmsg.h"
  15. #include "smtprov.h"
  16. #include "smrootnd.h"
  17. #include "ipropbag.h"
  18. #include "smlogqry.h"
  19. #include "cmponent.h"
  20. #include "smcfgmsg.h"
  21. #include "newqdlg.h"
  22. #include "logwarnd.h"
  23. #include "strnoloc.h"
  24. #include "ctrsprop.h"
  25. #include "fileprop.h"
  26. #include "provprop.h"
  27. #include "schdprop.h"
  28. #include "tracprop.h"
  29. #include "AlrtGenP.h"
  30. #include "AlrtActP.h"
  31. //
  32. #include "compdata.h"
  33. USE_HANDLE_MACROS("SMLOGCFG(compdata.cpp)");
  34. GUID g_guidSystemTools = structuuidNodetypeSystemTools;
  35. extern DWORD g_dwRealTimeQuery;
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CComponentData
  38. CComponentData::CComponentData()
  39. : m_bIsExtension( FALSE ),
  40. m_ipConsoleNameSpace ( NULL ),
  41. m_ipConsole ( NULL ),
  42. m_ipResultData ( NULL ),
  43. m_ipPrshtProvider ( NULL ),
  44. m_ipScopeImage ( NULL )
  45. {
  46. m_hModule = (HINSTANCE)GetModuleHandleW (_CONFIG_DLL_NAME_W_);
  47. }
  48. CComponentData::~CComponentData()
  49. {
  50. // Make sure the list is empty.
  51. ASSERT ( m_listpRootNode.IsEmpty() );
  52. }
  53. /////////////////////////////////////////////////////////////////////////////
  54. // IComponentData methods
  55. //
  56. //---------------------------------------------------------------------------
  57. // We get here only once, when the user clicks on the snapin.
  58. //
  59. // This method should not change as we progress through further steps.
  60. // Here we get a chance to get pointer to some interfaces MMC provides.
  61. // We QueryInterface for pointers to the name space and console, which
  62. // we cache in local variables
  63. // The other task to acomplish here is the adding of a bitmap that contains
  64. // the icons to be used in the scope pane.
  65. //
  66. STDMETHODIMP
  67. CComponentData::Initialize (
  68. LPUNKNOWN pUnknown // [in] Pointer to the IConsole�s IUnknown interface
  69. )
  70. {
  71. HRESULT hr;
  72. ASSERT( NULL != pUnknown );
  73. HBITMAP hbmpSNodes16 = NULL;
  74. HBITMAP hbmpSNodes32 = NULL;
  75. BOOL bWasReleased;
  76. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  77. // LPIMAGELIST lpScopeImage;
  78. // MMC should only call ::Initialize once!
  79. ASSERT( NULL == m_ipConsoleNameSpace );
  80. // Get pointer to name space interface
  81. hr = pUnknown->QueryInterface(IID_IConsoleNameSpace, (VOID**)(&m_ipConsoleNameSpace));
  82. ASSERT( S_OK == hr );
  83. // Get pointer to console interface
  84. hr = pUnknown->QueryInterface(IID_IConsole, (VOID**)(&m_ipConsole));
  85. ASSERT( S_OK == hr );
  86. // Get pointer to property sheet provider interface
  87. hr = m_ipConsole->QueryInterface(IID_IPropertySheetProvider, (VOID**)&m_ipPrshtProvider);
  88. ASSERT( S_OK == hr );
  89. // Add the images for the scope tree
  90. hr = m_ipConsole->QueryScopeImageList(&m_ipScopeImage);
  91. ASSERT( S_OK == hr );
  92. // Load the bitmaps from the dll
  93. hbmpSNodes16 = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_NODES_16x16));
  94. ASSERT( NULL != hbmpSNodes16 );
  95. hbmpSNodes32 = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_NODES_32x32));
  96. ASSERT( NULL != hbmpSNodes32 );
  97. // Set the images
  98. hr = m_ipScopeImage->ImageListSetStrip(
  99. (LONG_PTR *)hbmpSNodes16,
  100. (LONG_PTR *)hbmpSNodes32,
  101. 0,
  102. RGB(0,255,0)
  103. );
  104. ASSERT( S_OK == hr );
  105. if ( NULL != hbmpSNodes16 ) {
  106. bWasReleased = DeleteObject( hbmpSNodes16 );
  107. ASSERT( bWasReleased );
  108. }
  109. if ( NULL != hbmpSNodes32 ) {
  110. bWasReleased = DeleteObject( hbmpSNodes32 );
  111. ASSERT( bWasReleased );
  112. }
  113. return S_OK;
  114. } // end Initialize()
  115. //---------------------------------------------------------------------------
  116. // Release interfaces and clean up objects which allocated memory
  117. //
  118. STDMETHODIMP
  119. CComponentData::Destroy()
  120. {
  121. CSmRootNode* pRootNode = NULL;
  122. POSITION Pos = m_listpRootNode.GetHeadPosition();
  123. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  124. while ( Pos != NULL) {
  125. pRootNode = m_listpRootNode.GetNext( Pos );
  126. pRootNode->Destroy();
  127. delete (pRootNode);
  128. }
  129. // empty the list now that everything has been closed;
  130. m_listpRootNode.RemoveAll();
  131. // Free interfaces
  132. if ( NULL != m_ipConsoleNameSpace )
  133. m_ipConsoleNameSpace->Release();
  134. if ( NULL != m_ipConsole )
  135. m_ipConsole->Release();
  136. if ( NULL != m_ipResultData )
  137. m_ipResultData->Release();
  138. if ( NULL != m_ipScopeImage )
  139. m_ipScopeImage->Release();
  140. if ( NULL != m_ipPrshtProvider)
  141. m_ipPrshtProvider->Release();
  142. return S_OK;
  143. } // end Destroy()
  144. //---------------------------------------------------------------------------
  145. // Come in here once right after Initialize. MMC wants a pointer to the
  146. // IComponent interface.
  147. //
  148. STDMETHODIMP
  149. CComponentData::CreateComponent (
  150. LPCOMPONENT* ppComponent // [out] Pointer to the location that stores
  151. ) // the newly created pointer to IComponent
  152. {
  153. HRESULT hr = E_FAIL;
  154. CComObject<CComponent>* pObject;
  155. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  156. // MMC asks us for a pointer to the IComponent interface
  157. //
  158. // For those getting up to speed with COM...
  159. // If we had implemented IUnknown with its methods QueryInterface, AddRef,
  160. // and Release in our CComponent class...
  161. // The following line would have worked
  162. //
  163. // pNewSnapin = new CComponent(this);
  164. //
  165. // In this code we will have ATL take care of IUnknown for us and create
  166. // an object in the following manner...
  167. if ( NULL == ppComponent ) {
  168. ASSERT ( FALSE );
  169. hr = E_INVALIDARG;
  170. } else {
  171. CComObject<CComponent>::CreateInstance( &pObject );
  172. if ( NULL != pObject ) {
  173. hr = pObject->SetIComponentData( this );
  174. if ( SUCCEEDED ( hr ) ) {
  175. hr = pObject->QueryInterface (
  176. IID_IComponent,
  177. reinterpret_cast<void**>(ppComponent) );
  178. } else {
  179. pObject->Release();
  180. }
  181. }
  182. }
  183. return hr;
  184. } // end CreateComponent()
  185. //---------------------------------------------------------------------------
  186. // In this first step, we only implement EXPAND.
  187. // The expand message asks us to populate what is under our root node.
  188. // We just put one item under there.
  189. //
  190. STDMETHODIMP
  191. CComponentData::Notify (
  192. LPDATAOBJECT pDataObject, // [in] Points to the selected data object
  193. MMC_NOTIFY_TYPE event, // [in] Identifies action taken by user.
  194. LPARAM arg, // [in] Depends on the notification type
  195. LPARAM param // [in] Depends on the notification type
  196. )
  197. {
  198. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  199. HRESULT hr = S_OK;
  200. switch (event)
  201. {
  202. case MMCN_EXPAND:
  203. hr = OnExpand( pDataObject, arg, param );
  204. break;
  205. case MMCN_DELETE: // Function not implemented
  206. LOCALTRACE( L"ComponentData::Notify: MMCN_DELETE unimplemented\n" );
  207. hr = S_FALSE;
  208. break;
  209. case MMCN_RENAME: // Function not implemented
  210. LOCALTRACE( L"ComponentData::Notify: MMCN_RENAME unimplemented\n" );
  211. hr = S_FALSE; // False signifies Rename not allowed.
  212. break;
  213. case MMCN_SELECT: // Function not implemented
  214. LOCALTRACE( L"ComponentData::Notify: MMCN_SELECT unimplemented\n" );
  215. hr = S_FALSE;
  216. break;
  217. case MMCN_PROPERTY_CHANGE: // Function not implemented
  218. LOCALTRACE( L"ComponentData::Notify: MMCN_PROPERTY_CHANGE unimplemented\n" );
  219. hr = S_FALSE;
  220. break;
  221. case MMCN_REMOVE_CHILDREN: // Function not implemented
  222. hr = OnRemoveChildren( pDataObject, arg, param );
  223. break;
  224. default:
  225. LOCALTRACE( L"CComponentData::Notify: unimplemented event %x\n", event );
  226. hr = S_FALSE;
  227. break;
  228. }
  229. return hr;
  230. } // end Notify()
  231. //---------------------------------------------------------------------------
  232. // This is where MMC asks us to provide IDataObjects for every node in the
  233. // scope pane. We have to QI the object so it gets AddRef'd. The node
  234. // manager handles deleting the objects.
  235. //
  236. STDMETHODIMP
  237. CComponentData::QueryDataObject (
  238. LPARAM mmcCookie, // [in] Data object's unique identifier
  239. DATA_OBJECT_TYPES context, // [in] Data object's type
  240. LPDATAOBJECT* ppDataObject // [out] Points to the returned data object
  241. )
  242. {
  243. HRESULT hr = S_OK;
  244. CSmNode* pNode = NULL;
  245. CComObject<CDataObject>* pDataObj = NULL;
  246. CString strMessage;
  247. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  248. context; // to prevent compiler errors in no-debug builds
  249. ASSERT( CCT_SCOPE == context || // Must have a context
  250. CCT_RESULT == context || // we understand
  251. CCT_SNAPIN_MANAGER == context
  252. );
  253. if ( NULL != ppDataObject
  254. && ( CCT_SCOPE == context
  255. || CCT_RESULT == context
  256. || CCT_SNAPIN_MANAGER == context ) ) {
  257. CComObject<CDataObject>::CreateInstance( &pDataObj );
  258. if( NULL == pDataObj ) { // DataObject was not created
  259. MFC_TRY
  260. strMessage.LoadString ( IDS_ERRMSG_UNABLEALLOCDATAOBJECT );
  261. ::MessageBox( NULL,
  262. (LPCWSTR)strMessage,
  263. L"CComponentData::QueryDataObject",
  264. MB_OK | MB_ICONERROR
  265. );
  266. MFC_CATCH_HR;
  267. hr = E_OUTOFMEMORY;
  268. } else {
  269. // If the passed-in mmcCookie is non-NULL, then it should be one we
  270. // created when we added a node to the scope pane.
  271. //
  272. // Otherwise the mmcCookie refers to the root folder (this snapin's
  273. // static folder in the scope pane or snapin manager).
  274. //
  275. // Init the mmCookie, context and type in the data object.
  276. if( mmcCookie ) {
  277. pNode = (CSmNode*)mmcCookie;
  278. if ( NULL != pNode->CastToRootNode() ) {
  279. pDataObj->SetData( mmcCookie, CCT_SCOPE, COOKIE_IS_ROOTNODE );
  280. } else if ( NULL != pNode->CastToCounterLogService() ) {
  281. pDataObj->SetData( mmcCookie, CCT_SCOPE, COOKIE_IS_COUNTERMAINNODE );
  282. } else if ( NULL != pNode->CastToTraceLogService() ) {
  283. pDataObj->SetData( mmcCookie, CCT_SCOPE, COOKIE_IS_TRACEMAINNODE );
  284. } else if ( NULL != pNode->CastToAlertService() ) {
  285. pDataObj->SetData( mmcCookie, CCT_SCOPE, COOKIE_IS_ALERTMAINNODE );
  286. } else {
  287. ::MessageBox( NULL,
  288. L"Bad mmcCookie",
  289. L"CComponentData::QueryDataObject",
  290. MB_OK | MB_ICONERROR
  291. );
  292. hr = E_FAIL;
  293. }
  294. } else {
  295. ASSERT( CCT_RESULT != context );
  296. // NOTE: Passed in scope might be either CCT_SNAPIN_MANAGER or CCT_SCOPE
  297. // This case occcurs when the snapin is not an extension.
  298. pDataObj->SetData( mmcCookie, CCT_SCOPE, COOKIE_IS_ROOTNODE );
  299. }
  300. if ( SUCCEEDED ( hr ) ) {
  301. hr = pDataObj->QueryInterface(
  302. IID_IDataObject,
  303. reinterpret_cast<void**>(ppDataObject) );
  304. } else {
  305. if ( NULL != pDataObj ) {
  306. delete pDataObj;
  307. }
  308. *ppDataObject = NULL;
  309. }
  310. }
  311. } else {
  312. ASSERT ( FALSE );
  313. hr = E_POINTER;
  314. }
  315. return hr;
  316. } // end QueryDataObject()
  317. //---------------------------------------------------------------------------
  318. // This is where we provide strings for nodes in the scope pane.
  319. // MMC handles the root node string.
  320. //
  321. STDMETHODIMP
  322. CComponentData::GetDisplayInfo (
  323. LPSCOPEDATAITEM pItem ) // [in, out] Points to a SCOPEDATAITEM struct
  324. {
  325. HRESULT hr = S_OK;
  326. PSMNODE pTmp = NULL;
  327. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  328. ResourceStateManager rsm;
  329. if ( NULL != pItem ) {
  330. if( pItem->mask & SDI_STR ) {
  331. // Note: Text buffer allocated for each information type, so that
  332. // the buffer pointer is persistent for a single item (line in the result pane).
  333. // Set the name of the selected node
  334. pTmp = reinterpret_cast<PSMNODE>(pItem->lParam);
  335. if ( NULL != pTmp ) {
  336. m_strDisplayInfoName = pTmp->GetDisplayName();
  337. pItem->displayname = T2OLE ( m_strDisplayInfoName.GetBuffer( m_strDisplayInfoName.GetLength() ) );
  338. }
  339. }
  340. if( pItem->mask & SDI_IMAGE ) { // Looking for image
  341. pTmp = reinterpret_cast<PSMNODE>(pItem->lParam);
  342. if ( NULL != pTmp ) {
  343. if ( NULL != pTmp->CastToRootNode() ) {
  344. ASSERT((pItem->mask & (SDI_IMAGE | SDI_OPENIMAGE)) == 0);
  345. pItem->nImage = eBmpRootIcon;
  346. pItem->nOpenImage = eBmpRootIcon;
  347. hr = S_OK;
  348. } else if ( NULL != pTmp->CastToAlertService() ){
  349. pItem->nImage = eBmpAlertType;
  350. } else {
  351. pItem->nImage = eBmpLogType;
  352. }
  353. }
  354. }
  355. } else {
  356. ASSERT ( FALSE );
  357. hr = E_POINTER;
  358. }
  359. return hr;
  360. } // end GetDisplayInfo()
  361. //---------------------------------------------------------------------------
  362. //
  363. STDMETHODIMP
  364. CComponentData::CompareObjects (
  365. LPDATAOBJECT pDataObjectA, // [in] First data object to compare
  366. LPDATAOBJECT pDataObjectB ) // [in] Second data object to compare
  367. {
  368. HRESULT hr = S_OK;
  369. CDataObject *pdoA = NULL;
  370. CDataObject *pdoB = NULL;
  371. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  372. // At least one of these data objects is supposed to be ours, so one
  373. // of the extracted pointers should be non-NULL.
  374. pdoA = ExtractOwnDataObject( pDataObjectA );
  375. pdoB = ExtractOwnDataObject( pDataObjectB );
  376. ASSERT( pdoA || pdoB ); // Assert if we can't get any objects
  377. // If extraction failed for one of them, then that one is foreign and
  378. // can't be equal to the other one. (Or else ExtractOwnDataObject
  379. // returned NULL because it ran out of memory, but the most conservative
  380. // thing to do in that case is say they're not equal.)
  381. if( !pdoA || !pdoB ) {
  382. hr = S_FALSE;
  383. } else {
  384. if( pdoA->GetCookieType() != pdoB->GetCookieType() ) {
  385. // The cookie type could be COOKIE_IS_ROOTNODE or COOKIE_IS_MAINNODE
  386. // If they differ then the objects refer to different things.
  387. hr = S_FALSE;
  388. }
  389. }
  390. return hr;
  391. } // end CompareObjects()
  392. /////////////////////////////////////////////////////////////////////////////
  393. // Methods needed to support IComponentData
  394. //
  395. //---------------------------------------------------------------------------
  396. // Here is our chance to place things under the root node.
  397. //
  398. HRESULT
  399. CComponentData::OnExpand (
  400. LPDATAOBJECT pDataObject, // [in] Points to data object
  401. LPARAM arg, // [in] TRUE is we are expanding
  402. LPARAM param ) // [in] Points to the HSCOPEITEM
  403. {
  404. HRESULT hr = S_FALSE;
  405. HRESULT hrBootState= NOERROR;
  406. INT iBootState;
  407. GUID guidObjectType;
  408. CSmRootNode* pRootNode = NULL;
  409. CDataObject* pDO = NULL;
  410. SCOPEDATAITEM sdi;
  411. INT iResult;
  412. CString strTmp;
  413. CString strServerName;
  414. CString strMessage;
  415. CString strSysMessage;
  416. CString strTitle;
  417. CString strComputerName;
  418. ResourceStateManager rsm;
  419. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  420. ASSERT( NULL != m_ipConsoleNameSpace ); // Make sure we QI'ed for the interface
  421. ASSERT( NULL != pDataObject ); // Must have valid data object
  422. if ( NULL == pDataObject ) {
  423. ASSERT ( FALSE );
  424. hr = E_POINTER;
  425. } else {
  426. if( TRUE == arg ) {
  427. hr = ExtractObjectTypeGUID (pDataObject, &guidObjectType);
  428. ASSERT ( SUCCEEDED (hr) );
  429. if ( SUCCEEDED ( hr) ) {
  430. if ( IsMyComputerNodetype (guidObjectType) ) {
  431. // Is extension of Computer Management snapin
  432. hr = ExtractMachineName (
  433. pDataObject,
  434. strServerName);
  435. ASSERT ( SUCCEEDED ( hr ) );
  436. if ( SUCCEEDED (hr) ) {
  437. MFC_TRY
  438. pRootNode = new CSmRootNode;
  439. MFC_CATCH_HR
  440. if ( SUCCEEDED (hr) ) {
  441. pRootNode->SetMachineName ( strServerName );
  442. //
  443. // As an extension snapin, the log nodes should be added
  444. // beneath a "Performance Logs and Alerts" node. Insert that node,
  445. // and remember it as the root of the Performance Logs and Alerts namespace.
  446. //
  447. ZeroMemory(&sdi, sizeof sdi);
  448. sdi.mask = SDI_STR |
  449. SDI_PARAM |
  450. SDI_IMAGE |
  451. SDI_OPENIMAGE | // nOpenImage is valid
  452. SDI_PARENT;
  453. sdi.relativeID = (HSCOPEITEM)param;
  454. sdi.displayname = MMC_CALLBACK;
  455. sdi.nImage = eBmpRootIcon;
  456. sdi.nOpenImage = eBmpRootIcon;
  457. sdi.lParam = reinterpret_cast<LPARAM>(pRootNode);
  458. hr = m_ipConsoleNameSpace->InsertItem( &sdi );
  459. if (SUCCEEDED(hr)) {
  460. // Make this node the the root node
  461. pRootNode->SetScopeItemHandle ( (HSCOPEITEM)sdi.ID );
  462. pRootNode->SetParentScopeItemHandle( (HSCOPEITEM)param );
  463. pRootNode->SetExtension( TRUE );
  464. SetExtension( TRUE );
  465. m_listpRootNode.AddTail(pRootNode);
  466. } else {
  467. hr = E_UNEXPECTED;
  468. }
  469. } // Allocate CSmRootNode
  470. } // ExtractMachineName
  471. } else { // Not IsMyComputerNodeType
  472. pDO = ExtractOwnDataObject( pDataObject );
  473. if( NULL != pDO ) {
  474. // Make sure that what we are placing ourselves under is the root node
  475. // or the extension root node!
  476. if ( COOKIE_IS_ROOTNODE == pDO->GetCookieType() ) {
  477. pRootNode = (CSmRootNode*)pDO->GetCookie();
  478. if ( NULL == pRootNode ) {
  479. // If root node cookie is null, then the root node was created by
  480. // the snapin manager, and this is a standalone node.
  481. MFC_TRY
  482. pRootNode = new CSmRootNode;
  483. MFC_CATCH_HR
  484. if ( SUCCEEDED ( hr ) ) {
  485. // Cache the root node handle
  486. pRootNode->SetScopeItemHandle ( (HSCOPEITEM)param );
  487. pRootNode->SetExtension( FALSE );
  488. SetExtension( FALSE );
  489. // NOTE: No way to associate root node data directly with node.
  490. // Node only added once, so no need to check for duplicates.
  491. m_listpRootNode.AddTail(pRootNode);
  492. }
  493. } else {
  494. if ( m_listpRootNode.IsEmpty() ) {
  495. hr = S_FALSE;
  496. }
  497. }
  498. if ( SUCCEEDED ( hr ) && S_FALSE != hr ) {
  499. ASSERT ( NULL != pRootNode->CastToRootNode() );
  500. ASSERT ( NULL != pRootNode->GetScopeItemHandle() );
  501. ASSERT( CCT_SCOPE == pDO->GetContext() ); // Scope pane must be current context
  502. // For extensions, the root node was created in a previous call to this method.
  503. // The root was NOT expanded in that call.
  504. // For non-extensions, the root node is expanded in the same call as it is created.
  505. if ( !pRootNode->IsExpanded() ) {
  506. CWaitCursor WaitCursor;
  507. DWORD dwStatus;
  508. hr = S_OK;
  509. strServerName = pRootNode->GetMachineName();
  510. if ( !pRootNode->GetCounterLogService()->IsOpen() ) {
  511. dwStatus = pRootNode->GetCounterLogService()->Open ( strServerName );
  512. if ( ERROR_SUCCESS == dwStatus ) {
  513. // Place node for counter logs
  514. memset( &sdi, 0, sizeof(SCOPEDATAITEM) );
  515. sdi.mask = SDI_STR | // Displayname is valid
  516. SDI_PARAM | // lParam is valid
  517. SDI_IMAGE | // nImage is valid
  518. SDI_OPENIMAGE | // nOpenImage is valid
  519. SDI_CHILDREN | // Children count (0 vs. 1) is valid.
  520. SDI_PARENT;
  521. sdi.relativeID = pRootNode->GetScopeItemHandle(); // Performance Logs and Alerts root node
  522. sdi.nImage = eBmpLogType;
  523. sdi.nOpenImage = sdi.nImage; // select icon is same as non select
  524. sdi.displayname = MMC_CALLBACK;
  525. sdi.lParam = reinterpret_cast<LPARAM>(pRootNode->GetCounterLogService()); // The cookie
  526. sdi.cChildren = 0; // No children in the scope pane.
  527. hr = m_ipConsoleNameSpace->InsertItem( &sdi );
  528. } else {
  529. hr = E_FAIL;
  530. strComputerName = strServerName;
  531. if ( strComputerName.IsEmpty() )
  532. strComputerName.LoadString ( IDS_LOCAL );
  533. if ( SMCFG_NO_READ_ACCESS == dwStatus || SMCFG_NO_INSTALL_ACCESS == dwStatus ) {
  534. FormatSmLogCfgMessage (
  535. strMessage,
  536. m_hModule,
  537. dwStatus,
  538. (LPCTSTR)strComputerName);
  539. } else {
  540. FormatMessage (
  541. FORMAT_MESSAGE_FROM_SYSTEM,
  542. NULL,
  543. dwStatus,
  544. 0,
  545. strMessage.GetBufferSetLength( MAX_PATH ),
  546. MAX_PATH,
  547. NULL );
  548. strMessage.ReleaseBuffer();
  549. if ( strMessage.IsEmpty() ) {
  550. strMessage.Format ( _T("0x%08lX"), dwStatus );
  551. }
  552. }
  553. strTitle.LoadString ( IDS_PROJNAME );
  554. m_ipConsole->MessageBox(
  555. (LPCWSTR)strMessage,
  556. (LPCWSTR)strTitle,
  557. MB_OK | MB_ICONWARNING,
  558. &iResult
  559. );
  560. }
  561. }
  562. if ( SUCCEEDED(hr) && !pRootNode->GetTraceLogService()->IsOpen() ) {
  563. hrBootState = NOERROR;
  564. dwStatus = pRootNode->GetTraceLogService()->Open ( strServerName );
  565. hrBootState = pRootNode->GetTraceLogService()->GetProviders()->GetBootState ( iBootState );
  566. if ( ERROR_SUCCESS == dwStatus
  567. && SUCCEEDED ( hrBootState )
  568. && 0 == iBootState ) {
  569. // Place node for trace logs
  570. memset( &sdi, 0, sizeof(SCOPEDATAITEM) );
  571. sdi.mask = SDI_STR | // Displayname is valid
  572. SDI_PARAM | // lParam is valid
  573. SDI_IMAGE | // nImage is valid
  574. SDI_OPENIMAGE | // nOpenImage is valid
  575. SDI_CHILDREN | // Children count (0 vs. 1) is valid.
  576. SDI_PARENT;
  577. sdi.relativeID = pRootNode->GetScopeItemHandle(); // Performance Logs and Alerts root node
  578. sdi.nImage = eBmpLogType;
  579. sdi.nOpenImage = sdi.nImage; // select icon is same as non select
  580. sdi.displayname = MMC_CALLBACK;
  581. sdi.lParam = reinterpret_cast<LPARAM>(pRootNode->GetTraceLogService()); // The cookie
  582. sdi.cChildren = 0; // No children in the scope pane.
  583. hr = m_ipConsoleNameSpace->InsertItem( &sdi );
  584. } else {
  585. strComputerName = strServerName;
  586. if ( strComputerName.IsEmpty() )
  587. strComputerName.LoadString ( IDS_LOCAL );
  588. if ( SMCFG_NO_READ_ACCESS == dwStatus || SMCFG_NO_INSTALL_ACCESS == dwStatus ) {
  589. hr = E_FAIL;
  590. FormatSmLogCfgMessage (
  591. strMessage,
  592. m_hModule,
  593. dwStatus,
  594. (LPCTSTR)strComputerName);
  595. } else if ( ERROR_SUCCESS != dwStatus ) {
  596. hr = E_FAIL;
  597. FormatMessage (
  598. FORMAT_MESSAGE_FROM_SYSTEM,
  599. NULL,
  600. dwStatus,
  601. 0,
  602. strMessage.GetBufferSetLength( MAX_PATH ),
  603. MAX_PATH,
  604. NULL );
  605. strMessage.ReleaseBuffer();
  606. if ( strMessage.IsEmpty() ) {
  607. strMessage.Format ( _T("0x%08lX"), dwStatus );
  608. }
  609. } else if ( FAILED ( hrBootState ) ) {
  610. FormatSmLogCfgMessage (
  611. strMessage,
  612. m_hModule,
  613. SMCFG_UNABLE_OPEN_TRACESVC,
  614. (LPCTSTR)strComputerName);
  615. FormatMessage (
  616. FORMAT_MESSAGE_FROM_SYSTEM,
  617. NULL,
  618. hrBootState,
  619. 0,
  620. strSysMessage.GetBufferSetLength( MAX_PATH ),
  621. MAX_PATH,
  622. NULL );
  623. strSysMessage.ReleaseBuffer();
  624. if ( strSysMessage.IsEmpty() ) {
  625. strSysMessage.Format ( _T("0x%08lX"), hrBootState );
  626. }
  627. strMessage += strSysMessage;
  628. } else if ( 0 != iBootState ) {
  629. FormatSmLogCfgMessage (
  630. strMessage,
  631. m_hModule,
  632. SMCFG_SAFE_BOOT_STATE,
  633. (LPCTSTR)strComputerName);
  634. }
  635. strTitle.LoadString ( IDS_PROJNAME );
  636. m_ipConsole->MessageBox(
  637. (LPCWSTR)strMessage,
  638. (LPCWSTR)strTitle,
  639. MB_OK | MB_ICONWARNING,
  640. &iResult
  641. );
  642. }
  643. }
  644. if ( SUCCEEDED(hr) && !pRootNode->GetAlertService()->IsOpen() ) {
  645. dwStatus = pRootNode->GetAlertService()->Open ( strServerName );
  646. if ( ERROR_SUCCESS == dwStatus ) {
  647. // Place node for alerts
  648. memset( &sdi, 0, sizeof(SCOPEDATAITEM) );
  649. sdi.mask = SDI_STR | // Displayname is valid
  650. SDI_PARAM | // lParam is valid
  651. SDI_IMAGE | // nImage is valid
  652. SDI_OPENIMAGE | // nOpenImage is valid
  653. SDI_CHILDREN | // Children count (0 vs. 1) is valid.
  654. SDI_PARENT;
  655. sdi.relativeID = pRootNode->GetScopeItemHandle(); // Performance Logs and Alerts root node
  656. sdi.nImage = eBmpAlertType;
  657. sdi.nOpenImage = sdi.nImage; // select icon is same as non select
  658. sdi.displayname = MMC_CALLBACK;
  659. sdi.lParam = reinterpret_cast<LPARAM>(pRootNode->GetAlertService()); // The cookie
  660. sdi.cChildren = 0; // No children in the scope pane.
  661. hr = m_ipConsoleNameSpace->InsertItem( &sdi );
  662. } else {
  663. hr = E_FAIL;
  664. strComputerName = strServerName;
  665. if ( strComputerName.IsEmpty() )
  666. strComputerName.LoadString ( IDS_LOCAL );
  667. if ( SMCFG_NO_READ_ACCESS == dwStatus || SMCFG_NO_INSTALL_ACCESS == dwStatus ) {
  668. FormatSmLogCfgMessage (
  669. strMessage,
  670. m_hModule,
  671. dwStatus,
  672. (LPCTSTR)strComputerName);
  673. } else {
  674. FormatMessage (
  675. FORMAT_MESSAGE_FROM_SYSTEM,
  676. NULL,
  677. dwStatus,
  678. 0,
  679. strMessage.GetBufferSetLength( MAX_PATH ),
  680. MAX_PATH,
  681. NULL );
  682. strMessage.ReleaseBuffer();
  683. if ( strMessage.IsEmpty() ) {
  684. strMessage.Format ( _T("0x%08lX"), dwStatus );
  685. }
  686. }
  687. strTitle.LoadString ( IDS_PROJNAME );
  688. m_ipConsole->MessageBox(
  689. (LPCWSTR)strMessage,
  690. (LPCWSTR)strTitle,
  691. MB_OK | MB_ICONWARNING,
  692. &iResult
  693. );
  694. }
  695. }
  696. if ( SUCCEEDED( hr ) ) {
  697. pRootNode->SetExpanded( TRUE );
  698. hr = ProcessCommandLine( strServerName );
  699. }
  700. }
  701. } // Insert other scope nodes
  702. } // COOKIE_IS_ROOTNODE
  703. } else {
  704. // Unknown data object
  705. strMessage.LoadString ( IDS_ERRMSG_UNKDATAOBJ );
  706. m_ipConsole->MessageBox(
  707. (LPCWSTR)strMessage,
  708. L"CComponentData::OnExpand",
  709. MB_OK | MB_ICONERROR,
  710. &iResult
  711. );
  712. hr = E_UNEXPECTED;
  713. } // ExtractOwnDataObject
  714. } // IsMyComputerNodeType
  715. } // ExtractObjectTypeGUID
  716. } else { // FALSE == arg
  717. hr = S_FALSE;
  718. }
  719. } // Parameters are valid
  720. return hr;
  721. } // end OnExpand()
  722. //---------------------------------------------------------------------------
  723. // Remove and delete all children under the specified node.
  724. //
  725. HRESULT
  726. CComponentData::OnRemoveChildren (
  727. LPDATAOBJECT pDataObject, // [in] Points to data object of node whose children are to be deleted.
  728. LPARAM arg, // [in] HSCOPEITEM of node whose children are to be deleted;
  729. LPARAM /* param */ // [in] Not used
  730. )
  731. {
  732. HRESULT hr = S_FALSE;
  733. HRESULT hrLocal;
  734. CSmRootNode* pRootNode = NULL;
  735. CSmRootNode* pTestNode;
  736. POSITION Pos = m_listpRootNode.GetHeadPosition();
  737. HSCOPEITEM hParent = (HSCOPEITEM)arg;
  738. LPRESULTDATA pResultData;
  739. CDataObject* pDO = NULL;
  740. ASSERT ( !m_listpRootNode.IsEmpty() );
  741. if ( NULL == pDataObject ) {
  742. hr = E_POINTER;
  743. } else {
  744. while ( Pos != NULL) {
  745. pTestNode = m_listpRootNode.GetNext( Pos );
  746. // For standalone, the root node's parent handle is NULL.
  747. if ( hParent == pTestNode->GetScopeItemHandle()
  748. || ( hParent == pTestNode->GetParentScopeItemHandle()
  749. && pTestNode->IsExtension() ) ) {
  750. pRootNode = pTestNode;
  751. break;
  752. }
  753. }
  754. // Optimization - If root node, remove all of the result items here.
  755. if ( pRootNode ) {
  756. pResultData = GetResultData ();
  757. ASSERT (pResultData);
  758. if ( pResultData ) {
  759. hrLocal = pResultData->DeleteAllRsltItems ();
  760. }
  761. }
  762. // For standalone, we didn't create the root node, so don't delete it.
  763. // For extension, the parent of the root node is passed so the root node gets deleted.
  764. hrLocal = m_ipConsoleNameSpace->DeleteItem ( hParent, FALSE );
  765. if ( pRootNode ) {
  766. // Close all queries and the connection to the log service.
  767. m_listpRootNode.RemoveAt( m_listpRootNode.Find ( pRootNode ) );
  768. pRootNode->Destroy();
  769. delete pRootNode;
  770. hr = S_OK;
  771. } else {
  772. // Close all queries and the connection to the log service for this service type.
  773. pDO = ExtractOwnDataObject( pDataObject );
  774. if ( NULL != pDO ) {
  775. if ( NULL != pDO->GetCookie() ) {
  776. if ( COOKIE_IS_COUNTERMAINNODE == pDO->GetCookieType() ) {
  777. CSmCounterLogService* pService = (CSmCounterLogService*)pDO->GetCookie();
  778. pService->Close();
  779. } else if ( COOKIE_IS_TRACEMAINNODE == pDO->GetCookieType() ) {
  780. CSmTraceLogService* pService = (CSmTraceLogService*)pDO->GetCookie();
  781. pService->Close();
  782. } else if ( COOKIE_IS_ALERTMAINNODE == pDO->GetCookieType() ) {
  783. CSmAlertService* pService = (CSmAlertService*)pDO->GetCookie();
  784. pService->Close();
  785. } else {
  786. ASSERT ( FALSE );
  787. }
  788. }
  789. } else {
  790. hr = E_UNEXPECTED;
  791. }
  792. }
  793. }
  794. return hr;
  795. }
  796. BOOL CComponentData::IsMyComputerNodetype (GUID& refguid)
  797. {
  798. return (::IsEqualGUID (refguid, g_guidSystemTools));
  799. }
  800. BOOL
  801. CComponentData::IsScopeNode
  802. (
  803. MMC_COOKIE mmcCookie
  804. )
  805. {
  806. BOOL bIsScopeNode = FALSE;
  807. CSmRootNode* pRootNode = NULL;
  808. POSITION Pos = m_listpRootNode.GetHeadPosition();
  809. while ( Pos != NULL) {
  810. pRootNode = m_listpRootNode.GetNext( Pos );
  811. if ( mmcCookie == (MMC_COOKIE)pRootNode ) {
  812. bIsScopeNode = TRUE;
  813. break;
  814. }
  815. if ( !bIsScopeNode ) {
  816. bIsScopeNode = IsLogService ( mmcCookie );
  817. }
  818. }
  819. return bIsScopeNode;
  820. }
  821. BOOL
  822. CComponentData::IsLogService (
  823. MMC_COOKIE mmcCookie )
  824. {
  825. CSmRootNode* pRootNode = NULL;
  826. POSITION Pos = m_listpRootNode.GetHeadPosition();
  827. BOOL bReturn = FALSE;
  828. while ( Pos != NULL) {
  829. pRootNode = m_listpRootNode.GetNext( Pos );
  830. bReturn = pRootNode->IsLogService( mmcCookie );
  831. if ( bReturn )
  832. break;
  833. }
  834. return bReturn;
  835. }
  836. BOOL
  837. CComponentData::IsAlertService ( MMC_COOKIE mmcCookie)
  838. {
  839. CSmRootNode* pRootNode = NULL;
  840. POSITION Pos = m_listpRootNode.GetHeadPosition();
  841. BOOL bReturn = FALSE;
  842. while ( Pos != NULL) {
  843. pRootNode = m_listpRootNode.GetNext( Pos );
  844. bReturn = pRootNode->IsAlertService( mmcCookie );
  845. if ( bReturn )
  846. break;
  847. }
  848. return bReturn;
  849. }
  850. BOOL
  851. CComponentData::IsLogQuery (
  852. MMC_COOKIE mmcCookie )
  853. {
  854. CSmRootNode* pRootNode = NULL;
  855. POSITION Pos = m_listpRootNode.GetHeadPosition();
  856. BOOL bReturn = FALSE;
  857. while ( Pos != NULL ) {
  858. pRootNode = m_listpRootNode.GetNext ( Pos );
  859. bReturn = pRootNode->IsLogQuery ( mmcCookie );
  860. if ( bReturn )
  861. break;
  862. }
  863. return bReturn;
  864. }
  865. BOOL
  866. CComponentData::IsRunningQuery (
  867. PSLQUERY pQuery )
  868. {
  869. return pQuery->IsRunning();
  870. }
  871. ///////////////////////////////////////////////////////////////////////////////
  872. /// IExtendPropertySheet
  873. STDMETHODIMP
  874. CComponentData::QueryPagesFor ( LPDATAOBJECT pDataObject )
  875. {
  876. HRESULT hr = S_FALSE;
  877. CDataObject *pDO = NULL;
  878. if (NULL == pDataObject) {
  879. ASSERT(FALSE);
  880. hr = E_POINTER;
  881. } else {
  882. pDO = ExtractOwnDataObject( pDataObject );
  883. if ( NULL == pDO ) {
  884. ASSERT(FALSE);
  885. hr = E_UNEXPECTED;
  886. } else {
  887. if ( NULL != pDO->GetCookie() ) {
  888. hr = m_ipPrshtProvider->FindPropertySheet((MMC_COOKIE)pDO->GetCookie(), NULL, pDataObject);
  889. } else {
  890. hr = S_FALSE;
  891. }
  892. }
  893. }
  894. return hr;
  895. } // CComponentData::QueryPagesFor()
  896. //---------------------------------------------------------------------------
  897. // Implement some context menu items
  898. //
  899. STDMETHODIMP
  900. CComponentData::AddMenuItems (
  901. LPDATAOBJECT pDataObject, // [in] Points to data object
  902. LPCONTEXTMENUCALLBACK pCallbackUnknown, // [in] Points to callback function
  903. long* pInsertionAllowed ) // [in,out] Insertion flags
  904. {
  905. HRESULT hr = S_OK;
  906. BOOL bIsLogSvc = FALSE;
  907. CDataObject* pDO = NULL;
  908. PSLSVC pLogService;
  909. static CONTEXTMENUITEM ctxMenu[1];
  910. CString strTemp1, strTemp2, strTemp3, strTemp4;
  911. ResourceStateManager rsm;
  912. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  913. if ( NULL == pDataObject ) {
  914. ASSERT ( FALSE );
  915. hr = E_POINTER;
  916. } else if ( NULL == pCallbackUnknown ) {
  917. ASSERT ( FALSE );
  918. hr = E_POINTER;
  919. } else {
  920. pDO = ExtractOwnDataObject( pDataObject );
  921. if ( NULL == pDO ) {
  922. ASSERT ( FALSE );
  923. hr = E_UNEXPECTED;
  924. }
  925. }
  926. // Only add menu items when we are allowed to.
  927. if ( SUCCEEDED ( hr ) ) {
  928. if ( ( COOKIE_IS_COUNTERMAINNODE == pDO->GetCookieType() )
  929. || ( COOKIE_IS_TRACEMAINNODE == pDO->GetCookieType() )
  930. || ( COOKIE_IS_ALERTMAINNODE == pDO->GetCookieType() ) )
  931. {
  932. if( CCM_INSERTIONALLOWED_NEW & *pInsertionAllowed ) {
  933. // Add "New Query..." context menu item
  934. bIsLogSvc = IsLogService ( pDO->GetCookie() );
  935. if (bIsLogSvc) {
  936. pLogService = (PSLSVC)pDO->GetCookie();
  937. ZeroMemory ( &ctxMenu, sizeof ctxMenu );
  938. MFC_TRY
  939. if ( NULL != pLogService->CastToCounterLogService() ) {
  940. strTemp1.LoadString( IDS_MMC_MENU_NEW_PERF_LOG );
  941. strTemp2.LoadString( IDS_MMC_STATUS_NEW_PERF_LOG );
  942. strTemp3.LoadString( IDS_MMC_MENU_PERF_LOG_FROM );
  943. strTemp4.LoadString( IDS_MMC_STATUS_PERF_LOG_FROM );
  944. } else if ( pLogService->CastToTraceLogService() ) {
  945. strTemp1.LoadString( IDS_MMC_MENU_NEW_TRACE_LOG );
  946. strTemp2.LoadString( IDS_MMC_STATUS_NEW_TRACE_LOG );
  947. strTemp3.LoadString( IDS_MMC_MENU_TRACE_LOG_FROM );
  948. strTemp4.LoadString( IDS_MMC_STATUS_TRACE_LOG_FROM );
  949. } else if ( pLogService->CastToAlertService() ) {
  950. strTemp1.LoadString( IDS_MMC_MENU_NEW_ALERT );
  951. strTemp2.LoadString( IDS_MMC_STATUS_NEW_ALERT );
  952. strTemp3.LoadString( IDS_MMC_MENU_ALERT_FROM );
  953. strTemp4.LoadString( IDS_MMC_STATUS_ALERT_FROM );
  954. } else {
  955. ::MessageBox( NULL,
  956. _T("Bad Cookie"),
  957. _T("CComponent::AddMenuItems"),
  958. MB_OK | MB_ICONERROR
  959. );
  960. hr = E_OUTOFMEMORY;
  961. }
  962. MFC_CATCH_HR_RETURN
  963. if ( SUCCEEDED( hr ) ) {
  964. // Create new...
  965. ctxMenu[0].strName = T2OLE(const_cast<LPTSTR>((LPCTSTR)strTemp1));
  966. ctxMenu[0].strStatusBarText = T2OLE(const_cast<LPTSTR>((LPCTSTR)strTemp2));
  967. ctxMenu[0].lCommandID = IDM_NEW_QUERY;
  968. ctxMenu[0].lInsertionPointID = CCM_INSERTIONPOINTID_PRIMARY_TOP;
  969. ctxMenu[0].fFlags = MF_ENABLED;
  970. ctxMenu[0].fSpecialFlags = 0;
  971. hr = pCallbackUnknown->AddItem( &ctxMenu[0] );
  972. if ( SUCCEEDED(hr) ) {
  973. // Create from...
  974. ctxMenu[0].strName = T2OLE(const_cast<LPTSTR>((LPCTSTR)strTemp3));
  975. ctxMenu[0].strStatusBarText = T2OLE(const_cast<LPTSTR>((LPCTSTR)strTemp4));
  976. ctxMenu[0].lCommandID = IDM_NEW_QUERY_FROM;
  977. ctxMenu[0].lInsertionPointID = CCM_INSERTIONPOINTID_PRIMARY_TOP;
  978. ctxMenu[0].fFlags = MF_ENABLED;
  979. ctxMenu[0].fSpecialFlags = 0;
  980. hr = pCallbackUnknown->AddItem( &ctxMenu[0] );
  981. }
  982. }
  983. }
  984. }
  985. }
  986. }
  987. return hr;
  988. } // end AddMenuItems()
  989. //---------------------------------------------------------------------------
  990. // Implement the command method so we can handle notifications
  991. // from our Context menu extensions.
  992. //
  993. STDMETHODIMP
  994. CComponentData::Command (
  995. long nCommandID, // [in] Command to handle
  996. LPDATAOBJECT pDataObject // [in] Points to data object, pass through
  997. )
  998. {
  999. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1000. HRESULT hr = S_OK;
  1001. switch( nCommandID ) {
  1002. case IDM_NEW_QUERY:
  1003. CreateNewLogQuery( pDataObject );
  1004. break;
  1005. case IDM_NEW_QUERY_FROM:
  1006. CreateLogQueryFrom( pDataObject );
  1007. break;
  1008. default:
  1009. hr = S_FALSE;
  1010. }
  1011. return hr;
  1012. } // end Command()
  1013. STDMETHODIMP
  1014. CComponentData::CreatePropertyPages(
  1015. LPPROPERTYSHEETCALLBACK pCallBack,
  1016. LONG_PTR /* handle */, // This handle must be saved in the property
  1017. // page object to notify the parent when modified
  1018. LPDATAOBJECT pDataObject)
  1019. {
  1020. if (NULL == pCallBack || NULL == pDataObject)
  1021. {
  1022. ASSERT(FALSE);
  1023. return E_POINTER;
  1024. }
  1025. return S_FALSE;
  1026. } // CComponentData::CreatePropertyPages()
  1027. LPCTSTR
  1028. CComponentData::GetConceptsHTMLHelpFileName()
  1029. {
  1030. return CGlobalString::m_cszConceptsHTMLHelpFileName;
  1031. }
  1032. LPCTSTR
  1033. CComponentData::GetSnapinHTMLHelpFileName()
  1034. {
  1035. return CGlobalString::m_cszSnapinHTMLHelpFileName;
  1036. }
  1037. LPCTSTR
  1038. CComponentData::GetHTMLHelpTopic()
  1039. {
  1040. return CGlobalString::m_cszHTMLHelpTopic;
  1041. }
  1042. const CString&
  1043. CComponentData::GetContextHelpFilePath()
  1044. {
  1045. LPTSTR lpszBuffer;
  1046. UINT nLen;
  1047. if ( m_strContextHelpFilePath.IsEmpty() ) {
  1048. MFC_TRY
  1049. if ( m_strWindowsDirectory.IsEmpty() ) {
  1050. lpszBuffer = m_strWindowsDirectory.GetBuffer(2*MAX_PATH);
  1051. nLen = ::GetWindowsDirectory(lpszBuffer, 2*MAX_PATH);
  1052. m_strWindowsDirectory.ReleaseBuffer();
  1053. }
  1054. if ( !m_strWindowsDirectory.IsEmpty() )
  1055. {
  1056. m_strContextHelpFilePath = m_strWindowsDirectory + CGlobalString::m_cszContextHelpFileName;
  1057. }
  1058. MFC_CATCH_MINIMUM;
  1059. }
  1060. return m_strContextHelpFilePath;
  1061. }
  1062. // CComponentData::GetHelpTopic()
  1063. HRESULT
  1064. CComponentData::GetHelpTopic (
  1065. LPOLESTR* lpCompiledHelpFile )
  1066. {
  1067. HRESULT hr = E_FAIL;
  1068. LPCWSTR lpszHelpFileName;
  1069. CString strHelpFilePath;
  1070. LPTSTR lpszBuffer;
  1071. UINT nLen;
  1072. UINT nBytes;
  1073. if ( NULL == lpCompiledHelpFile) {
  1074. hr = E_POINTER;
  1075. } else {
  1076. *lpCompiledHelpFile = NULL;
  1077. MFC_TRY
  1078. lpszHelpFileName = GetSnapinHTMLHelpFileName();
  1079. if ( NULL == lpszHelpFileName) {
  1080. hr = E_UNEXPECTED;
  1081. } else {
  1082. lpszBuffer = strHelpFilePath.GetBuffer(2*MAX_PATH);
  1083. nLen = ::GetWindowsDirectory(lpszBuffer, 2*MAX_PATH);
  1084. if ( 0 == nLen ) {
  1085. hr = E_UNEXPECTED;
  1086. } else {
  1087. wcscpy(&lpszBuffer[nLen], lpszHelpFileName);
  1088. nBytes = (lstrlen(lpszBuffer)+1) * sizeof(WCHAR);
  1089. *lpCompiledHelpFile = (LPOLESTR)::CoTaskMemAlloc(nBytes);
  1090. if ( NULL == *lpCompiledHelpFile ) {
  1091. hr = E_OUTOFMEMORY;
  1092. } else {
  1093. memcpy(*lpCompiledHelpFile, (LPCWSTR)strHelpFilePath, nBytes);
  1094. hr = S_OK;
  1095. }
  1096. }
  1097. strHelpFilePath.ReleaseBuffer();
  1098. }
  1099. MFC_CATCH_HR
  1100. }
  1101. return hr;
  1102. }
  1103. LPRESULTDATA
  1104. CComponentData::GetResultData()
  1105. {
  1106. if ( !m_ipResultData )
  1107. {
  1108. if ( m_ipConsole )
  1109. {
  1110. HRESULT hResult = m_ipConsole->QueryInterface(IID_IResultData, (void**)&m_ipResultData);
  1111. ASSERT (SUCCEEDED (hResult));
  1112. }
  1113. }
  1114. return m_ipResultData;
  1115. }
  1116. HRESULT
  1117. CComponentData::ProcessCommandLine ( CString& rstrMachineName )
  1118. {
  1119. HRESULT hr = S_OK;
  1120. LPCWSTR pszNext = NULL;
  1121. LPWSTR* pszArgList = NULL;
  1122. INT iNumArgs;
  1123. INT iArgIndex;
  1124. LPWSTR pszNextArg = NULL;
  1125. LPWSTR pszThisArg = NULL;
  1126. TCHAR szTemp[MAX_PATH];
  1127. LPTSTR pszToken = NULL;
  1128. TCHAR szFileName[MAX_PATH];
  1129. CString strSettings;
  1130. CString strWmi;
  1131. // Process only for local node.
  1132. if ( rstrMachineName.IsEmpty() ) {
  1133. pszNext = GetCommandLineW();
  1134. pszArgList = CommandLineToArgvW ( pszNext, &iNumArgs );
  1135. }
  1136. if ( NULL != pszArgList ) {
  1137. // This code assumes that UNICODE is defined. That is, TCHAR is the
  1138. // same size as WCHAR.
  1139. // Todo: Define _T constants as L constants
  1140. // Todo: Filename, etc. as WCHAR
  1141. ASSERT ( sizeof(TCHAR) == sizeof (WCHAR) );
  1142. for ( iArgIndex = 0; iArgIndex < iNumArgs; iArgIndex++ ) {
  1143. pszNextArg = (LPWSTR)pszArgList[iArgIndex];
  1144. pszThisArg = pszNextArg;
  1145. while (pszThisArg ) {
  1146. if (0 == *pszThisArg) {
  1147. break;
  1148. }
  1149. if ( *pszThisArg++ == _T('/') ) { // argument found
  1150. lstrcpyn ( szTemp, pszThisArg, min(lstrlen(pszThisArg)+1, MAX_PATH ) );
  1151. pszToken = _tcstok ( szTemp, _T("/ =\"") );
  1152. MFC_TRY
  1153. strSettings.LoadString( IDS_CMDARG_SYSMONLOG_SETTINGS );
  1154. strWmi.LoadString(IDS_CMDARG_SYSMONLOG_WMI);
  1155. MFC_CATCH_MINIMUM;
  1156. if ( !strSettings.IsEmpty() && !strWmi.IsEmpty() ) {
  1157. if ( 0 == strSettings.CompareNoCase ( pszToken ) ) {
  1158. // Strip the initial non-token characters for string comparison.
  1159. pszThisArg = _tcsspnp ( pszNextArg, _T("/ =\"") );
  1160. if ( NULL != pszThisArg ) {
  1161. if ( 0 == strSettings.CompareNoCase ( pszThisArg ) ) {
  1162. // Get the next argument (the file name)
  1163. iArgIndex++;
  1164. pszNextArg = (LPWSTR)pszArgList[iArgIndex];
  1165. pszThisArg = pszNextArg;
  1166. } else {
  1167. // File was created by Windows 2000 perfmon5.exe,
  1168. // so file name is part of the arg.
  1169. ZeroMemory ( szFileName, sizeof ( szFileName ) );
  1170. pszThisArg += strSettings.GetLength();
  1171. lstrcpyn ( szFileName, pszThisArg, min(lstrlen(pszThisArg)+1, MAX_PATH ) );
  1172. pszThisArg = _tcstok ( szFileName, _T("=\"") );
  1173. }
  1174. hr = LoadFromFile( pszThisArg );
  1175. }
  1176. } else if ( 0 == strWmi.CompareNoCase ( pszToken ) ) {
  1177. g_dwRealTimeQuery = DATA_SOURCE_WBEM;
  1178. }
  1179. }
  1180. }
  1181. }
  1182. }
  1183. }
  1184. if ( NULL != pszArgList ) {
  1185. GlobalFree ( pszArgList );
  1186. }
  1187. return hr;
  1188. }
  1189. HRESULT
  1190. CComponentData::LoadFromFile ( LPTSTR pszFileName )
  1191. {
  1192. HRESULT hr = S_OK;
  1193. TCHAR szLocalName [MAX_PATH];
  1194. LPTSTR pFileNameStart;
  1195. HANDLE hFindFile;
  1196. WIN32_FIND_DATA FindFileInfo;
  1197. INT iNameOffset;
  1198. lstrcpy ( szLocalName, pszFileName );
  1199. pFileNameStart = ExtractFileName (szLocalName) ;
  1200. iNameOffset = (INT)(pFileNameStart - szLocalName);
  1201. // convert short filename to long NTFS filename if necessary
  1202. hFindFile = FindFirstFile ( szLocalName, &FindFileInfo) ;
  1203. if (hFindFile && hFindFile != INVALID_HANDLE_VALUE) {
  1204. HANDLE hOpenFile;
  1205. // append the file name back to the path name
  1206. lstrcpy (&szLocalName[iNameOffset], FindFileInfo.cFileName) ;
  1207. FindClose (hFindFile) ;
  1208. // Open the file
  1209. hOpenFile = CreateFile (
  1210. szLocalName,
  1211. GENERIC_READ,
  1212. 0, // Not shared
  1213. NULL, // Security attributes
  1214. OPEN_EXISTING,
  1215. FILE_ATTRIBUTE_NORMAL,
  1216. NULL );
  1217. if ( hOpenFile && hOpenFile != INVALID_HANDLE_VALUE ) {
  1218. DWORD dwFileSize;
  1219. DWORD dwFileSizeHigh;
  1220. LPTSTR pszFirstData = NULL;
  1221. HRESULT hr = S_OK;
  1222. // Read the file contents into a memory buffer.
  1223. dwFileSize = GetFileSize ( hOpenFile, &dwFileSizeHigh );
  1224. ASSERT ( 0 == dwFileSizeHigh );
  1225. MFC_TRY
  1226. pszFirstData = new TCHAR[(dwFileSize + sizeof(TCHAR))/sizeof(TCHAR)];
  1227. MFC_CATCH_HR
  1228. if ( NULL != pszFirstData ) {
  1229. BOOL bAtLeastOneSysmonObjectRead = FALSE;
  1230. if ( FileRead ( hOpenFile, pszFirstData, dwFileSize ) ) {
  1231. LPTSTR pszCurrentObject = NULL;
  1232. LPTSTR pszNextObject = NULL;
  1233. pszCurrentObject = pszFirstData;
  1234. while ( SUCCEEDED ( hr ) && NULL != pszCurrentObject ) {
  1235. CImpIPropertyBag* pPropBag = NULL;
  1236. // Write contents to a property bag
  1237. MFC_TRY
  1238. pPropBag = new CImpIPropertyBag;
  1239. MFC_CATCH_HR
  1240. if ( NULL != pPropBag ) {
  1241. DWORD dwStatus = pPropBag->LoadData( pszCurrentObject, &pszNextObject );
  1242. hr = HRESULT_FROM_WIN32( dwStatus );
  1243. if ( SUCCEEDED ( hr ) ) {
  1244. PSLSVC pSvc = NULL;
  1245. PSLQUERY pQuery = NULL;
  1246. DWORD dwLogType;
  1247. LPTSTR pszQueryName = NULL;
  1248. DWORD dwBufSize = 0;
  1249. CSmRootNode* pRoot = NULL;
  1250. CString strQueryName;
  1251. bAtLeastOneSysmonObjectRead = TRUE;
  1252. // Get root node
  1253. ASSERT ( !m_listpRootNode.IsEmpty() );
  1254. pRoot = m_listpRootNode.GetHead();
  1255. // Determine log type from property bag. Default to counter log.
  1256. hr = CSmLogQuery::DwordFromPropertyBag (
  1257. pPropBag,
  1258. NULL,
  1259. IDS_HTML_LOG_TYPE,
  1260. SLQ_COUNTER_LOG,
  1261. dwLogType);
  1262. // Get service pointer and log/alert name based on log type.
  1263. if ( SLQ_ALERT == dwLogType ) {
  1264. pSvc = pRoot->GetAlertService();
  1265. hr = CSmLogQuery::StringFromPropertyBag (
  1266. pPropBag,
  1267. NULL,
  1268. IDS_HTML_ALERT_NAME,
  1269. _T(""),
  1270. &pszQueryName,
  1271. &dwBufSize );
  1272. if ( NULL == pszQueryName ) {
  1273. hr = CSmLogQuery::StringFromPropertyBag (
  1274. pPropBag,
  1275. NULL,
  1276. IDS_HTML_LOG_NAME,
  1277. _T(""),
  1278. &pszQueryName,
  1279. &dwBufSize );
  1280. }
  1281. } else {
  1282. if ( SLQ_TRACE_LOG == dwLogType ) {
  1283. pSvc = pRoot->GetTraceLogService();
  1284. } else {
  1285. // Default to counter log service
  1286. pSvc = pRoot->GetCounterLogService();
  1287. }
  1288. hr = CSmLogQuery::StringFromPropertyBag (
  1289. pPropBag,
  1290. NULL,
  1291. IDS_HTML_LOG_NAME,
  1292. _T(""),
  1293. &pszQueryName,
  1294. &dwBufSize );
  1295. if ( NULL == pszQueryName ) {
  1296. hr = CSmLogQuery::StringFromPropertyBag (
  1297. pPropBag,
  1298. NULL,
  1299. IDS_HTML_ALERT_NAME,
  1300. _T(""),
  1301. &pszQueryName,
  1302. &dwBufSize );
  1303. }
  1304. }
  1305. strQueryName = pszQueryName;
  1306. delete pszQueryName;
  1307. while ( NULL == pQuery ) {
  1308. if ( !strQueryName.IsEmpty() ) {
  1309. pQuery = pSvc->CreateQuery ( strQueryName );
  1310. if ( NULL != pQuery ) {
  1311. BOOL bRegistryUpdated;
  1312. pQuery->LoadFromPropertyBag ( pPropBag, NULL );
  1313. dwStatus = pQuery->UpdateService ( bRegistryUpdated );
  1314. break;
  1315. } else {
  1316. dwStatus = GetLastError();
  1317. }
  1318. if ( ERROR_SUCCESS != dwStatus ) {
  1319. INT iResult;
  1320. CString strMessage;
  1321. CString csTitle;
  1322. BOOL bBreakImmediately = TRUE;
  1323. if ( SMCFG_NO_MODIFY_ACCESS == dwStatus ) {
  1324. CString strMachineName;
  1325. strMachineName = pSvc->GetMachineDisplayName ();
  1326. FormatSmLogCfgMessage (
  1327. strMessage,
  1328. m_hModule,
  1329. SMCFG_NO_MODIFY_ACCESS,
  1330. (LPCTSTR)strMachineName);
  1331. } else if ( SMCFG_DUP_QUERY_NAME == dwStatus ) {
  1332. FormatSmLogCfgMessage (
  1333. strMessage,
  1334. m_hModule,
  1335. SMCFG_DUP_QUERY_NAME,
  1336. (LPCTSTR)strQueryName);
  1337. bBreakImmediately = FALSE;
  1338. } else {
  1339. CString strSysMessage;
  1340. FormatSmLogCfgMessage (
  1341. strMessage,
  1342. m_hModule,
  1343. SMCFG_SYSTEM_MESSAGE,
  1344. (LPCTSTR)strQueryName);
  1345. FormatMessage (
  1346. FORMAT_MESSAGE_FROM_SYSTEM,
  1347. NULL,
  1348. dwStatus,
  1349. 0,
  1350. strSysMessage.GetBufferSetLength( MAX_PATH ),
  1351. MAX_PATH,
  1352. NULL );
  1353. strSysMessage.ReleaseBuffer();
  1354. if ( strSysMessage.IsEmpty() ) {
  1355. strSysMessage.Format ( _T("0x%08lX"), dwStatus );
  1356. }
  1357. strMessage += strSysMessage;
  1358. }
  1359. csTitle.LoadString ( IDS_PROJNAME );
  1360. hr = m_ipConsole->MessageBox(
  1361. (LPCWSTR)strMessage,
  1362. (LPCWSTR)csTitle,
  1363. MB_OK | MB_ICONERROR,
  1364. &iResult
  1365. );
  1366. if ( bBreakImmediately ) {
  1367. break;
  1368. }
  1369. }
  1370. }
  1371. if ( NULL == pQuery ) {
  1372. CNewQueryDlg cNewDlg(NULL, ((SLQ_ALERT == dwLogType) ? FALSE : TRUE));
  1373. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1374. cNewDlg.SetContextHelpFilePath( GetContextHelpFilePath() );
  1375. cNewDlg.m_strName = strQueryName;
  1376. if ( IDOK == cNewDlg.DoModal() ) {
  1377. strQueryName = cNewDlg.m_strName;
  1378. } else {
  1379. break;
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. pszCurrentObject = pszNextObject;
  1386. delete pPropBag;
  1387. } // end while
  1388. }
  1389. delete pszFirstData;
  1390. // Message to the user if no queries Read.
  1391. if ( !bAtLeastOneSysmonObjectRead ) {
  1392. CString strMessage;
  1393. CString strTitle;
  1394. INT iResult;
  1395. FormatSmLogCfgMessage (
  1396. strMessage,
  1397. m_hModule,
  1398. SMCFG_NO_HTML_SYSMON_OBJECT );
  1399. strTitle.LoadString ( IDS_PROJNAME );
  1400. m_ipConsole->MessageBox (
  1401. strMessage,
  1402. strTitle,
  1403. MB_OK | MB_ICONERROR,
  1404. &iResult );
  1405. }
  1406. } else {
  1407. hr = E_OUTOFMEMORY;
  1408. }
  1409. CloseHandle ( hOpenFile );
  1410. }
  1411. }
  1412. return hr;
  1413. }
  1414. HRESULT
  1415. CComponentData::InitPropertySheet (
  1416. CSmLogQuery* pQuery,
  1417. MMC_COOKIE mmcCookie,
  1418. LONG_PTR handle,
  1419. CPropertySheet& rcpsMain )
  1420. {
  1421. CCountersProperty *pPage1 = NULL;
  1422. CFilesProperty *pPage2 = NULL;
  1423. CScheduleProperty *pPage3 = NULL;
  1424. CTraceProperty *pPage4 = NULL;
  1425. CProvidersProperty *pPage5 = NULL;
  1426. CAlertActionProp *pPage6 = NULL;
  1427. CAlertGenProp *pPage7 = NULL;
  1428. HRESULT hr = NOERROR;
  1429. ASSERT ( NULL != pQuery );
  1430. rcpsMain.SetTitle (pQuery->GetLogName());
  1431. MFC_TRY
  1432. if ( SLQ_ALERT == pQuery->GetLogType() ) {
  1433. pPage7 = new CAlertGenProp (mmcCookie, handle);
  1434. pPage6 = new CAlertActionProp (mmcCookie, handle);
  1435. pPage3 = new CScheduleProperty (mmcCookie, handle, NULL);
  1436. if ( NULL != pPage7 ) {
  1437. pPage7->SetContextHelpFilePath( GetContextHelpFilePath() );
  1438. rcpsMain.AddPage (pPage7);
  1439. }
  1440. if ( NULL != pPage6 ) {
  1441. pPage6->SetContextHelpFilePath( GetContextHelpFilePath() );
  1442. rcpsMain.AddPage (pPage6);
  1443. }
  1444. if ( NULL != pPage3 ) {
  1445. pPage3->SetContextHelpFilePath( GetContextHelpFilePath() );
  1446. rcpsMain.AddPage (pPage3);
  1447. }
  1448. } else {
  1449. if ( SLQ_TRACE_LOG == pQuery->GetLogType() ) {
  1450. CWaitCursor WaitCursor;
  1451. // Connect to the server before creating the dialog
  1452. // so that the wait cursor can be used consistently.
  1453. // Sync the providers here so that the WMI calls are consistently
  1454. // from a single thread.
  1455. ASSERT ( NULL != pQuery->CastToTraceLogQuery() );
  1456. hr = (pQuery->CastToTraceLogQuery())->SyncGenProviders();
  1457. if ( SUCCEEDED ( hr ) ) {
  1458. pPage5 = new CProvidersProperty(mmcCookie, handle);
  1459. if ( NULL != pPage5 )
  1460. pPage5->SetContextHelpFilePath( GetContextHelpFilePath() );
  1461. rcpsMain.AddPage (pPage5);
  1462. } else {
  1463. CString strMachineName;
  1464. CString strLogName;
  1465. pQuery->GetMachineDisplayName( strMachineName );
  1466. strLogName = pQuery->GetLogName();
  1467. HandleTraceConnectError (
  1468. hr,
  1469. strLogName,
  1470. strMachineName );
  1471. }
  1472. } else {
  1473. pPage1 = new CCountersProperty ( mmcCookie, handle );
  1474. if ( NULL != pPage1 ) {
  1475. pPage1->SetContextHelpFilePath( GetContextHelpFilePath() );
  1476. rcpsMain.AddPage (pPage1);
  1477. }
  1478. }
  1479. if ( SUCCEEDED ( hr ) ) {
  1480. pPage2 = new CFilesProperty(mmcCookie, handle);
  1481. if ( NULL != pPage2 ) {
  1482. pPage2->SetContextHelpFilePath( GetContextHelpFilePath() );
  1483. rcpsMain.AddPage (pPage2);
  1484. }
  1485. pPage3 = new CScheduleProperty(mmcCookie, handle, NULL);
  1486. if ( NULL != pPage3 ) {
  1487. pPage3->SetContextHelpFilePath( GetContextHelpFilePath() );
  1488. rcpsMain.AddPage (pPage3);
  1489. }
  1490. if ( SLQ_TRACE_LOG == pQuery->GetLogType() ) {
  1491. pPage4 = new CTraceProperty(mmcCookie, handle);
  1492. if ( NULL != pPage4 ) {
  1493. pPage4->SetContextHelpFilePath( GetContextHelpFilePath() );
  1494. rcpsMain.AddPage (pPage4);
  1495. }
  1496. }
  1497. }
  1498. }
  1499. MFC_CATCH_HR
  1500. return hr;
  1501. } // End InitPropertySheet
  1502. void
  1503. CComponentData::HandleTraceConnectError (
  1504. HRESULT& rhr,
  1505. CString& rstrLogName,
  1506. CString& rstrMachineName )
  1507. {
  1508. ASSERT ( FAILED ( rhr ) );
  1509. if ( FAILED ( rhr ) ) {
  1510. CString strMessage;
  1511. CString strSysMessage;
  1512. INT iResult;
  1513. FormatSmLogCfgMessage (
  1514. strMessage,
  1515. m_hModule,
  1516. SMCFG_UNABLE_OPEN_TRACESVC_DLG,
  1517. rstrMachineName,
  1518. rstrLogName );
  1519. FormatMessage (
  1520. FORMAT_MESSAGE_FROM_SYSTEM,
  1521. NULL,
  1522. rhr,
  1523. 0,
  1524. strSysMessage.GetBufferSetLength( MAX_PATH ),
  1525. MAX_PATH,
  1526. NULL );
  1527. strSysMessage.ReleaseBuffer();
  1528. if ( strSysMessage.IsEmpty() ) {
  1529. strSysMessage.Format ( _T("0x%08lX"), rhr );
  1530. }
  1531. strMessage += strSysMessage;
  1532. m_ipConsole->MessageBox(
  1533. strMessage,
  1534. rstrLogName,
  1535. MB_OK | MB_ICONERROR,
  1536. &iResult);
  1537. }
  1538. return;
  1539. }// end HandleTraceConnectError()
  1540. HRESULT
  1541. CComponentData::NewTypedQuery (
  1542. CSmLogService* pSvc,
  1543. IPropertyBag* pPropBag,
  1544. LPDATAOBJECT pDataObject ) // [in] Points to the data object
  1545. {
  1546. HRESULT hr = S_OK;
  1547. LPTSTR szQueryName = NULL;
  1548. DWORD dwBufSize = 0;
  1549. ResourceStateManager rsm;
  1550. CNewQueryDlg cNewDlg(NULL, (((CSmNode*)pSvc)->CastToAlertService() ? FALSE : TRUE));
  1551. CThemeContextActivator activator;
  1552. ASSERT ( NULL != pSvc );
  1553. if ( NULL != pPropBag ) {
  1554. if ( NULL != ((CSmNode*)pSvc)->CastToAlertService() ) {
  1555. hr = CSmLogQuery::StringFromPropertyBag (
  1556. pPropBag,
  1557. NULL,
  1558. IDS_HTML_ALERT_NAME,
  1559. _T(""),
  1560. &szQueryName,
  1561. &dwBufSize );
  1562. if ( NULL == szQueryName ) {
  1563. hr = CSmLogQuery::StringFromPropertyBag (
  1564. pPropBag,
  1565. NULL,
  1566. IDS_HTML_LOG_NAME,
  1567. _T(""),
  1568. &szQueryName,
  1569. &dwBufSize );
  1570. }
  1571. } else {
  1572. hr = CSmLogQuery::StringFromPropertyBag (
  1573. pPropBag,
  1574. NULL,
  1575. IDS_HTML_LOG_NAME,
  1576. _T(""),
  1577. &szQueryName,
  1578. &dwBufSize );
  1579. if ( NULL == szQueryName ) {
  1580. hr = CSmLogQuery::StringFromPropertyBag (
  1581. pPropBag,
  1582. NULL,
  1583. IDS_HTML_ALERT_NAME,
  1584. _T(""),
  1585. &szQueryName,
  1586. &dwBufSize );
  1587. }
  1588. }
  1589. }
  1590. cNewDlg.SetContextHelpFilePath( GetContextHelpFilePath() );
  1591. cNewDlg.m_strName = szQueryName;
  1592. // Loop until the user hits Cancel or CreateQuery fails.
  1593. while ( IDOK == cNewDlg.DoModal() ) {
  1594. PSLQUERY pQuery;
  1595. pQuery = pSvc->CreateQuery ( cNewDlg.m_strName );
  1596. if ( NULL != pQuery ) {
  1597. MMC_COOKIE mmcQueryCookie = (MMC_COOKIE)pQuery;
  1598. LONG_PTR handle = NULL;
  1599. INT iPageIndex;
  1600. CPropertySheet cpsMain;
  1601. // If property bag provided, override defaults with the provided properties.
  1602. if ( NULL != pPropBag ) {
  1603. hr = pQuery->LoadFromPropertyBag ( pPropBag, NULL );
  1604. }
  1605. if ( FAILED(hr) ) {
  1606. hr = S_OK;
  1607. }
  1608. // now show property pages to modify the new query
  1609. hr = InitPropertySheet ( pQuery, mmcQueryCookie, handle, cpsMain );
  1610. if ( SUCCEEDED(hr) ) {
  1611. cpsMain.DoModal();
  1612. }
  1613. if ( pQuery->IsFirstModification() ) {
  1614. m_ipConsole->UpdateAllViews ( pDataObject, 0, eSmHintNewQuery );
  1615. } else {
  1616. // Delete query if newly created and OnApply was never called.
  1617. pSvc->DeleteQuery ( pQuery );
  1618. }
  1619. for (iPageIndex = cpsMain.GetPageCount() - 1; iPageIndex >= 0; iPageIndex-- ) {
  1620. delete cpsMain.GetPage( iPageIndex );
  1621. }
  1622. break;
  1623. } else {
  1624. INT iResult;
  1625. CString strMessage;
  1626. CString csTitle;
  1627. DWORD dwStatus;
  1628. BOOL bBreakImmediately = TRUE;
  1629. dwStatus = GetLastError();
  1630. if ( SMCFG_NO_MODIFY_ACCESS == dwStatus ) {
  1631. CString strMachineName;
  1632. strMachineName = pSvc->GetMachineDisplayName ();
  1633. FormatSmLogCfgMessage (
  1634. strMessage,
  1635. m_hModule,
  1636. SMCFG_NO_MODIFY_ACCESS,
  1637. (LPCTSTR)strMachineName);
  1638. } else if ( SMCFG_DUP_QUERY_NAME == dwStatus ) {
  1639. FormatSmLogCfgMessage (
  1640. strMessage,
  1641. m_hModule,
  1642. SMCFG_DUP_QUERY_NAME,
  1643. (LPCTSTR)cNewDlg.m_strName);
  1644. bBreakImmediately = FALSE;
  1645. } else {
  1646. FormatMessage (
  1647. FORMAT_MESSAGE_FROM_SYSTEM,
  1648. NULL,
  1649. dwStatus,
  1650. 0,
  1651. strMessage.GetBufferSetLength( MAX_PATH ),
  1652. MAX_PATH,
  1653. NULL );
  1654. strMessage.ReleaseBuffer();
  1655. if ( strMessage.IsEmpty() ) {
  1656. strMessage.Format ( _T("0x%08lX"), dwStatus );
  1657. }
  1658. }
  1659. csTitle.LoadString ( IDS_PROJNAME );
  1660. hr = m_ipConsole->MessageBox(
  1661. (LPCWSTR)strMessage,
  1662. (LPCWSTR)csTitle,
  1663. MB_OK | MB_ICONERROR,
  1664. &iResult
  1665. );
  1666. if ( bBreakImmediately ) {
  1667. break;
  1668. }
  1669. }
  1670. }
  1671. delete szQueryName;
  1672. return hr;
  1673. }
  1674. HRESULT
  1675. CComponentData::CreateNewLogQuery (
  1676. LPDATAOBJECT pDataObject, // [in] Points to the data object
  1677. IPropertyBag* pPropBag )
  1678. {
  1679. HRESULT hr = S_OK;
  1680. CDataObject* pDO = NULL;
  1681. MMC_COOKIE mmcSvcCookie;
  1682. BOOL bIsLogSvc;
  1683. PSLSVC pLogService;
  1684. ResourceStateManager rsm;
  1685. ASSERT( NULL != GetResultData() );
  1686. if ( NULL == pDataObject ) {
  1687. ASSERT ( FALSE );
  1688. hr = E_POINTER;
  1689. } else {
  1690. pDO = ExtractOwnDataObject( pDataObject );
  1691. if ( NULL == pDO ) {
  1692. ASSERT ( FALSE );
  1693. hr = E_UNEXPECTED;
  1694. }
  1695. }
  1696. if ( SUCCEEDED ( hr ) ) {
  1697. // If this is the root node, don't need to do anything
  1698. if( COOKIE_IS_ROOTNODE == pDO->GetCookieType() ) {
  1699. hr = S_FALSE;
  1700. } else {
  1701. // Just make sure we are where we think we are
  1702. ASSERT ( COOKIE_IS_COUNTERMAINNODE == pDO->GetCookieType()
  1703. || COOKIE_IS_TRACEMAINNODE == pDO->GetCookieType()
  1704. || COOKIE_IS_ALERTMAINNODE == pDO->GetCookieType() );
  1705. mmcSvcCookie = (MMC_COOKIE)pDO->GetCookie();
  1706. bIsLogSvc = IsLogService (mmcSvcCookie);
  1707. if (bIsLogSvc) {
  1708. pLogService = (PSLSVC)mmcSvcCookie;
  1709. hr = NewTypedQuery ( pLogService, pPropBag, pDataObject );
  1710. }
  1711. hr = S_OK;
  1712. }
  1713. }
  1714. return hr;
  1715. } // end CreateNewLogQuery()
  1716. HRESULT
  1717. CComponentData::CreateLogQueryFrom (
  1718. LPDATAOBJECT pDataObject ) // [in] Points to the data object
  1719. {
  1720. HRESULT hr = S_OK;
  1721. INT_PTR iPtrResult = IDCANCEL;
  1722. INT iResult = IDCANCEL;
  1723. CDataObject* pDO = NULL;
  1724. HWND hwndMain;
  1725. CString strFileExtension;
  1726. CString strFileFilter;
  1727. HANDLE hOpenFile;
  1728. TCHAR szInitialDir[MAX_PATH];
  1729. DWORD dwFileSize;
  1730. DWORD dwFileSizeHigh;
  1731. LPTSTR pszData = NULL;
  1732. CString strMessage;
  1733. CString strTitle;
  1734. CImpIPropertyBag* pPropBag = NULL;
  1735. DWORD dwStatus;
  1736. DWORD dwLogType;
  1737. CLogWarnd LogWarnd;
  1738. DWORD dwCookieType;
  1739. ResourceStateManager rsm;
  1740. if ( NULL == pDataObject ) {
  1741. ASSERT ( FALSE );
  1742. hr = E_POINTER;
  1743. } else {
  1744. pDO = ExtractOwnDataObject( pDataObject );
  1745. if ( NULL == pDO ) {
  1746. ASSERT ( FALSE );
  1747. hr = E_UNEXPECTED;
  1748. }
  1749. }
  1750. if ( SUCCEEDED ( hr ) ) {
  1751. if ( IsLogService ( pDO->GetCookie() ) ) {
  1752. // Find file to create from.
  1753. MFC_TRY
  1754. strFileExtension.LoadString ( IDS_HTML_EXTENSION );
  1755. strFileFilter.LoadString ( IDS_HTML_FILE );
  1756. MFC_CATCH_HR
  1757. strFileFilter.Replace ( _T('|'), _T('\0') );
  1758. hr = m_ipConsole->GetMainWindow( &hwndMain );
  1759. if ( SUCCEEDED(hr) ) {
  1760. OPENFILENAME ofn;
  1761. BOOL bResult;
  1762. TCHAR szFileName[MAX_PATH];
  1763. ZeroMemory( szFileName, MAX_PATH*sizeof(TCHAR) );
  1764. ZeroMemory( &ofn, sizeof( OPENFILENAME ) );
  1765. ofn.lStructSize = sizeof(OPENFILENAME);
  1766. ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  1767. ofn.lpstrFile = szFileName;
  1768. ofn.nMaxFile = MAX_PATH;
  1769. ofn.lpstrDefExt = (LPCTSTR)strFileExtension;
  1770. ofn.lpstrFilter = strFileFilter;
  1771. ofn.hwndOwner = hwndMain;
  1772. ofn.hInstance = m_hModule;
  1773. if ( SUCCEEDED ( SHGetFolderPathW ( NULL, CSIDL_PERSONAL, NULL, 0, szInitialDir ) ) ) {
  1774. ofn.lpstrInitialDir = szInitialDir;
  1775. }
  1776. bResult = GetOpenFileName( &ofn );
  1777. if ( bResult ) {
  1778. // Open the file to find the query name.
  1779. hOpenFile = CreateFile (
  1780. ofn.lpstrFile,
  1781. GENERIC_READ,
  1782. 0, // Not shared
  1783. NULL, // Security attributes
  1784. OPEN_EXISTING, //
  1785. FILE_ATTRIBUTE_NORMAL,
  1786. NULL );
  1787. if ( hOpenFile && INVALID_HANDLE_VALUE != hOpenFile ) {
  1788. // Create a property bag and load it. Use the existing query
  1789. // name as the default to ask the user for a new query name.
  1790. // New query name is required if the current name exists in the registry.
  1791. // Read the file contents into a memory buffer.
  1792. dwFileSize = GetFileSize ( hOpenFile, &dwFileSizeHigh );
  1793. ASSERT ( 0 == dwFileSizeHigh );
  1794. MFC_TRY
  1795. pszData = new TCHAR[(dwFileSize + sizeof(TCHAR))/sizeof(TCHAR)];
  1796. MFC_CATCH_HR
  1797. if ( NULL != pszData ) {
  1798. if ( FileRead ( hOpenFile, pszData, dwFileSize ) ) {
  1799. // Read contents from a property bag
  1800. MFC_TRY
  1801. pPropBag = new CImpIPropertyBag;
  1802. MFC_CATCH_HR
  1803. if ( NULL != pPropBag ) {
  1804. MFC_TRY
  1805. strTitle.LoadString ( IDS_PROJNAME );
  1806. MFC_CATCH_HR
  1807. dwStatus = pPropBag->LoadData( pszData );
  1808. hr = HRESULT_FROM_WIN32( dwStatus );
  1809. if ( SUCCEEDED ( hr ) ) {
  1810. //get the log type from the pPropBag and compare it with service(cookie) type
  1811. // Determine log type from property bag. Default to -1 SMONCTRL_LOG
  1812. hr = CSmLogQuery::DwordFromPropertyBag (
  1813. pPropBag,
  1814. NULL,
  1815. IDS_HTML_LOG_TYPE,
  1816. SMONCTRL_LOG, //indicates tha it's a smonctrl log
  1817. dwLogType);
  1818. if (SUCCEEDED (hr) ){
  1819. dwCookieType = (DWORD)pDO->GetCookieType();
  1820. switch(dwCookieType){
  1821. case COOKIE_IS_COUNTERMAINNODE:
  1822. if (dwLogType != SLQ_COUNTER_LOG ){
  1823. //Error
  1824. LogWarnd.m_ErrorMsg = ID_ERROR_COUNTER_LOG;
  1825. hr = S_FALSE;
  1826. }
  1827. break;
  1828. case COOKIE_IS_TRACEMAINNODE:
  1829. if (dwLogType != SLQ_TRACE_LOG ){
  1830. //Error
  1831. LogWarnd.m_ErrorMsg = ID_ERROR_TRACE_LOG;
  1832. hr = S_FALSE;
  1833. }
  1834. break;
  1835. case COOKIE_IS_ALERTMAINNODE:
  1836. if (dwLogType != SLQ_ALERT){
  1837. //Error
  1838. LogWarnd.m_ErrorMsg = ID_ERROR_ALERT_LOG;
  1839. hr = S_FALSE;
  1840. }
  1841. break;
  1842. case SMONCTRL_LOG:
  1843. //Error
  1844. LogWarnd.m_ErrorMsg = ID_ERROR_SMONCTRL_LOG;
  1845. hr = S_FALSE;
  1846. break;
  1847. }
  1848. if (hr == S_FALSE){
  1849. if(dwLogType == SLQ_TRACE_LOG || LogWarnd.m_ErrorMsg == ID_ERROR_TRACE_LOG ){
  1850. MFC_TRY
  1851. strMessage.LoadString(IDS_ERRMSG_TRACE_LOG);
  1852. MFC_CATCH_HR
  1853. m_ipConsole->MessageBox (
  1854. strMessage,
  1855. strTitle,
  1856. MB_OK | MB_ICONERROR,
  1857. &iResult );
  1858. } else {
  1859. LogWarnd.m_dwLogType = dwLogType;
  1860. MFC_TRY
  1861. LogWarnd.m_strContextHelpFile = GetContextHelpFilePath();
  1862. // TODO: Handle error
  1863. MFC_CATCH_MINIMUM
  1864. if(!LogTypeCheckNoMore(&LogWarnd)){
  1865. LogWarnd.SetTitleString ( strTitle );
  1866. LogWarnd.DoModal();
  1867. }
  1868. CreateNewLogQuery ( pDataObject, pPropBag );
  1869. }
  1870. }
  1871. }
  1872. if ( S_OK == hr ) {
  1873. hr = CreateNewLogQuery ( pDataObject, pPropBag );
  1874. }
  1875. } else {
  1876. FormatSmLogCfgMessage (
  1877. strMessage,
  1878. m_hModule,
  1879. SMCFG_NO_HTML_SYSMON_OBJECT );
  1880. m_ipConsole->MessageBox (
  1881. strMessage,
  1882. strTitle,
  1883. MB_OK | MB_ICONERROR,
  1884. &iResult );
  1885. }
  1886. }
  1887. }
  1888. delete pszData;
  1889. }
  1890. CloseHandle ( hOpenFile );
  1891. }
  1892. }
  1893. }
  1894. }
  1895. }
  1896. return hr;
  1897. } // End CreateLogQueryFrom
  1898. BOOL
  1899. CComponentData::LogTypeCheckNoMore (
  1900. CLogWarnd* LogWarnd )
  1901. {
  1902. BOOL bretVal = FALSE;
  1903. long nErr;
  1904. HKEY hKey;
  1905. DWORD dwWarnFlag;
  1906. DWORD dwDataType;
  1907. DWORD dwDataSize;
  1908. DWORD dwDisposition;
  1909. TCHAR RegValName[MAX_PATH];
  1910. switch (LogWarnd->m_dwLogType){
  1911. case SLQ_COUNTER_LOG:
  1912. _stprintf(RegValName, _T("NoWarnCounterLog"));
  1913. break;
  1914. case SLQ_ALERT:
  1915. _stprintf(RegValName, _T("NoWarnAlertLog"));
  1916. break;
  1917. }
  1918. // check registry setting to see if we need to pop up warning dialog
  1919. nErr = RegOpenKey(
  1920. HKEY_CURRENT_USER,
  1921. _T("Software\\Microsoft\\PerformanceLogsAndAlerts"),
  1922. &hKey );
  1923. if( nErr != ERROR_SUCCESS ) {
  1924. nErr = RegCreateKeyEx(
  1925. HKEY_CURRENT_USER,
  1926. _T("Software\\Microsoft\\PerformanceLogsAndAlerts"),
  1927. 0,
  1928. _T("REG_DWORD"),
  1929. REG_OPTION_NON_VOLATILE,
  1930. KEY_ALL_ACCESS,
  1931. NULL,
  1932. &hKey,
  1933. &dwDisposition );
  1934. }
  1935. dwWarnFlag = 0;
  1936. if( nErr == ERROR_SUCCESS ) {
  1937. dwDataSize = sizeof(DWORD);
  1938. nErr = RegQueryValueExW(
  1939. hKey,
  1940. RegValName,
  1941. NULL,
  1942. &dwDataType,
  1943. (LPBYTE) &dwWarnFlag,
  1944. (LPDWORD) &dwDataSize
  1945. );
  1946. if (ERROR_SUCCESS == nErr ){
  1947. LogWarnd->m_hKey = hKey;
  1948. }
  1949. if ( (dwDataType != REG_DWORD) || (dwDataSize != sizeof(DWORD)))
  1950. dwWarnFlag = 0;
  1951. if (dwWarnFlag)
  1952. bretVal = TRUE;
  1953. nErr = RegCloseKey( hKey );
  1954. if( ERROR_SUCCESS != nErr ){
  1955. // DisplayError( GetLastError(), _T("Close PerfLog user Key Failed") );
  1956. bretVal = FALSE;
  1957. }
  1958. }
  1959. return bretVal;
  1960. }