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.

434 lines
9.4 KiB

  1. // HealthmonScopePane.cpp: implementation of the CHealthmonScopePane class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "snapin.h"
  6. #include "AllSystemsScopeItem.h"
  7. #include "HealthmonScopePane.h"
  8. #include "HealthmonResultsPane.h"
  9. #include "SystemsScopeItem.h"
  10. #include "RootScopeItem.h"
  11. #include "SystemGroup.h"
  12. #include "System.h"
  13. #include "EventManager.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. IMPLEMENT_DYNCREATE(CHealthmonScopePane,CScopePane)
  20. //////////////////////////////////////////////////////////////////////
  21. // Construction/Destruction
  22. //////////////////////////////////////////////////////////////////////
  23. CHealthmonScopePane::CHealthmonScopePane()
  24. {
  25. EnableAutomation();
  26. // To keep the application running as long as an OLE automation
  27. // object is active, the constructor calls AfxOleLockApp.
  28. AfxOleLockApp();
  29. // create the root scope item
  30. SetRootScopeItem(CreateRootScopeItem());
  31. m_pRootGroup = NULL;
  32. }
  33. CHealthmonScopePane::~CHealthmonScopePane()
  34. {
  35. if( m_pRootItem )
  36. {
  37. if( GfxCheckObjPtr(m_pRootItem,CScopePaneItem) )
  38. {
  39. delete m_pRootItem;
  40. }
  41. m_pRootItem = NULL;
  42. }
  43. // To terminate the application when all objects created with
  44. // with OLE automation, the destructor calls AfxOleUnlockApp.
  45. AfxOleUnlockApp();
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. // Creation/Destruction Overrideable Members
  49. /////////////////////////////////////////////////////////////////////////////
  50. bool CHealthmonScopePane::OnCreate()
  51. {
  52. TRACEX(_T("CHealthmonScopePane::OnCreate\n"));
  53. // create connection manager
  54. if( ! CnxCreate() )
  55. {
  56. TRACE(_T("FAILED : CnxCreate returns false.\n"));
  57. return false;
  58. }
  59. // call base class to create root scope item
  60. if( ! CScopePane::OnCreate() )
  61. {
  62. TRACE(_T("FAILED : CScopePane::OnCreate failed.\n"));
  63. return false;
  64. }
  65. // create the Root Group
  66. m_pRootGroup = new CSystemGroup;
  67. m_pRootGroup->SetScopePane(this);
  68. CScopePaneItem* pRootItem = GetRootScopeItem();
  69. m_pRootGroup->SetName(pRootItem->GetDisplayName());
  70. m_pRootGroup->AddScopeItem(pRootItem);
  71. ((CHMScopeItem*)pRootItem)->SetObjectPtr(m_pRootGroup);
  72. EvtGetEventManager()->AddContainer(_T(""),_T(""),m_pRootGroup->GetGuid(),m_pRootGroup);
  73. // create the All Systems Group and add it to the root group
  74. CAllSystemsGroup* pAllSystemsGroup = new CAllSystemsGroup;
  75. CString sName;
  76. sName.LoadString(IDS_STRING_ALL_SYSTEMS_NODE);
  77. pAllSystemsGroup->SetName(sName);
  78. m_pRootGroup->AddChild(pAllSystemsGroup);
  79. return true;
  80. }
  81. LPCOMPONENT CHealthmonScopePane::OnCreateComponent()
  82. {
  83. TRACEX(_T("CHealthmonScopePane::OnCreateComponent\n"));
  84. CHealthmonResultsPane* pNewPane = new CHealthmonResultsPane;
  85. if( ! GfxCheckObjPtr(pNewPane,CHealthmonResultsPane) )
  86. {
  87. TRACE(_T("FAILED : Out of memory.\n"));
  88. return NULL;
  89. }
  90. pNewPane->SetOwnerScopePane(this);
  91. int iIndex = AddResultsPane(pNewPane);
  92. ASSERT(iIndex != -1);
  93. LPCOMPONENT pComponent = (LPCOMPONENT)pNewPane->GetInterface(&IID_IComponent);
  94. if( ! CHECKPTR(pComponent,sizeof(IComponent)) )
  95. {
  96. return NULL;
  97. }
  98. return pComponent;
  99. }
  100. bool CHealthmonScopePane::OnDestroy()
  101. {
  102. TRACEX(_T("CHealthmonScopePane::OnDestroy\n"));
  103. // unhook the window first
  104. UnhookWindow();
  105. if( m_pMsgHook )
  106. {
  107. delete m_pMsgHook;
  108. m_pMsgHook = NULL;
  109. }
  110. // destroy the root item and all its child scope items
  111. if( m_pRootItem )
  112. {
  113. if( GfxCheckObjPtr(m_pRootItem,CScopePaneItem) )
  114. {
  115. delete m_pRootItem;
  116. }
  117. m_pRootItem = NULL;
  118. }
  119. m_pSelectedScopeItem = NULL;
  120. // destroy the HMObjects we allocated during the console session
  121. if( m_pRootGroup )
  122. {
  123. m_pRootGroup->Destroy();
  124. EvtGetEventManager()->RemoveContainer(_T(""),m_pRootGroup->GetGuid());
  125. delete m_pRootGroup;
  126. m_pRootGroup = NULL;
  127. }
  128. // Release all the interfaces queried for
  129. if( m_pIConsole )
  130. {
  131. m_pIConsole->Release();
  132. m_pIConsole = NULL;
  133. }
  134. if( m_pIConsoleNamespace )
  135. {
  136. m_pIConsoleNamespace->Release();
  137. m_pIConsoleNamespace = NULL;
  138. }
  139. if( m_pIImageList )
  140. {
  141. m_pIImageList->Release();
  142. m_pIImageList = NULL;
  143. }
  144. // empty Result Panes array
  145. for( int i = GetResultsPaneCount()-1; i >= 0; i-- )
  146. {
  147. RemoveResultsPane(i);
  148. }
  149. CnxDestroy();
  150. return true;
  151. }
  152. /////////////////////////////////////////////////////////////////////////////
  153. // Root Scope Pane Item Members
  154. /////////////////////////////////////////////////////////////////////////////
  155. CScopePaneItem* CHealthmonScopePane::CreateRootScopeItem()
  156. {
  157. TRACEX(_T("CHealthmonScopePane::CreateRootScopeItem\n"));
  158. return new CRootScopeItem;
  159. }
  160. /////////////////////////////////////////////////////////////////////////////
  161. // Healthmon Scope Helper Members
  162. /////////////////////////////////////////////////////////////////////////////
  163. CAllSystemsScopeItem* CHealthmonScopePane::GetAllSystemsScopeItem()
  164. {
  165. TRACEX(_T("CHealthmonScopePane::GetAllSystemsScopeItem\n"));
  166. CScopePaneItem* pRootItem = GetRootScopeItem();
  167. if( ! pRootItem )
  168. {
  169. TRACE(_T("FAILED : CScopePane::GetRootScopeItem returns NULL.\n"));
  170. return NULL;
  171. }
  172. for( int i = 0; i < pRootItem->GetChildCount(); i++ )
  173. {
  174. CScopePaneItem* pTempItem = pRootItem->GetChild(i);
  175. if( pTempItem && pTempItem->IsKindOf(RUNTIME_CLASS(CAllSystemsScopeItem)) )
  176. {
  177. return (CAllSystemsScopeItem*)pTempItem;
  178. }
  179. }
  180. TRACE(_T("FAILED : A node of type CAllSystemsScopeItem was not found.\n"));
  181. ASSERT(FALSE);
  182. return NULL;
  183. }
  184. CSystemGroup* CHealthmonScopePane::GetAllSystemsGroup()
  185. {
  186. TRACEX(_T("CHealthmonScopePane::GetAllSystemsGroup\n"));
  187. CSystemGroup* pRG = GetRootGroup();
  188. CSystemGroup* pASG = (CSystemGroup*)pRG->GetChild(0);
  189. if( ! GfxCheckObjPtr(pASG,CSystemGroup) )
  190. {
  191. return NULL;
  192. }
  193. return pASG;
  194. }
  195. CSystem* CHealthmonScopePane::GetSystem(const CString& sName)
  196. {
  197. TRACEX(_T("CHealthmonScopePane::GetSystem\n"));
  198. CSystemGroup* pGroup = GetAllSystemsGroup();
  199. CSystem* pSystem = (CSystem*)pGroup->GetChild(sName);
  200. if( ! GfxCheckObjPtr(pSystem,CSystem) )
  201. {
  202. return NULL;
  203. }
  204. return pSystem;
  205. }
  206. /////////////////////////////////////////////////////////////////////////////
  207. // Serialization
  208. /////////////////////////////////////////////////////////////////////////////
  209. bool CHealthmonScopePane::OnLoad(CArchive& ar)
  210. {
  211. TRACEX(_T("CHealthmonScopePane::OnLoad\n"));
  212. if( ! CScopePane::OnLoad(ar) )
  213. {
  214. return false;
  215. }
  216. CSystemGroup* pASG = GetAllSystemsGroup();
  217. ASSERT(pASG);
  218. pASG->Serialize(ar);
  219. CStringArray saSystems;
  220. if( ParseCommandLine(saSystems) )
  221. {
  222. for( int z = 0; z < saSystems.GetSize(); z++ )
  223. {
  224. IWbemServices* pServices = NULL;
  225. BOOL bAvail = FALSE;
  226. if( CnxGetConnection(saSystems[z],pServices,bAvail) == E_FAIL )
  227. {
  228. MessageBeep(MB_ICONEXCLAMATION);
  229. }
  230. if( pServices )
  231. {
  232. pServices->Release();
  233. }
  234. CSystem* pNewSystem = new CSystem;
  235. pNewSystem->SetName(saSystems[z]);
  236. pNewSystem->SetSystemName(saSystems[z]);
  237. pNewSystem->SetScopePane(this);
  238. pASG->AddChild(pNewSystem);
  239. pNewSystem->Connect();
  240. CActionPolicy* pPolicy = new CActionPolicy;
  241. pPolicy->SetSystemName(pNewSystem->GetName());
  242. pNewSystem->AddChild(pPolicy);
  243. /* causes AV in MMCNDMGR
  244. if( z == 0 )
  245. {
  246. for( int x = 0; x < pNewSystem->GetScopeItemCount(); x++ )
  247. {
  248. CScopePaneItem* pItem = pNewSystem->GetScopeItem(x);
  249. if( pItem )
  250. {
  251. pItem->SelectItem();
  252. }
  253. }
  254. }
  255. */
  256. }
  257. }
  258. CSystemGroup* pMSG = GetRootGroup();
  259. int iSystemGroupCount;
  260. ar >> iSystemGroupCount;
  261. for( int i = 0; i < iSystemGroupCount; i++ )
  262. {
  263. CSystemGroup* pNewGroup = new CSystemGroup;
  264. pNewGroup->SetScopePane(this);
  265. pNewGroup->SetName(pMSG->GetUniqueChildName());
  266. pMSG->AddChild(pNewGroup);
  267. pNewGroup->Serialize(ar);
  268. }
  269. return true;
  270. }
  271. bool CHealthmonScopePane::OnSave(CArchive& ar)
  272. {
  273. TRACEX(_T("CHealthmonScopePane::OnSave\n"));
  274. if( ! CScopePane::OnSave(ar) )
  275. {
  276. return false;
  277. }
  278. CSystemGroup* pASG = GetAllSystemsGroup();
  279. ASSERT(pASG);
  280. pASG->Serialize(ar);
  281. CSystemGroup* pMSG = GetRootGroup();
  282. int iSystemGroupCount = pMSG->GetChildCount(RUNTIME_CLASS(CSystemGroup))-1;
  283. ar << iSystemGroupCount;
  284. for( int i = 1; i <= iSystemGroupCount; i++ )
  285. {
  286. pMSG->GetChild(i)->Serialize(ar);
  287. }
  288. return true;
  289. }
  290. /////////////////////////////////////////////////////////////////////////////
  291. // Parse Command Line
  292. /////////////////////////////////////////////////////////////////////////////
  293. bool CHealthmonScopePane::ParseCommandLine(CStringArray& saSystems)
  294. {
  295. TRACEX(_T("CHealthmonScopePane::ParseCommandLine\n"));
  296. saSystems.RemoveAll();
  297. CString sCmdLine = GetCommandLine();
  298. sCmdLine.MakeUpper();
  299. int iIndex = sCmdLine.Find(_T("/HEALTHMON_SYSTEMS:"));
  300. if( iIndex == -1 )
  301. {
  302. return false;
  303. }
  304. sCmdLine = sCmdLine.Right(sCmdLine.GetLength()-iIndex-19);
  305. iIndex = sCmdLine.Find(_T(" "));
  306. if( iIndex != -1 )
  307. {
  308. sCmdLine = sCmdLine.Left(iIndex);
  309. }
  310. LPTSTR lpszCmdLine = new TCHAR[sCmdLine.GetLength()+1];
  311. _tcscpy(lpszCmdLine,sCmdLine);
  312. LPTSTR lpszToken = _tcstok(lpszCmdLine,_T(","));
  313. while(lpszToken)
  314. {
  315. saSystems.Add(lpszToken);
  316. lpszToken = _tcstok(NULL,_T(","));
  317. }
  318. delete[] lpszCmdLine;
  319. return true;
  320. }
  321. // {FBBB8DAE-AB34-11d2-BD62-0000F87A3912}
  322. IMPLEMENT_OLECREATE_EX(CHealthmonScopePane, "SnapIn.ScopePane", 0xfbbb8dae, 0xab34, 0x11d2, 0xbd, 0x62, 0x0, 0x0, 0xf8, 0x7a, 0x39, 0x12);
  323. BOOL CHealthmonScopePane::CHealthmonScopePaneFactory::UpdateRegistry(BOOL bRegister)
  324. {
  325. if (bRegister)
  326. return AfxOleRegisterServerClass(m_clsid, m_lpszProgID, m_lpszProgID, m_lpszProgID, OAT_DISPATCH_OBJECT);
  327. else
  328. return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  329. }