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.

567 lines
16 KiB

  1. // DPServicePage.cpp : implementation file
  2. //
  3. // 03/05/00 v-marfin bug 59643 : Make this the default starting page.
  4. // 03/22/00 v-marfin 60766 : Set proper dlg title for object being browsed.
  5. // 03/29/00 v-marfin bug 62585 : Set new Data collector's ENABLED to TRUE if user presses OK.
  6. // 03/30/00 v-marfin bug 59237 : If user does not change the default name of the data
  7. // collector when they first create it, change it for
  8. // them to a more meaningful name based on the data
  9. // they select in the property pages.
  10. #include "stdafx.h"
  11. #include "snapin.h"
  12. #include "DPServicePage.h"
  13. #include "HMObject.h"
  14. #include "WmiBrowseDlg.h"
  15. #include "WmiPropertyBrowseDlg.h"
  16. #include "DataElement.h"
  17. #include "DataGroupScopeItem.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CDPServicePage property page
  25. IMPLEMENT_DYNCREATE(CDPServicePage, CHMPropertyPage)
  26. CDPServicePage::CDPServicePage() : CHMPropertyPage(CDPServicePage::IDD)
  27. {
  28. //{{AFX_DATA_INIT(CDPServicePage)
  29. m_iType = 0;
  30. m_bRequireReset = FALSE;
  31. m_sService = _T("");
  32. m_sProcess = _T("");
  33. //}}AFX_DATA_INIT
  34. m_sHelpTopic = _T("HMon21.chm::/dDEserv.htm");
  35. }
  36. CDPServicePage::~CDPServicePage()
  37. {
  38. }
  39. void CDPServicePage::UpdateProperties(CListCtrl& Properties, const CString& sNamespace, const CString& sClass)
  40. {
  41. Properties.DeleteAllItems();
  42. if( sClass.IsEmpty() )
  43. {
  44. return;
  45. }
  46. CWbemClassObject classobject;
  47. classobject.SetNamespace(_T("\\\\") + GetObjectPtr()->GetSystemName() + _T("\\") + sNamespace);
  48. HRESULT hr = classobject.GetObject(sClass);
  49. if( hr != S_OK )
  50. {
  51. return;
  52. }
  53. CStringArray saNames;
  54. classobject.GetPropertyNames(saNames);
  55. classobject.Destroy();
  56. for( int i = 0; i < saNames.GetSize(); i++ )
  57. {
  58. Properties.InsertItem(0,saNames[i]);
  59. }
  60. Properties.SetColumnWidth(0,LVSCW_AUTOSIZE);
  61. }
  62. void CDPServicePage::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CHMPropertyPage::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CDPServicePage)
  66. DDX_Control(pDX, IDC_LIST_PROCESS_PROPERTIES, m_ProcessProperties);
  67. DDX_Control(pDX, IDC_LIST_SERVICE_PROPERTIES, m_ServiceProperties);
  68. DDX_Radio(pDX, IDC_RADIO1, m_iType);
  69. DDX_Check(pDX, IDC_CHECK_REQUIRE_RESET, m_bRequireReset);
  70. DDX_Text(pDX, IDC_EDIT_SERVICE, m_sService);
  71. DDX_Text(pDX, IDC_EDIT_PROCESS, m_sProcess);
  72. //}}AFX_DATA_MAP
  73. if( m_iType == 0 )
  74. {
  75. GetDlgItem(IDC_EDIT_SERVICE)->EnableWindow();
  76. GetDlgItem(IDC_BUTTON_BROWSE_SERVICE)->EnableWindow();
  77. GetDlgItem(IDC_LIST_SERVICE_PROPERTIES)->EnableWindow();
  78. GetDlgItem(IDC_BUTTON_BROWSE_PROCESS)->EnableWindow(FALSE);
  79. GetDlgItem(IDC_EDIT_PROCESS)->EnableWindow(FALSE);
  80. GetDlgItem(IDC_LIST_PROCESS_PROPERTIES)->EnableWindow(FALSE);
  81. }
  82. else if( m_iType == 1 )
  83. {
  84. GetDlgItem(IDC_EDIT_SERVICE)->EnableWindow(FALSE);
  85. GetDlgItem(IDC_BUTTON_BROWSE_SERVICE)->EnableWindow(FALSE);
  86. GetDlgItem(IDC_LIST_SERVICE_PROPERTIES)->EnableWindow(FALSE);
  87. GetDlgItem(IDC_BUTTON_BROWSE_PROCESS)->EnableWindow();
  88. GetDlgItem(IDC_EDIT_PROCESS)->EnableWindow();
  89. GetDlgItem(IDC_LIST_PROCESS_PROPERTIES)->EnableWindow();
  90. }
  91. }
  92. BEGIN_MESSAGE_MAP(CDPServicePage, CHMPropertyPage)
  93. //{{AFX_MSG_MAP(CDPServicePage)
  94. ON_WM_DESTROY()
  95. ON_BN_CLICKED(IDC_BUTTON_BROWSE_PROCESS, OnButtonBrowseProcess)
  96. ON_BN_CLICKED(IDC_BUTTON_BROWSE_SERVICE, OnButtonBrowseService)
  97. ON_EN_CHANGE(IDC_EDIT_PROCESS, OnChangeEditProcess)
  98. ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
  99. ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
  100. ON_EN_CHANGE(IDC_EDIT_SERVICE, OnChangeEditService)
  101. ON_BN_CLICKED(IDC_CHECK_REQUIRE_RESET, OnCheckRequireReset)
  102. ON_NOTIFY(NM_CLICK, IDC_LIST_PROCESS_PROPERTIES, OnClickListProcessProperties2)
  103. ON_NOTIFY(NM_CLICK, IDC_LIST_SERVICE_PROPERTIES, OnClickListServiceProperties)
  104. //}}AFX_MSG_MAP
  105. END_MESSAGE_MAP()
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CDPServicePage message handlers
  108. BOOL CDPServicePage::OnInitDialog()
  109. {
  110. // v-marfin : bug 59643 : This will be the default starting page for the property
  111. // sheet so call CnxPropertyPageCreate() to unmarshal the
  112. // connection for this thread. This function must be called
  113. // by the first page of the property sheet. It used to
  114. // be called by the "General" page and its call still remains
  115. // there as well in case the general page is loaded by a
  116. // different code path that does not also load this page.
  117. // The CnxPropertyPageCreate function has been safeguarded
  118. // to simply return if the required call has already been made.
  119. // CnxPropertyPageDestory() must be called from this page's
  120. // OnDestroy function.
  121. // unmarshal connmgr
  122. CnxPropertyPageCreate();
  123. CHMPropertyPage::OnInitDialog();
  124. // initialize the list view
  125. m_ServiceProperties.SetExtendedStyle(LVS_EX_CHECKBOXES);
  126. m_ProcessProperties.SetExtendedStyle(LVS_EX_CHECKBOXES);
  127. CString sColumnTitle;
  128. sColumnTitle.LoadString(IDS_STRING_NAME);
  129. m_ServiceProperties.InsertColumn(0,sColumnTitle);
  130. m_ProcessProperties.InsertColumn(0,sColumnTitle);
  131. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  132. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  133. {
  134. return TRUE;
  135. }
  136. UpdateProperties(m_ServiceProperties,_T("root\\cimv2"),_T("Win32_Service"));
  137. UpdateProperties(m_ProcessProperties,_T("root\\cimv2"),_T("Win32_Process"));
  138. //-------------------------------------------------------------------------
  139. // v-marfin 59237 : Store original name in case this data collector is
  140. // just being created. When they save, we will modify the
  141. // name if they haven't.
  142. pClassObject->GetProperty(IDS_STRING_MOF_NAME,m_sOriginalName);
  143. //-------------------------------------------------------------------------
  144. CString sQuery;
  145. pClassObject->GetProperty(IDS_STRING_MOF_QUERY,sQuery);
  146. int iIndex = -1;
  147. if( (iIndex = sQuery.Find(_T("Win32_Service"))) != -1 )
  148. {
  149. CWbemClassObject::GetPropertyValueFromString(sQuery,_T("Name"),m_sService);
  150. m_iType = 0;
  151. }
  152. else if( (iIndex = sQuery.Find(_T("Win32_Process"))) != -1 )
  153. {
  154. CWbemClassObject::GetPropertyValueFromString(sQuery,_T("Name"),m_sProcess);
  155. m_iType = 1;
  156. }
  157. CStringArray saProperties;
  158. CString sProperties;
  159. pClassObject->GetProperty(IDS_STRING_MOF_STATISTICSPROPERTYNAMES,saProperties);
  160. if( m_iType == 0 )
  161. {
  162. for( int i=0; i < saProperties.GetSize(); i++ )
  163. {
  164. LVFINDINFO lvfi;
  165. ZeroMemory(&lvfi,sizeof(LVFINDINFO));
  166. lvfi.flags = LVFI_WRAP|LVFI_STRING;
  167. lvfi.psz = saProperties[i];
  168. int iListIndex = m_ServiceProperties.FindItem(&lvfi);
  169. if( iListIndex >= 0 )
  170. {
  171. m_ServiceProperties.SetCheck(iListIndex);
  172. }
  173. }
  174. }
  175. else if( m_iType == 1 )
  176. {
  177. for( int i=0; i < saProperties.GetSize(); i++ )
  178. {
  179. LVFINDINFO lvfi;
  180. ZeroMemory(&lvfi,sizeof(LVFINDINFO));
  181. lvfi.flags = LVFI_WRAP|LVFI_STRING;
  182. lvfi.psz = saProperties[i];
  183. int iListIndex = m_ProcessProperties.FindItem(&lvfi);
  184. if( iListIndex >= 0 )
  185. {
  186. m_ProcessProperties.SetCheck(iListIndex);
  187. }
  188. }
  189. }
  190. bool bReset;
  191. pClassObject->GetProperty(IDS_STRING_MOF_REQUIRERESET,bReset);
  192. m_bRequireReset = bReset;
  193. delete pClassObject;
  194. UpdateData(FALSE);
  195. return TRUE; // return TRUE unless you set the focus to a control
  196. // EXCEPTION: OCX Property Pages should return FALSE
  197. }
  198. void CDPServicePage::OnDestroy()
  199. {
  200. // v-marfin 62585 : For this new data collector, set its Enabled property to TRUE, but
  201. // only if the user is not cancelling these property pages.
  202. if (m_bOnApplyUsed)
  203. {
  204. ClearStatistics(); // 62548
  205. CDataElement* pElement = (CDataElement*)GetObjectPtr();
  206. if (pElement && pElement->IsStateSetToEnabledOnOK())
  207. {
  208. TRACE(_T("CDPServicePage::OnDestroy - New Perfmon Collector: Setting to Enabled\n"));
  209. pElement->SetStateToEnabledOnOK(FALSE); // don't do this again
  210. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  211. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  212. {
  213. TRACE(_T("ERROR: CDPServicePage::OnDestroy - Failed to GetClassObject()\n"));
  214. return;
  215. }
  216. // Set the new collector to enabled.
  217. BOOL bEnabled=TRUE;
  218. HRESULT hr = pClassObject->GetProperty(IDS_STRING_MOF_ENABLE,bEnabled);
  219. hr = pClassObject->SetProperty(IDS_STRING_MOF_ENABLE,TRUE);
  220. if (!CHECKHRESULT(hr))
  221. {
  222. TRACE(_T("ERROR: CDPServicePage::OnDestroy - Failed to set ENABLED property on new collector\n"));
  223. }
  224. //-------------------------------------------------------------------
  225. // v-marfin 59237 : If the user has not changed the original default
  226. // name, do so for them. Compare against original
  227. // name we fetched during OnInitDialog.
  228. CString sName;
  229. pClassObject->GetProperty(IDS_STRING_MOF_NAME,sName);
  230. // Did the user change the default name?
  231. if (m_sOriginalName.CompareNoCase(sName)==0)
  232. {
  233. CString sObject = m_sService.IsEmpty() ? m_sProcess : m_sService;
  234. // No, so set the new name
  235. if (!sObject.IsEmpty())
  236. {
  237. // Use parent to ensure name is unique
  238. //CDataGroup* pParent = (CDataGroup*) pElement->GetCollectorsParentClassObject();
  239. if(pElement->GetScopeItemCount())
  240. {
  241. CDataElementScopeItem* pItem = (CDataElementScopeItem*)pElement->GetScopeItem(0);
  242. if( pItem )
  243. {
  244. CDataGroupScopeItem* pDGItem = (CDataGroupScopeItem*)pItem->GetParent();
  245. sName = pDGItem->GetUniqueDisplayName(sObject);
  246. }
  247. }
  248. // Set the local element's object data
  249. pElement->SetName(sName);
  250. // Set its WMI property
  251. pClassObject->SetProperty(IDS_STRING_MOF_NAME,sName);
  252. // Refresh to show the new name in the IU
  253. //pElement->Refresh(); // 63005
  254. pElement->UpdateStatus(); // 63005
  255. }
  256. }
  257. //-------------------------------------------------------------------
  258. pClassObject->SaveAllProperties();
  259. delete pClassObject;
  260. } // if (pElement && pElement->IsStateSetToEnabledOnOK())
  261. } // if (m_bOnApplyUsed)
  262. CHMPropertyPage::OnDestroy();
  263. // v-marfin : bug 59643 : CnxPropertyPageDestory() must be called from this page's
  264. // OnDestroy function.
  265. CnxPropertyPageDestroy();
  266. }
  267. void CDPServicePage::OnOK()
  268. {
  269. CHMPropertyPage::OnOK();
  270. }
  271. void CDPServicePage::OnButtonBrowseProcess()
  272. {
  273. CWmiInstanceBrowseDlg dlg;
  274. // v-marfin 60766 : Set proper dlg title for object being browsed
  275. // set the dialog window title
  276. CString sItem;
  277. sItem.LoadString(IDS_STRING_PROCESSES);
  278. dlg.m_sDlgTitle.Format(IDS_STRING_WMI_BROWSE_TITLE,sItem,GetObjectPtr()->GetSystemName());
  279. // create the instance enumerator
  280. if( ! CHECKHRESULT(dlg.m_ClassObject.Create(GetObjectPtr()->GetSystemName())) )
  281. {
  282. return;
  283. }
  284. CString sTemp;
  285. sTemp.Format(_T("\\\\%s\\%s"),GetObjectPtr()->GetSystemName(),_T("root\\cimv2"));
  286. dlg.m_ClassObject.SetNamespace(sTemp);
  287. // set the listbox title
  288. dlg.m_sTitle = sTemp;
  289. sTemp = _T("Win32_Process");
  290. BSTR bsTemp = sTemp.AllocSysString();
  291. if( ! CHECKHRESULT(dlg.m_ClassObject.CreateEnumerator(bsTemp)) )
  292. {
  293. ::SysFreeString(bsTemp);
  294. return;
  295. }
  296. ::SysFreeString(bsTemp);
  297. // display the dialog
  298. if( dlg.DoModal() == IDOK )
  299. {
  300. CString sProcess = dlg.m_sSelectedItem;
  301. CWbemClassObject ClassObject;
  302. sTemp.Format(_T("\\\\%s\\%s"),GetObjectPtr()->GetSystemName(),_T("root\\cimv2"));
  303. ClassObject.SetNamespace(sTemp);
  304. if( ! CHECKHRESULT(ClassObject.GetObject(sProcess)) )
  305. {
  306. return;
  307. }
  308. ClassObject.GetProperty(IDS_STRING_MOF_NAME,m_sProcess);
  309. m_sProcess.TrimLeft(_T("\""));
  310. m_sProcess.TrimRight(_T("\""));
  311. UpdateData(FALSE);
  312. }
  313. }
  314. void CDPServicePage::OnButtonBrowseService()
  315. {
  316. CWmiInstanceBrowseDlg dlg;
  317. // v-marfin 60766 : Set proper dlg title for object being browsed
  318. // set the dialog window title
  319. CString sItem;
  320. sItem.LoadString(IDS_STRING_SERVICES);
  321. dlg.m_sDlgTitle.Format(IDS_STRING_WMI_BROWSE_TITLE,sItem,GetObjectPtr()->GetSystemName());
  322. // create the instance enumerator
  323. if( ! CHECKHRESULT(dlg.m_ClassObject.Create(GetObjectPtr()->GetSystemName())) )
  324. {
  325. return;
  326. }
  327. CString sTemp;
  328. sTemp.Format(_T("\\\\%s\\%s"),GetObjectPtr()->GetSystemName(),_T("root\\cimv2"));
  329. dlg.m_ClassObject.SetNamespace(sTemp);
  330. // set the listbox title
  331. dlg.m_sTitle = sTemp;
  332. sTemp = _T("Win32_Service");
  333. BSTR bsTemp = sTemp.AllocSysString();
  334. if( ! CHECKHRESULT(dlg.m_ClassObject.CreateEnumerator(bsTemp)) )
  335. {
  336. ::SysFreeString(bsTemp);
  337. return;
  338. }
  339. ::SysFreeString(bsTemp);
  340. // display the dialog
  341. if( dlg.DoModal() == IDOK )
  342. {
  343. m_sService = dlg.m_sSelectedItem;
  344. int iIndex = m_sService.Find(_T("Name="));
  345. m_sService = m_sService.Right(m_sService.GetLength()-iIndex-5);
  346. m_sService.TrimLeft(_T("\""));
  347. m_sService.TrimRight(_T("\""));
  348. m_iType = 0;
  349. UpdateData(FALSE);
  350. }
  351. }
  352. void CDPServicePage::OnChangeEditProcess()
  353. {
  354. // TODO: If this is a RICHEDIT control, the control will not
  355. // send this notification unless you override the CHMPropertyPage::OnInitDialog()
  356. // function and call CRichEditCtrl().SetEventMask()
  357. // with the ENM_CHANGE flag ORed into the mask.
  358. UpdateData();
  359. SetModified();
  360. }
  361. void CDPServicePage::OnRadio1()
  362. {
  363. UpdateData();
  364. SetModified();
  365. }
  366. void CDPServicePage::OnRadio2()
  367. {
  368. UpdateData();
  369. SetModified();
  370. }
  371. void CDPServicePage::OnChangeEditService()
  372. {
  373. // TODO: If this is a RICHEDIT control, the control will not
  374. // send this notification unless you override the CHMPropertyPage::OnInitDialog()
  375. // function and call CRichEditCtrl().SetEventMask()
  376. // with the ENM_CHANGE flag ORed into the mask.
  377. UpdateData();
  378. SetModified();
  379. }
  380. BOOL CDPServicePage::OnApply()
  381. {
  382. if( ! CHMPropertyPage::OnApply() )
  383. {
  384. return FALSE;
  385. }
  386. // v-marfin 62585 : So we can set the collector's state to enabled when OK pressed.
  387. m_bOnApplyUsed=TRUE;
  388. UpdateData();
  389. CStringArray saCounters;
  390. if( m_iType == 0 )
  391. {
  392. if( m_ServiceProperties.GetItemCount() <= 0 )
  393. {
  394. return FALSE;
  395. }
  396. for( int i = 0; i < m_ServiceProperties.GetItemCount(); i++ )
  397. {
  398. if( m_ServiceProperties.GetCheck(i) )
  399. {
  400. saCounters.Add(m_ServiceProperties.GetItemText(i,0));
  401. }
  402. }
  403. }
  404. else if( m_iType == 1 )
  405. {
  406. if( m_ProcessProperties.GetItemCount() <= 0 )
  407. {
  408. return FALSE;
  409. }
  410. for( int i = 0; i < m_ProcessProperties.GetItemCount(); i++ )
  411. {
  412. if( m_ProcessProperties.GetCheck(i) )
  413. {
  414. saCounters.Add(m_ProcessProperties.GetItemText(i,0));
  415. }
  416. }
  417. }
  418. // if( ! saCounters.GetSize() )
  419. // {
  420. // return FALSE;
  421. // }
  422. CWbemClassObject* pClassObject = GetObjectPtr()->GetClassObject();
  423. if( ! GfxCheckObjPtr(pClassObject,CWbemClassObject) )
  424. {
  425. return FALSE;
  426. }
  427. CString sNamespace = _T("root\\cimv2");
  428. pClassObject->SetProperty(IDS_STRING_MOF_TARGETNAMESPACE,sNamespace);
  429. CString sQuery;
  430. if( m_iType == 0 )
  431. {
  432. sQuery.Format(_T("select * from Win32_Service where Name=\"%s\""),m_sService);
  433. pClassObject->SetProperty(IDS_STRING_MOF_QUERY,sQuery);
  434. }
  435. else if( m_iType == 1 )
  436. {
  437. sQuery.Format(_T("select * from Win32_Process where Name=\"%s\""),m_sProcess);
  438. pClassObject->SetProperty(IDS_STRING_MOF_QUERY,sQuery);
  439. }
  440. pClassObject->SetProperty(IDS_STRING_MOF_STATISTICSPROPERTYNAMES,saCounters);
  441. bool bReset = m_bRequireReset ? true : false;
  442. pClassObject->SetProperty(IDS_STRING_MOF_REQUIRERESET,bReset);
  443. pClassObject->SaveAllProperties();
  444. delete pClassObject;
  445. SetModified(FALSE);
  446. return TRUE;
  447. }
  448. void CDPServicePage::OnCheckRequireReset()
  449. {
  450. UpdateData();
  451. SetModified();
  452. }
  453. void CDPServicePage::OnClickListProcessProperties2(NMHDR* pNMHDR, LRESULT* pResult)
  454. {
  455. SetModified();
  456. *pResult = 0;
  457. }
  458. void CDPServicePage::OnClickListServiceProperties(NMHDR* pNMHDR, LRESULT* pResult)
  459. {
  460. SetModified();
  461. *pResult = 0;
  462. }