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.

315 lines
8.1 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. // NamespaceDlg.cpp : implementation file
  8. //
  9. #include "stdafx.h"
  10. #include "wmitest.h"
  11. #include "WMITestDoc.h"
  12. #include "NamespaceDlg.h"
  13. #include "MainFrm.h"
  14. //#include <cominit.h>
  15. #include "utils.h"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CNamespaceDlg dialog
  23. CNamespaceDlg::CNamespaceDlg(CWnd* pParent /*=NULL*/)
  24. : CDialog(CNamespaceDlg::IDD, pParent)
  25. {
  26. //{{AFX_DATA_INIT(CNamespaceDlg)
  27. m_strNamespace = _T("");
  28. //}}AFX_DATA_INIT
  29. }
  30. void CNamespaceDlg::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CDialog::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(CNamespaceDlg)
  34. DDX_Control(pDX, IDC_NAMESPACE_TREE, m_ctlNamespace);
  35. DDX_Text(pDX, IDC_NAMESPACE, m_strNamespace);
  36. //}}AFX_DATA_MAP
  37. }
  38. BEGIN_MESSAGE_MAP(CNamespaceDlg, CDialog)
  39. //{{AFX_MSG_MAP(CNamespaceDlg)
  40. ON_NOTIFY(TVN_SELCHANGED, IDC_NAMESPACE_TREE, OnSelchangedNamespaceTree)
  41. //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CNamespaceDlg message handlers
  45. BOOL CNamespaceDlg::OnInitDialog()
  46. {
  47. CDialog::OnInitDialog();
  48. m_ctlNamespace.SetImageList(
  49. &((CMainFrame *) AfxGetMainWnd())->m_imageList,
  50. TVSIL_NORMAL);
  51. if (!PopulateTree())
  52. OnCancel();
  53. return TRUE; // return TRUE unless you set the focus to a control
  54. // EXCEPTION: OCX Property Pages should return FALSE
  55. }
  56. _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator));
  57. _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices));
  58. _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject));
  59. _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
  60. typedef CList<_bstr_t, LPCWSTR> CBstrList;
  61. BOOL CNamespaceDlg::AddNamespaceToTree(
  62. HTREEITEM hitemParent,
  63. LPCWSTR szNamespace,
  64. IWbemLocator *pLocator,
  65. BSTR pUser,
  66. BSTR pPassword,
  67. BSTR pAuthority,
  68. DWORD dwImpLevel,
  69. DWORD dwAuthLevel)
  70. {
  71. HRESULT hr;
  72. IWbemServicesPtr pNamespace;
  73. if (SUCCEEDED(hr = pLocator->ConnectServer(
  74. _bstr_t(szNamespace),
  75. pUser, // username
  76. pPassword, // password
  77. 0, // locale
  78. 0, // securityFlags
  79. pAuthority, // authority (domain for NTLM)
  80. NULL, // context
  81. &pNamespace)))
  82. {
  83. SetSecurityHelper(
  84. pNamespace,
  85. pAuthority,
  86. pUser,
  87. pPassword,
  88. dwImpLevel,
  89. dwAuthLevel);
  90. IEnumWbemClassObjectPtr pEnum;
  91. if (SUCCEEDED(hr =
  92. pNamespace->ExecQuery(
  93. _bstr_t(L"WQL"),
  94. _bstr_t(L"SELECT NAME FROM __NAMESPACE"),
  95. WBEM_FLAG_USE_AMENDED_QUALIFIERS,
  96. NULL,
  97. &pEnum)))
  98. {
  99. IWbemClassObjectPtr pObj;
  100. DWORD dwCount;
  101. CBstrList listNamespaces;
  102. _variant_t vNamespace;
  103. HTREEITEM hitem;
  104. LPCWSTR szBaseName = wcsrchr(szNamespace, '\\');
  105. if (szBaseName)
  106. szBaseName++;
  107. else
  108. szBaseName = szNamespace;
  109. // Insert the parent into the tree.
  110. hitem =
  111. m_ctlNamespace.InsertItem(
  112. _bstr_t(szBaseName),
  113. IMAGE_ROOT,
  114. IMAGE_ROOT,
  115. hitemParent);
  116. SetSecurityHelper(
  117. pEnum,
  118. pAuthority,
  119. pUser,
  120. pPassword,
  121. dwImpLevel,
  122. dwAuthLevel);
  123. // Enum the child namespace names and put them into a list. We're
  124. // putting them into a list so we don't have a bunch of namespaces
  125. // open like we would if we were to recursively call this function
  126. // without first building a list and then closing the currently
  127. // open namespace.
  128. while (SUCCEEDED(hr =
  129. pEnum->Next(
  130. WBEM_INFINITE,
  131. 1,
  132. &pObj,
  133. &dwCount)) &&
  134. dwCount == 1 &&
  135. SUCCEEDED(hr =
  136. pObj->Get(
  137. L"NAME",
  138. 0,
  139. &vNamespace,
  140. NULL,
  141. NULL)))
  142. {
  143. listNamespaces.AddTail(V_BSTR(&vNamespace));
  144. }
  145. // This will release it.
  146. pNamespace = NULL;
  147. POSITION pos = listNamespaces.GetHeadPosition();
  148. while (pos)
  149. {
  150. WCHAR szNewNamespace[256];
  151. swprintf(
  152. szNewNamespace,
  153. L"%s\\%s",
  154. szNamespace,
  155. (LPWSTR) listNamespaces.GetNext(pos));
  156. if (!AddNamespaceToTree(
  157. hitem,
  158. szNewNamespace,
  159. pLocator,
  160. pUser,
  161. pPassword,
  162. pAuthority,
  163. dwImpLevel,
  164. dwAuthLevel))
  165. {
  166. hr = WBEM_E_FAILED;
  167. break;
  168. }
  169. }
  170. m_ctlNamespace.SortChildren(hitem);
  171. }
  172. }
  173. if (FAILED(hr))
  174. CWMITestDoc::DisplayWMIErrorBox(hr);
  175. return SUCCEEDED(hr);
  176. }
  177. BOOL CNamespaceDlg::PopulateTree()
  178. {
  179. HRESULT hr;
  180. IWbemLocatorPtr pLocator;
  181. IWbemServicesPtr pSvc;
  182. CWaitCursor wait;
  183. CString strNamespace = m_strNamespace;
  184. int iWhere;
  185. BOOL bRet;
  186. strNamespace.MakeUpper();
  187. // See if we can find a server name.
  188. if ((iWhere = strNamespace.Find("\\ROOT")) != -1 && iWhere != 0)
  189. m_strServer = m_strNamespace.Left(iWhere);
  190. // If we get something like \\server\root\blah we need to strip off
  191. // everything after the \\server\root.
  192. if ((iWhere = strNamespace.Find("ROOT\\")) != -1)
  193. strNamespace = m_strNamespace.Left(iWhere + 4);
  194. if ((hr = CoCreateInstance(
  195. CLSID_WbemLocator,
  196. NULL,
  197. CLSCTX_INPROC_SERVER,
  198. IID_IWbemLocator,
  199. (LPVOID *) &pLocator)) == S_OK)
  200. {
  201. // Using the locator, connect to CIMOM in the given namespace.
  202. BSTR pUser = m_strUser.IsEmpty() ? NULL :
  203. m_strUser.AllocSysString(),
  204. pPassword = m_bNullPassword || !pUser ? NULL :
  205. m_strPassword.AllocSysString(),
  206. pAuthority = m_strAuthority.IsEmpty() ? NULL :
  207. m_strAuthority.AllocSysString();
  208. bRet =
  209. AddNamespaceToTree(
  210. TVI_ROOT,
  211. _bstr_t(strNamespace),
  212. pLocator, pUser, pPassword, pAuthority,
  213. m_dwImpLevel,
  214. m_dwAuthLevel);
  215. m_ctlNamespace.Expand(m_ctlNamespace.GetRootItem(), TVE_EXPAND);
  216. // Done with BSTR vars.
  217. if (pUser)
  218. SysFreeString(pUser);
  219. if (pPassword)
  220. SysFreeString(pPassword);
  221. if (pAuthority)
  222. SysFreeString(pAuthority);
  223. }
  224. else
  225. {
  226. CWMITestDoc::DisplayWMIErrorBox(hr);
  227. bRet = FALSE;
  228. }
  229. return bRet;
  230. }
  231. void CNamespaceDlg::RefreshNamespaceText()
  232. {
  233. CString strNamespace;
  234. HTREEITEM hitem = m_ctlNamespace.GetSelectedItem();
  235. while (hitem != NULL)
  236. {
  237. CString strItem = m_ctlNamespace.GetItemText(hitem);
  238. if (!strNamespace.IsEmpty())
  239. strNamespace = strItem + "\\" + strNamespace;
  240. else
  241. strNamespace = strItem;
  242. hitem = m_ctlNamespace.GetParentItem(hitem);
  243. }
  244. if (!m_strServer.IsEmpty())
  245. strNamespace = m_strServer + "\\" + strNamespace;
  246. SetDlgItemText(IDC_NAMESPACE, strNamespace);
  247. }
  248. void CNamespaceDlg::OnSelchangedNamespaceTree(NMHDR* pNMHDR, LRESULT* pResult)
  249. {
  250. NM_TREEVIEW *pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  251. if ((pNMTreeView->itemNew.state & TVIS_SELECTED))
  252. RefreshNamespaceText();
  253. *pResult = 0;
  254. }