Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
7.4 KiB

  1. //-------------------------------------------------------------------------
  2. // File: WMIFilterBrowserDlg.cpp
  3. //
  4. // Author : Kishnan Nedungadi
  5. //
  6. // created : 3/27/2000
  7. //-------------------------------------------------------------------------
  8. #include "stdafx.h"
  9. #include <wbemidl.h>
  10. #include <commctrl.h>
  11. #include "resource.h"
  12. #include "defines.h"
  13. #include "ntdmutils.h"
  14. #include "SchemaManager.h"
  15. #include "WMIFilterBrowser.h"
  16. #include "WMIFilterBrowserDlg.h"
  17. #include "DlgFilterProperties.h"
  18. CWMIFilterBrowserDlg * g_pWMIFilterBrowserDlg = NULL;
  19. extern CFilterPropertiesDlg * g_pFilterProperties;
  20. //-------------------------------------------------------------------------
  21. INT_PTR CALLBACK WMIFilterBrowserDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  22. {
  23. if(g_pWMIFilterBrowserDlg)
  24. {
  25. return g_pWMIFilterBrowserDlg->WMIFilterBrowserDlgProc(hDlg, iMessage, wParam, lParam);
  26. }
  27. return FALSE;
  28. }
  29. //-------------------------------------------------------------------------
  30. CWMIFilterBrowserDlg::CWMIFilterBrowserDlg(CWMIFilterBrowser * pWMIFilterBrowser)
  31. {
  32. _ASSERT(pWMIFilterBrowser);
  33. m_hWnd = NULL;
  34. m_hwndListView = NULL;
  35. m_pIWbemClassObject = NULL;
  36. m_pWMIFilterBrowser = pWMIFilterBrowser;
  37. }
  38. //-------------------------------------------------------------------------
  39. CWMIFilterBrowserDlg::~CWMIFilterBrowserDlg()
  40. {
  41. NTDM_RELEASE_IF_NOT_NULL(m_pIWbemClassObject);
  42. }
  43. //-------------------------------------------------------------------------
  44. INT_PTR CALLBACK CWMIFilterBrowserDlg::WMIFilterBrowserDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  45. {
  46. LPPSHNOTIFY lppsn = NULL;
  47. switch(iMessage)
  48. {
  49. case WM_INITDIALOG:
  50. {
  51. m_hWnd = hDlg;
  52. InitializeDialog();
  53. PopulateFilterList();
  54. break;
  55. }
  56. case WM_DESTROY:
  57. {
  58. DestroyDialog();
  59. break;
  60. }
  61. case WM_COMMAND:
  62. {
  63. if(BN_CLICKED == HIWORD(wParam) && IDC_VIEW == LOWORD(wParam))
  64. {
  65. ViewSelectedItem();
  66. }
  67. switch(LOWORD(wParam))
  68. {
  69. case IDOK:
  70. OnOK();
  71. return TRUE;
  72. break;
  73. case IDCANCEL:
  74. EndDialog(m_hWnd, IDCANCEL);
  75. return TRUE;
  76. }
  77. break;
  78. }
  79. case WM_NOTIFY:
  80. {
  81. LPNMHDR lpnm = (LPNMHDR) lParam;
  82. switch (lpnm->code)
  83. {
  84. case NM_DBLCLK :
  85. {
  86. if(lpnm->idFrom == IDC_ALL_FILTERS)
  87. {
  88. ViewSelectedItem();
  89. return TRUE;
  90. }
  91. break;
  92. }
  93. default :
  94. break;
  95. }
  96. }
  97. }
  98. return FALSE;
  99. }
  100. //---------------------------------------------------------------------------
  101. STDMETHODIMP CWMIFilterBrowserDlg::InitializeDialog()
  102. {
  103. HRESULT hr;
  104. LVCOLUMN lvColumn;
  105. CComBSTR bstrName;
  106. NTDM_BEGIN_METHOD()
  107. bstrName.LoadString(_Module.GetResourceInstance(), IDS_NAME);
  108. //Initialize the ListView Control
  109. m_hwndListView = GetDlgItem(m_hWnd, IDC_ALL_FILTERS);
  110. NTDM_ERR_IF_NULL(m_hwndListView);
  111. ListView_SetExtendedListViewStyle(m_hwndListView, LVS_EX_FULLROWSELECT);
  112. lvColumn.mask = LVCF_TEXT|LVCF_FMT;
  113. lvColumn.fmt = LVCFMT_LEFT;
  114. lvColumn.pszText = bstrName;
  115. NTDM_ERR_IF_MINUSONE(ListView_InsertColumn(m_hwndListView, 0, &lvColumn));
  116. NTDM_ERR_IF_FALSE(ListView_SetColumnWidth(m_hwndListView, 0, LVSCW_AUTOSIZE_USEHEADER));
  117. NTDM_END_METHOD()
  118. // cleanup
  119. return hr;
  120. }
  121. //---------------------------------------------------------------------------
  122. STDMETHODIMP CWMIFilterBrowserDlg::DestroyDialog()
  123. {
  124. HRESULT hr;
  125. LVITEM lvItem;
  126. long lCount;
  127. NTDM_BEGIN_METHOD()
  128. //Release each item in the ListView Control
  129. lvItem.mask = LVIF_PARAM;
  130. lvItem.iSubItem = 0;
  131. lCount = ListView_GetItemCount(m_hwndListView);
  132. while(lCount > 0)
  133. {
  134. lCount--;
  135. lvItem.iItem = lCount;
  136. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  137. if(lvItem.lParam)
  138. {
  139. ((IWbemClassObject *)lvItem.lParam)->Release();
  140. }
  141. }
  142. NTDM_END_METHOD()
  143. // cleanup
  144. return hr;
  145. }
  146. //---------------------------------------------------------------------------
  147. BOOL CWMIFilterBrowserDlg::OnOK()
  148. {
  149. HRESULT hr;
  150. long lSelectionMark;
  151. NTDM_BEGIN_METHOD()
  152. lSelectionMark = ListView_GetSelectionMark(m_hwndListView);
  153. if(-1 == lSelectionMark)
  154. {
  155. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  156. goto error;
  157. }
  158. else
  159. {
  160. // get a pointer to the IWbemClassObject
  161. LVITEM lvItem;
  162. lvItem.mask = LVIF_PARAM;
  163. lvItem.iSubItem = 0;
  164. lvItem.iItem = lSelectionMark;
  165. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  166. if(lvItem.lParam)
  167. {
  168. m_pIWbemClassObject = (IWbemClassObject *)lvItem.lParam;
  169. m_pIWbemClassObject->AddRef();
  170. }
  171. }
  172. EndDialog(m_hWnd, IDOK);
  173. NTDM_END_METHOD()
  174. // cleanup
  175. return TRUE;
  176. }
  177. //---------------------------------------------------------------------------
  178. STDMETHODIMP CWMIFilterBrowserDlg::PopulateFilterList()
  179. {
  180. HRESULT hr;
  181. CComPtr<IEnumWbemClassObject> pEnumWbemClassObject;
  182. CComBSTR bstrClass(_T("MSFT_SomFilter"));
  183. ULONG uReturned;
  184. NTDM_BEGIN_METHOD()
  185. if(!m_pWMIFilterBrowser->m_pIWbemServices)
  186. NTDM_EXIT(E_FAIL);
  187. // Get the Enumeration
  188. NTDM_ERR_IF_FAIL(m_pWMIFilterBrowser->m_pIWbemServices->CreateInstanceEnum(bstrClass, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbemClassObject));
  189. // Loop through each item in the enumeration and add it to the list
  190. while(true)
  191. {
  192. IWbemClassObject *pIWbemClassObject = NULL;
  193. NTDM_ERR_IF_FAIL(pEnumWbemClassObject->Next(WBEM_INFINITE, 1, &pIWbemClassObject, &uReturned));
  194. if(!uReturned)
  195. break;
  196. // Add current Item to the list
  197. AddItemToList(pIWbemClassObject);
  198. pIWbemClassObject->Release();
  199. }
  200. ListView_SetItemState(m_hwndListView, 0, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
  201. NTDM_END_METHOD()
  202. // cleanup
  203. return hr;
  204. }
  205. //---------------------------------------------------------------------------
  206. STDMETHODIMP CWMIFilterBrowserDlg::AddItemToList(IWbemClassObject * pIWbemClassObject)
  207. {
  208. HRESULT hr;
  209. CComVariant vValue;
  210. CIMTYPE vType;
  211. LVITEM lvItem;
  212. NTDM_BEGIN_METHOD()
  213. NTDM_ERR_IF_FAIL(pIWbemClassObject->Get(_T("Name"), 0, &vValue, &vType, NULL));
  214. lvItem.mask = LVIF_TEXT|LVIF_PARAM;
  215. lvItem.iItem = 0;
  216. lvItem.iSubItem = 0;
  217. lvItem.pszText = vValue.bstrVal;
  218. lvItem.lParam = (LPARAM)pIWbemClassObject;
  219. NTDM_ERR_IF_MINUSONE(ListView_InsertItem(m_hwndListView, &lvItem));
  220. pIWbemClassObject->AddRef();
  221. NTDM_END_METHOD()
  222. // cleanup
  223. return hr;
  224. }
  225. //---------------------------------------------------------------------------
  226. STDMETHODIMP CWMIFilterBrowserDlg::ViewSelectedItem()
  227. {
  228. HRESULT hr;
  229. long lSelectionMark;
  230. NTDM_BEGIN_METHOD()
  231. lSelectionMark = ListView_GetSelectionMark(m_hwndListView);
  232. if(-1 == lSelectionMark)
  233. {
  234. CNTDMUtils::DisplayMessage(m_hWnd, IDS_ERR_NO_FILTER_SELECTED);
  235. NTDM_EXIT(E_FAIL);
  236. }
  237. else
  238. {
  239. // get a pointer to the IWbemClassObject
  240. LVITEM lvItem;
  241. lvItem.mask = LVIF_PARAM;
  242. lvItem.iSubItem = 0;
  243. lvItem.iItem = lSelectionMark;
  244. NTDM_ERR_IF_FALSE(ListView_GetItem(m_hwndListView, &lvItem));
  245. if(lvItem.lParam)
  246. {
  247. g_pFilterProperties = new CFilterPropertiesDlg(m_pWMIFilterBrowser->m_pIWbemServices, (IWbemClassObject *)lvItem.lParam);
  248. DialogBox(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDD_FILTER_PROPERTIES), m_hWnd, FilterPropertiesDlgProc);
  249. }
  250. }
  251. NTDM_END_METHOD()
  252. // cleanup
  253. return hr;
  254. }