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.

297 lines
6.3 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. // MethodsPg.cpp : implementation file
  8. //
  9. #include "stdafx.h"
  10. #include "wmitest.h"
  11. #include "MethodsPg.h"
  12. #include "WMITestDoc.h"
  13. #include "MainFrm.h"
  14. #include "OpWrap.h"
  15. #include "ParamsPg.h"
  16. #include "PropQualsPg.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CMethodsPg property page
  24. IMPLEMENT_DYNCREATE(CMethodsPg, CPropertyPage)
  25. CMethodsPg::CMethodsPg() : CPropertyPage(CMethodsPg::IDD)
  26. {
  27. //{{AFX_DATA_INIT(CMethodsPg)
  28. // NOTE: the ClassWizard will add member initialization here
  29. //}}AFX_DATA_INIT
  30. }
  31. CMethodsPg::~CMethodsPg()
  32. {
  33. }
  34. void CMethodsPg::DoDataExchange(CDataExchange* pDX)
  35. {
  36. CPropertyPage::DoDataExchange(pDX);
  37. //{{AFX_DATA_MAP(CMethodsPg)
  38. DDX_Control(pDX, IDC_METHODS, m_ctlMethods);
  39. //}}AFX_DATA_MAP
  40. }
  41. BEGIN_MESSAGE_MAP(CMethodsPg, CPropertyPage)
  42. //{{AFX_MSG_MAP(CMethodsPg)
  43. ON_BN_CLICKED(IDC_ADD, OnAdd)
  44. ON_BN_CLICKED(IDC_EDIT, OnEdit)
  45. ON_BN_CLICKED(IDC_DELETE, OnDelete)
  46. ON_NOTIFY(NM_DBLCLK, IDC_METHODS, OnDblclkMethods)
  47. ON_NOTIFY(LVN_ITEMCHANGED, IDC_METHODS, OnItemchangedMethods)
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMethodsPg message handlers
  52. void CMethodsPg::OnAdd()
  53. {
  54. CPropertySheet sheet(IDS_EDIT_NEW_METHOD);
  55. CParamsPg pgParams;
  56. pgParams.m_bNewMethod = TRUE;
  57. pgParams.m_pClass = m_pObj;
  58. sheet.AddPage(&pgParams);
  59. if (sheet.DoModal() == IDOK)
  60. {
  61. HRESULT hr;
  62. hr =
  63. m_pObj->PutMethod(
  64. _bstr_t(pgParams.m_strName),
  65. 0,
  66. pgParams.m_pObjIn,
  67. pgParams.m_pObjOut);
  68. if (SUCCEEDED(hr))
  69. AddMethod(pgParams.m_strName);
  70. else
  71. CWMITestDoc::DisplayWMIErrorBox(hr);
  72. }
  73. }
  74. void CMethodsPg::OnEdit()
  75. {
  76. int iItem = GetSelectedItem();
  77. if (iItem != -1)
  78. {
  79. CPropertySheet sheet(IDS_EDIT_METHOD);
  80. CParamsPg pgParams;
  81. CPropQualsPg pgQuals;
  82. pgParams.m_strName = m_ctlMethods.GetItemText(iItem, 0);
  83. pgParams.m_bNewMethod = FALSE;
  84. pgParams.m_pClass = m_pObj;
  85. sheet.AddPage(&pgParams);
  86. pgQuals.m_pObj = m_pObj;
  87. pgQuals.m_bIsInstance = FALSE;
  88. pgQuals.m_mode = CPropQualsPg::QMODE_METHOD;
  89. pgQuals.m_strMethodName = pgParams.m_strName;
  90. sheet.AddPage(&pgQuals);
  91. if (sheet.DoModal() == IDOK)
  92. {
  93. HRESULT hr;
  94. hr =
  95. m_pObj->PutMethod(
  96. _bstr_t(pgParams.m_strName),
  97. 0,
  98. pgParams.m_pObjIn,
  99. pgParams.m_pObjOut);
  100. if (SUCCEEDED(hr))
  101. {
  102. LV_ITEM item;
  103. item.mask = LVIF_IMAGE;
  104. item.iItem = iItem;
  105. item.iImage = GetMethodImage(pgParams.m_strName);
  106. m_ctlMethods.SetItem(&item);
  107. }
  108. else
  109. CWMITestDoc::DisplayWMIErrorBox(hr);
  110. }
  111. }
  112. }
  113. void CMethodsPg::OnDelete()
  114. {
  115. int iItem = GetSelectedItem();
  116. if (iItem != -1)
  117. {
  118. HRESULT hr;
  119. CString strName = m_ctlMethods.GetItemText(iItem, 0);
  120. if (SUCCEEDED(hr =
  121. m_pObj->DeleteMethod(_bstr_t(strName))))
  122. {
  123. m_ctlMethods.DeleteItem(iItem);
  124. int nItems = m_ctlMethods.GetItemCount();
  125. if (iItem >= nItems)
  126. iItem--;
  127. if (iItem >= 0)
  128. m_ctlMethods.SetItemState(iItem, LVIS_SELECTED, LVIS_SELECTED);
  129. }
  130. else
  131. CWMITestDoc::DisplayWMIErrorBox(hr);
  132. }
  133. }
  134. void CMethodsPg::OnDblclkMethods(NMHDR* pNMHDR, LRESULT* pResult)
  135. {
  136. OnEdit();
  137. *pResult = 0;
  138. }
  139. void CMethodsPg::OnItemchangedMethods(NMHDR* pNMHDR, LRESULT* pResult)
  140. {
  141. UpdateButtons();
  142. *pResult = 0;
  143. }
  144. BOOL CMethodsPg::OnInitDialog()
  145. {
  146. CPropertyPage::OnInitDialog();
  147. InitListCtrl();
  148. LoadMethods();
  149. return TRUE; // return TRUE unless you set the focus to a control
  150. // EXCEPTION: OCX Property Pages should return FALSE
  151. }
  152. void CMethodsPg::UpdateButtons()
  153. {
  154. int iIndex = GetSelectedItem();
  155. GetDlgItem(IDC_DELETE)->EnableWindow(iIndex != -1);
  156. GetDlgItem(IDC_EDIT)->EnableWindow(iIndex != -1);
  157. }
  158. int CMethodsPg::GetSelectedItem()
  159. {
  160. POSITION pos = m_ctlMethods.GetFirstSelectedItemPosition();
  161. if (pos)
  162. return m_ctlMethods.GetNextSelectedItem(pos);
  163. else
  164. return -1;
  165. }
  166. void CMethodsPg::InitListCtrl()
  167. {
  168. RECT rect;
  169. CString strTemp;
  170. m_ctlMethods.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  171. m_ctlMethods.SetImageList(&((CMainFrame *) AfxGetMainWnd())->m_imageList,
  172. LVSIL_SMALL);
  173. m_ctlMethods.GetClientRect(&rect);
  174. strTemp.LoadString(IDS_NAME);
  175. m_ctlMethods.InsertColumn(0, strTemp, LVCFMT_LEFT, rect.right);
  176. }
  177. void CMethodsPg::LoadMethods()
  178. {
  179. HRESULT hr;
  180. m_ctlMethods.DeleteAllItems();
  181. int iItem = 0;
  182. if (SUCCEEDED(hr = m_pObj->BeginMethodEnumeration(0)))
  183. {
  184. BSTR pName = NULL;
  185. while(1)
  186. {
  187. hr =
  188. m_pObj->NextMethod(
  189. 0,
  190. &pName,
  191. NULL,
  192. NULL);
  193. if (FAILED(hr) || hr == WBEM_S_NO_MORE_DATA)
  194. break;
  195. AddMethod(_bstr_t(pName));
  196. SysFreeString(pName);
  197. }
  198. }
  199. UpdateButtons();
  200. if (FAILED(hr))
  201. CWMITestDoc::DisplayWMIErrorBox(hr);
  202. }
  203. int CMethodsPg::GetMethodImage(LPCTSTR szName)
  204. {
  205. IWbemQualifierSetPtr pQuals;
  206. HRESULT hr;
  207. int iImage = IMAGE_OBJECT;
  208. if (SUCCEEDED(hr = m_pObj->GetMethodQualifierSet(
  209. _bstr_t(szName),
  210. &pQuals)))
  211. {
  212. _variant_t vStatic;
  213. if (SUCCEEDED(hr = pQuals->Get(
  214. L"static",
  215. 0,
  216. &vStatic,
  217. NULL)) && vStatic.vt == VT_BOOL && (bool) vStatic == true)
  218. {
  219. iImage = IMAGE_CLASS;
  220. }
  221. }
  222. return iImage;
  223. }
  224. void CMethodsPg::AddMethod(LPCTSTR szName)
  225. {
  226. int iImage = GetMethodImage(szName),
  227. iItem = m_ctlMethods.GetItemCount();
  228. m_ctlMethods.InsertItem(iItem, szName, iImage);
  229. }