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.

1273 lines
32 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. // WMITestDoc.cpp : implementation of the CWMITestDoc class
  8. //
  9. #include "stdafx.h"
  10. //#include <cominit.h>
  11. #include "utils.h"
  12. #include "WMITest.h"
  13. #include "OpWrap.h"
  14. #include "WMITestDoc.h"
  15. #include "LoginDlg.h"
  16. #include "GetTextDlg.h"
  17. #include "MainFrm.h"
  18. #include "OpView.h"
  19. #include "ObjVw.h"
  20. #include "PrefDlg.h"
  21. #include "ErrorDlg.h"
  22. #include "PropsPg.H"
  23. #include "PropQualsPg.h"
  24. #include "ExecMethodDlg.h"
  25. #include "MofDlg.h"
  26. #include "BindingSheet.h"
  27. #include "FilterPg.h"
  28. #include "ConsumerPg.h"
  29. #include "BindingPg.h"
  30. #include "ExportDlg.h"
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CWMITestDoc
  38. IMPLEMENT_DYNCREATE(CWMITestDoc, CDocument)
  39. BEGIN_MESSAGE_MAP(CWMITestDoc, CDocument)
  40. //{{AFX_MSG_MAP(CWMITestDoc)
  41. ON_COMMAND(ID_CONNECT, OnConnect)
  42. ON_UPDATE_COMMAND_UI_RANGE(ID_FIRST_NEED_CONNECTION, ID_LAST_NEED_CONNECTION, OnUpdateAgainstConnection)
  43. ON_COMMAND(ID_QUERY, OnQuery)
  44. ON_COMMAND(ID_NOTIFICATIONQUERY, OnNotificationQuery)
  45. ON_COMMAND(ID_STOP, OnStop)
  46. //ON_UPDATE_COMMAND_UI(ID_STOP, OnUpdateStop)
  47. ON_COMMAND(ID_REFRESH_ALL, OnRefreshAll)
  48. ON_COMMAND(ID_ENUMERATEINSTANCES, OnEnumerateInstances)
  49. ON_COMMAND(ID_ENUMERATE_CLASSES, OnEnumerateClasses)
  50. ON_COMMAND(ID_GET_CLASS, OnGetClass)
  51. ON_COMMAND(ID_GETINSTANCE, OnGetInstance)
  52. ON_COMMAND(ID_REFRESH_CURRENT, OnRefreshCurrent)
  53. ON_COMMAND(ID_ASSOCIATORS, OnAssociators)
  54. ON_UPDATE_COMMAND_UI(ID_ASSOCIATORS, OnUpdateAssociators)
  55. ON_COMMAND(ID_REFERENCES, OnReferences)
  56. ON_COMMAND(ID_INST_GET_CLASS, OnInstGetClass)
  57. ON_COMMAND(ID_INST_GET_INST, OnInstGetInst)
  58. ON_COMMAND(ID_CLASS_INSTANCES, OnClassInstances)
  59. ON_COMMAND(ID_CLASS_SUPERCLASS, OnClassSuperclass)
  60. ON_COMMAND(ID_CLASS_INSTANCES_DEEP, OnClassInstancesDeep)
  61. ON_COMMAND(ID_CLASS_SUBCLASSES_DEEP, OnClassSubclassesDeep)
  62. ON_COMMAND(ID_CLASS_SUBCLASSES, OnClassSubclasses)
  63. ON_COMMAND(ID_OPTIONS, OnOptions)
  64. ON_COMMAND(ID_SYSTEM_PROPS, OnSystemProps)
  65. ON_UPDATE_COMMAND_UI(ID_SYSTEM_PROPS, OnUpdateSystemProps)
  66. ON_COMMAND(ID_INHERITED_PROPS, OnInheritedProps)
  67. ON_UPDATE_COMMAND_UI(ID_INHERITED_PROPS, OnUpdateInheritedProps)
  68. ON_COMMAND(ID_RECONNECT, OnReconnect)
  69. ON_UPDATE_COMMAND_UI(ID_RECONNECT, OnUpdateReconnect)
  70. ON_COMMAND(ID_TRANSLATE_VALUES, OnTranslateValues)
  71. ON_UPDATE_COMMAND_UI(ID_TRANSLATE_VALUES, OnUpdateTranslateValues)
  72. ON_COMMAND(ID_SAVE, OnSave)
  73. ON_UPDATE_COMMAND_UI(ID_SAVE, OnUpdateSave)
  74. ON_COMMAND(ID_CREATE_CLASS, OnCreateClass)
  75. ON_COMMAND(ID_CREATE_INSTANCE, OnCreateInstance)
  76. ON_COMMAND(ID_CLASS_CREATE_INST, OnClassCreateInstance)
  77. ON_COMMAND(ID_ERROR_DETAILS, OnErrorDetails)
  78. ON_UPDATE_COMMAND_UI(ID_ERROR_DETAILS, OnUpdateErrorDetails)
  79. ON_COMMAND(ID_EXEC_METHOD, OnExecMethod)
  80. ON_COMMAND(ID_SHOW_MOF, OnShowMof)
  81. ON_UPDATE_COMMAND_UI(ID_SHOW_MOF, OnUpdateShowMof)
  82. ON_COMMAND(ID_EXPORT_TREE, OnExportTree)
  83. ON_COMMAND(ID_EXPORT_ITEM, OnExportItem)
  84. ON_COMMAND(ID_FILTER_BINDINGS, OnFilterBindings)
  85. ON_COMMAND(ID_STOP_CURRENT, OnStopCurrent)
  86. ON_UPDATE_COMMAND_UI(ID_REFRESH_CURRENT, OnUpdateRefreshCurrent)
  87. ON_UPDATE_COMMAND_UI(ID_REFERENCES, OnUpdateAssociators)
  88. ON_UPDATE_COMMAND_UI(ID_INST_GET_CLASS, OnUpdateAssociators)
  89. ON_UPDATE_COMMAND_UI(ID_INST_GET_INST, OnUpdateAssociators)
  90. ON_UPDATE_COMMAND_UI(ID_CLASS_INSTANCES, OnUpdateAssociators)
  91. ON_UPDATE_COMMAND_UI(ID_CLASS_SUPERCLASS, OnUpdateAssociators)
  92. ON_UPDATE_COMMAND_UI(ID_STOP_CURRENT, OnUpdateStopCurrent)
  93. //}}AFX_MSG_MAP
  94. ON_COMMAND_RANGE(IDC_EXECUTE_METHOD_FIRST, IDC_EXECUTE_METHOD_LAST, OnExecuteMethod)
  95. END_MESSAGE_MAP()
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CWMITestDoc construction/destruction
  98. #define CLIPFORMAT_PROPERTIES _T("WMITest Properties")
  99. #define CLIPFORMAT_OPS _T("WMITest Operations")
  100. CWMITestDoc::CWMITestDoc() :
  101. m_pOpView(NULL),
  102. m_pObjView(NULL),
  103. m_nBusyOps(0)
  104. //m_pPrincipal(NULL),
  105. //m_pAuthIdentity(NULL)
  106. {
  107. m_pNamespace = NULL;
  108. m_cfProps = (CLIPFORMAT) RegisterClipboardFormat(CLIPFORMAT_PROPERTIES);
  109. m_cfOps = (CLIPFORMAT) RegisterClipboardFormat(CLIPFORMAT_OPS);
  110. }
  111. CWMITestDoc::~CWMITestDoc()
  112. {
  113. }
  114. BOOL CWMITestDoc::OnNewDocument()
  115. {
  116. if (m_pOpView)
  117. m_pOpView->FlushItems();
  118. if (!CDocument::OnNewDocument())
  119. return FALSE;
  120. return TRUE;
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CWMITestDoc serialization
  124. void CWMITestDoc::Serialize(CArchive& archive)
  125. {
  126. if (archive.IsStoring())
  127. {
  128. int nCount = m_pOpView->GetOpCount();
  129. CTreeCtrl *pTree = m_pOpView->m_pTree;
  130. // This will go into the op wrappers.
  131. m_pObjView->SaveColumns();
  132. archive << m_strNamespace;
  133. archive << nCount;
  134. for (HTREEITEM hitemOp = pTree->GetChildItem(m_pOpView->m_hitemRoot);
  135. hitemOp != NULL;
  136. hitemOp = pTree->GetNextSiblingItem(hitemOp))
  137. {
  138. COpWrap *pWrap = (COpWrap*) pTree->GetItemData(hitemOp);
  139. pWrap->Serialize(archive);
  140. }
  141. }
  142. else
  143. {
  144. if (!m_pOpView)
  145. {
  146. POSITION pos = GetFirstViewPosition();
  147. m_pOpView = (COpView*) GetNextView(pos);
  148. m_pObjView = (CObjView*) GetNextView(pos);
  149. }
  150. if (m_pOpView)
  151. m_pOpView->FlushItems();
  152. int nCount;
  153. archive >> m_strNamespace;
  154. if (SUCCEEDED(Connect(FALSE)))
  155. {
  156. archive >> nCount;
  157. for (int i = 0; i < nCount; i++)
  158. {
  159. COpWrap *pWrap = new COpWrap;
  160. pWrap->Serialize(archive);
  161. m_pOpView->AddOpItem(pWrap);
  162. }
  163. }
  164. }
  165. }
  166. /////////////////////////////////////////////////////////////////////////////
  167. // CWMITestDoc diagnostics
  168. #ifdef _DEBUG
  169. void CWMITestDoc::AssertValid() const
  170. {
  171. CDocument::AssertValid();
  172. }
  173. void CWMITestDoc::Dump(CDumpContext& dc) const
  174. {
  175. CDocument::Dump(dc);
  176. }
  177. #endif //_DEBUG
  178. /////////////////////////////////////////////////////////////////////////////
  179. // CWMITestDoc commands
  180. void CWMITestDoc::OnConnect()
  181. {
  182. CLoginDlg dlg;
  183. dlg.m_strAuthority =
  184. theApp.GetProfileString(_T("Login"), _T("Authority"), _T(""));
  185. dlg.m_strNamespace =
  186. theApp.GetProfileString(_T("Login"), _T("Namespace"), _T("root\\default"));
  187. dlg.m_strUser = theApp.GetProfileString(_T("Login"), _T("User"), _T(""));
  188. dlg.m_strLocale = theApp.GetProfileString(_T("Login"), _T("Locale"), _T(""));
  189. dlg.m_dwImpLevel = theApp.GetProfileInt(_T("Login"), _T("Imp"),
  190. RPC_C_IMP_LEVEL_IMPERSONATE);
  191. dlg.m_dwAuthLevel = theApp.GetProfileInt(_T("Login"), _T("Auth"),
  192. RPC_C_AUTHN_LEVEL_CONNECT);
  193. dlg.m_bNullPassword = theApp.GetProfileInt(_T("Login"), _T("Null"), FALSE);
  194. if (dlg.DoModal() == IDOK)
  195. {
  196. Disconnect();
  197. theApp.WriteProfileString(_T("Login"), _T("Authority"), dlg.m_strAuthority);
  198. theApp.WriteProfileString(_T("Login"), _T("Namespace"), dlg.m_strNamespace);
  199. theApp.WriteProfileString(_T("Login"), _T("User"), dlg.m_strUser);
  200. theApp.WriteProfileString(_T("Login"), _T("Locale"), dlg.m_strLocale);
  201. theApp.WriteProfileInt(_T("Login"), _T("Imp"), dlg.m_dwImpLevel);
  202. theApp.WriteProfileInt(_T("Login"), _T("Auth"), dlg.m_dwAuthLevel);
  203. theApp.WriteProfileInt(_T("Login"), _T("Null"), dlg.m_bNullPassword);
  204. m_strPassword = dlg.m_strPassword;
  205. m_strNamespace = dlg.m_strNamespace;
  206. Connect(FALSE, TRUE);
  207. }
  208. }
  209. void CWMITestDoc::AutoConnect()
  210. {
  211. // Only do this if we're not already connected.
  212. if (m_pNamespace == NULL)
  213. {
  214. // If we weren't able to connect without prompting for information,
  215. // then display the connect dialog and try again.
  216. if (FAILED(Connect(TRUE, TRUE)))
  217. OnConnect();
  218. }
  219. }
  220. void CWMITestDoc::SetInterfaceSecurity(IUnknown *pUnk)
  221. {
  222. CString strAuthority =
  223. theApp.GetProfileString(_T("Login"), _T("Authority"), _T("")),
  224. strUser =
  225. theApp.GetProfileString(_T("Login"), _T("User"), _T(""));
  226. BOOL bNullPassword = theApp.GetProfileInt(_T("Login"), _T("Null"),
  227. FALSE);
  228. BSTR pUser = strUser.IsEmpty() ? NULL :
  229. strUser.AllocSysString(),
  230. pPassword = bNullPassword || !pUser ? NULL :
  231. m_strPassword.AllocSysString(),
  232. pAuthority = strAuthority.IsEmpty() ? NULL :
  233. strAuthority.AllocSysString();
  234. DWORD dwImpLevel = theApp.GetProfileInt(_T("Login"), _T("Imp"),
  235. RPC_C_IMP_LEVEL_IMPERSONATE),
  236. dwAuthLevel = theApp.GetProfileInt(_T("Login"), _T("Auth"),
  237. RPC_C_AUTHN_LEVEL_CONNECT);
  238. SetSecurityHelper(
  239. pUnk,
  240. pAuthority,
  241. pUser,
  242. pPassword,
  243. dwImpLevel,
  244. dwAuthLevel);
  245. if (pUser)
  246. SysFreeString(pUser);
  247. if (pPassword)
  248. SysFreeString(pPassword);
  249. if (pAuthority)
  250. SysFreeString(pAuthority);
  251. }
  252. HRESULT CWMITestDoc::Connect(BOOL bSilent, BOOL bFlushItems)
  253. {
  254. // Create an instance of the WbemLocator interface.
  255. IWbemLocator *pLocator = NULL;
  256. HRESULT hr;
  257. // Make sure this isn't empty.
  258. if (m_strNamespace.IsEmpty())
  259. m_strNamespace =
  260. theApp.GetProfileString(
  261. _T("Login"), _T("Namespace"), _T("root\\default"));
  262. if ((hr = CoCreateInstance(
  263. CLSID_WbemLocator,
  264. NULL,
  265. CLSCTX_INPROC_SERVER,
  266. IID_IWbemLocator,
  267. (LPVOID *) &pLocator)) == S_OK)
  268. {
  269. CString strAuthority =
  270. theApp.GetProfileString(_T("Login"), _T("Authority"), _T("")),
  271. strUser =
  272. theApp.GetProfileString(_T("Login"), _T("User"), _T("")),
  273. strLocale =
  274. theApp.GetProfileString(_T("Login"), _T("Locale"), _T(""));
  275. BOOL bNullPassword = theApp.GetProfileInt(_T("Login"), _T("Null"), FALSE);
  276. DWORD dwImpLevel = theApp.GetProfileInt(_T("Login"), _T("Imp"),
  277. RPC_C_IMP_LEVEL_IMPERSONATE),
  278. dwAuthLevel = theApp.GetProfileInt(_T("Login"), _T("Auth"),
  279. RPC_C_AUTHN_LEVEL_CONNECT);
  280. // Using the locator, connect to CIMOM in the given namespace.
  281. CWaitCursor wait;
  282. BSTR pNamespace = m_strNamespace.AllocSysString(),
  283. pUser = strUser.IsEmpty() ? NULL :
  284. strUser.AllocSysString(),
  285. pPassword = bNullPassword || !pUser ? NULL :
  286. m_strPassword.AllocSysString(),
  287. pAuthority = strAuthority.IsEmpty() ? NULL :
  288. strAuthority.AllocSysString(),
  289. pLocale = strLocale.IsEmpty() ? NULL :
  290. strLocale.AllocSysString();
  291. #ifdef SVCEX
  292. IWbemServices *pTemp = NULL;
  293. #endif
  294. if ((hr = pLocator->ConnectServer(
  295. pNamespace,
  296. pUser, // username
  297. pPassword, // password
  298. pLocale, // locale
  299. 0L, // securityFlags
  300. pAuthority, // authority (domain for NTLM)
  301. NULL, // context
  302. #ifdef SVCEX
  303. &pTemp)) == S_OK)
  304. #else
  305. &m_pNamespace)) == S_OK)
  306. #endif
  307. {
  308. #ifdef SVCEX
  309. pTemp->QueryInterface(IID_IWbemServicesEx, (void **) &m_pNamespace);
  310. pTemp->Release();
  311. #endif
  312. SetSecurityHelper(
  313. m_pNamespace,
  314. pAuthority,
  315. pUser,
  316. pPassword,
  317. dwImpLevel,
  318. dwAuthLevel);
  319. }
  320. else
  321. m_pNamespace = NULL;
  322. /*
  323. HRESULT hr;
  324. IWbemClassObject *pClass = NULL;
  325. _bstr_t strClass = L"__ProviderRegistration";
  326. IWbemServices *pSvc = NULL;
  327. m_pNamespace->QueryInterface(IID_IWbemServicesEx, (void **) &pSvc);
  328. hr =
  329. m_pNamespace->GetObject(
  330. strClass,
  331. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  332. NULL,
  333. &pClass,
  334. NULL);
  335. */
  336. if (m_pOpView)
  337. {
  338. m_pOpView->UpdateRootText();
  339. if (bFlushItems)
  340. m_pOpView->FlushItems();
  341. }
  342. // Done with BSTR vars.
  343. if (pNamespace)
  344. SysFreeString(pNamespace);
  345. if (pUser)
  346. SysFreeString(pUser);
  347. if (pPassword)
  348. SysFreeString(pPassword);
  349. if (pAuthority)
  350. SysFreeString(pAuthority);
  351. if (pLocale)
  352. SysFreeString(pLocale);
  353. // Done with pIWbemLocator.
  354. pLocator->Release();
  355. }
  356. if (!bSilent && FAILED(hr))
  357. CWMITestDoc::DisplayWMIErrorBox(hr);
  358. return hr;
  359. }
  360. void CWMITestDoc::Disconnect()
  361. {
  362. // If already connected, release m_pIWbemServices.
  363. if (m_pNamespace)
  364. {
  365. StopOps();
  366. m_pNamespace->Release();
  367. m_nBusyOps = 0;
  368. m_pNamespace = NULL;
  369. }
  370. }
  371. void CWMITestDoc::OnUpdateAgainstConnection(CCmdUI* pCmdUI)
  372. {
  373. pCmdUI->Enable(m_pNamespace != NULL);
  374. }
  375. void CWMITestDoc::OnQuery()
  376. {
  377. CGetTextDlg dlg;
  378. dlg.m_dwPromptID = IDS_QUERY_PROMPT;
  379. dlg.m_dwTitleID = IDS_QUERY_TITLE;
  380. dlg.m_bAllowQueryBrowse = TRUE;
  381. dlg.m_pNamespace = m_pNamespace;
  382. dlg.LoadListViaReg(_T("QueryHistory"));
  383. if (dlg.DoModal() == IDOK)
  384. {
  385. m_pOpView->AddOpItem(WMI_QUERY, dlg.m_strText);
  386. }
  387. }
  388. void CWMITestDoc::OnCloseDocument()
  389. {
  390. if (m_pOpView)
  391. m_pOpView->FlushItems();
  392. Disconnect();
  393. CDocument::OnCloseDocument();
  394. }
  395. void CWMITestDoc::DoConnectDlg()
  396. {
  397. OnConnect();
  398. }
  399. void CWMITestDoc::OnNotificationQuery()
  400. {
  401. CGetTextDlg dlg;
  402. dlg.m_dwPromptID = IDS_NOTIFICATION_QUERY_PROMPT;
  403. dlg.m_dwTitleID = IDS_NOTIFICATION_QUERY_TITLE;
  404. dlg.m_dwOptionID = IDS_MONITORY_QUERY;
  405. dlg.m_bAllowQueryBrowse = TRUE;
  406. dlg.m_pNamespace = m_pNamespace;
  407. dlg.m_strSuperClass = _T("__EVENT");
  408. dlg.LoadListViaReg(_T("NotiQueryHistory"));
  409. dlg.m_bOptionChecked = theApp.GetProfileInt(_T("Settings"), _T("MonitorQuery"), FALSE);
  410. if (dlg.DoModal() == IDOK)
  411. {
  412. theApp.WriteProfileInt(_T("Settings"), _T("MonitorQuery"), dlg.m_bOptionChecked);
  413. m_pOpView->AddOpItem(WMI_EVENT_QUERY, dlg.m_strText, dlg.m_bOptionChecked);
  414. }
  415. }
  416. void CWMITestDoc::OnStop()
  417. {
  418. StopOps();
  419. }
  420. void CWMITestDoc::StopOps()
  421. {
  422. CTreeCtrl *pTree = m_pOpView->m_pTree;
  423. for (HTREEITEM hitemOp = pTree->GetChildItem(m_pOpView->m_hitemRoot);
  424. hitemOp != NULL;
  425. hitemOp = pTree->GetNextSiblingItem(hitemOp))
  426. {
  427. COpWrap *pWrap = (COpWrap*) pTree->GetItemData(hitemOp);
  428. pWrap->CancelOp(m_pNamespace);
  429. }
  430. }
  431. /*
  432. void CWMITestDoc::OnUpdateStop(CCmdUI* pCmdUI)
  433. {
  434. pCmdUI->Enable(m_nBusyOps != 0);
  435. }
  436. */
  437. void CWMITestDoc::OnRefreshAll()
  438. {
  439. m_pOpView->RefreshItems();
  440. }
  441. void CWMITestDoc::OnEnumerateInstances()
  442. {
  443. CGetTextDlg dlg;
  444. dlg.m_dwPromptID = IDS_ENUM_INST_PROMPT;
  445. dlg.m_dwTitleID = IDS_ENUM_INST_TITLE;
  446. dlg.m_dwOptionID = IDS_RECURSIVE;
  447. dlg.m_bAllowClassBrowse = TRUE;
  448. dlg.m_pNamespace = m_pNamespace;
  449. dlg.LoadListViaReg(_T("EnumInstHistory"));
  450. dlg.m_bOptionChecked = theApp.GetProfileInt(_T("Settings"), _T("Recurse"), FALSE);
  451. if (dlg.DoModal() == IDOK)
  452. {
  453. theApp.WriteProfileInt(_T("Settings"), _T("Recurse"), dlg.m_bOptionChecked);
  454. m_pOpView->AddOpItem(WMI_ENUM_OBJ, dlg.m_strText, dlg.m_bOptionChecked);
  455. }
  456. }
  457. void CWMITestDoc::OnEnumerateClasses()
  458. {
  459. CGetTextDlg dlg;
  460. dlg.m_dwPromptID = IDS_ENUM_CLASS_PROMPT;
  461. dlg.m_dwTitleID = IDS_ENUM_CLASS_TITLE;
  462. dlg.m_dwOptionID = IDS_RECURSIVE;
  463. dlg.m_bEmptyOK = TRUE;
  464. dlg.m_bAllowClassBrowse = TRUE;
  465. dlg.m_pNamespace = m_pNamespace;
  466. dlg.LoadListViaReg(_T("EnumClassHistory"));
  467. dlg.m_bOptionChecked = theApp.GetProfileInt(_T("Settings"), _T("Recurse"), FALSE);
  468. if (dlg.DoModal() == IDOK)
  469. {
  470. theApp.WriteProfileInt(_T("Settings"), _T("Recurse"), dlg.m_bOptionChecked);
  471. m_pOpView->AddOpItem(WMI_ENUM_CLASS, dlg.m_strText, dlg.m_bOptionChecked);
  472. }
  473. }
  474. void CWMITestDoc::OnGetClass()
  475. {
  476. CGetTextDlg dlg;
  477. dlg.m_dwPromptID = IDS_GET_CLASS_PROMPT;
  478. dlg.m_dwTitleID = IDS_GET_CLASS_TITLE;
  479. dlg.m_bAllowClassBrowse = TRUE;
  480. dlg.m_pNamespace = m_pNamespace;
  481. dlg.LoadListViaReg(_T("GetClassHistory"));
  482. if (dlg.DoModal() == IDOK)
  483. {
  484. m_pOpView->AddOpItem(WMI_GET_CLASS, dlg.m_strText);
  485. }
  486. }
  487. void CWMITestDoc::OnCreateClass()
  488. {
  489. CGetTextDlg dlg;
  490. dlg.m_dwPromptID = IDS_CREATE_CLASS_PROMPT;
  491. dlg.m_dwTitleID = IDS_CREATE_CLASS_TITLE;
  492. dlg.m_bAllowClassBrowse = TRUE;
  493. dlg.m_bEmptyOK = TRUE;
  494. dlg.m_pNamespace = m_pNamespace;
  495. dlg.LoadListViaReg(_T("GetClassHistory"));
  496. if (dlg.DoModal() == IDOK)
  497. {
  498. m_pOpView->AddOpItem(WMI_CREATE_CLASS, dlg.m_strText);
  499. }
  500. }
  501. void CWMITestDoc::OnGetInstance()
  502. {
  503. CGetTextDlg dlg;
  504. dlg.m_dwPromptID = IDS_GET_INST_PROMPT;
  505. dlg.m_dwTitleID = IDS_GET_INST_TITLE;
  506. dlg.LoadListViaReg(_T("GetInstHistory"));
  507. if (dlg.DoModal() == IDOK)
  508. {
  509. m_pOpView->AddOpItem(WMI_GET_OBJ, dlg.m_strText);
  510. }
  511. }
  512. void CWMITestDoc::OnCreateInstance()
  513. {
  514. CGetTextDlg dlg;
  515. dlg.m_dwPromptID = IDS_CREATE_OBJ_PROMPT;
  516. dlg.m_dwTitleID = IDS_CREATE_OBJ_TITLE;
  517. dlg.m_bAllowClassBrowse = TRUE;
  518. dlg.m_pNamespace = m_pNamespace;
  519. dlg.LoadListViaReg(_T("GetClassHistory"));
  520. if (dlg.DoModal() == IDOK)
  521. {
  522. m_pOpView->AddOpItem(WMI_CREATE_OBJ, dlg.m_strText);
  523. }
  524. }
  525. void CWMITestDoc::OnRefreshCurrent()
  526. {
  527. HTREEITEM hItem = m_pOpView->m_pTree->GetSelectedItem();
  528. m_pOpView->RefreshItem(hItem);
  529. }
  530. HTREEITEM CWMITestDoc::GetCurrentItem()
  531. {
  532. CMainFrame *pFrame = (CMainFrame*) AfxGetMainWnd();
  533. if (pFrame->GetActiveView() == m_pOpView)
  534. return m_pOpView->m_pTree->GetSelectedItem();
  535. else
  536. return m_pObjView->GetSelectedItem();
  537. }
  538. CObjInfo *CWMITestDoc::GetCurrentObj()
  539. {
  540. HTREEITEM hitem = GetCurrentItem();
  541. return m_pOpView->GetObjInfo(hitem);
  542. }
  543. void CWMITestDoc::OnAssociators()
  544. {
  545. /*
  546. HTREEITEM hitem = GetCurrentItem();
  547. if (hitem && m_pOpView->IsObj(hitem))
  548. {
  549. CString strQuery,
  550. strPath;
  551. strPath = m_pOpView->m_pTree->GetItemText(hitem);
  552. strQuery.Format(
  553. _T("associators of {%s}"),
  554. (LPCTSTR) strPath);
  555. m_pOpView->AddOpItem(WMI_QUERY, strQuery);
  556. }
  557. */
  558. CString strObj;
  559. if (GetSelectedObjPath(strObj))
  560. {
  561. CString strQuery;
  562. strQuery.Format(
  563. _T("associators of {%s}"),
  564. (LPCTSTR) strObj);
  565. m_pOpView->AddOpItem(WMI_QUERY, strQuery);
  566. }
  567. }
  568. void CWMITestDoc::OnUpdateAssociators(CCmdUI* pCmdUI)
  569. {
  570. //HTREEITEM hitem = GetCurrentItem();
  571. //pCmdUI->Enable(hitem && m_pOpView->IsObj(hitem));
  572. CString strObj;
  573. pCmdUI->Enable(GetSelectedObjPath(strObj));
  574. }
  575. void CWMITestDoc::OnReferences()
  576. {
  577. /*
  578. HTREEITEM hitem = GetCurrentItem();
  579. if (hitem && m_pOpView->IsObj(hitem))
  580. {
  581. CString strQuery,
  582. strPath;
  583. strPath = m_pOpView->m_pTree->GetItemText(hitem);
  584. strQuery.Format(
  585. _T("references of {%s}"),
  586. (LPCTSTR) strPath);
  587. m_pOpView->AddOpItem(WMI_QUERY, strQuery);
  588. }
  589. */
  590. CString strObj;
  591. if (GetSelectedObjPath(strObj))
  592. {
  593. CString strQuery;
  594. strQuery.Format(
  595. _T("references of {%s}"),
  596. (LPCTSTR) strObj);
  597. m_pOpView->AddOpItem(WMI_QUERY, strQuery);
  598. }
  599. }
  600. void CWMITestDoc::OnInstGetClass()
  601. {
  602. CString strClass;
  603. if (GetSelectedClass(strClass))
  604. m_pOpView->AddOpItem(WMI_GET_CLASS, strClass);
  605. }
  606. void CWMITestDoc::OnInstGetInst()
  607. {
  608. CString strObj;
  609. if (GetSelectedObjPath(strObj))
  610. m_pOpView->AddOpItem(WMI_GET_OBJ, strObj);
  611. }
  612. void CWMITestDoc::OnClassInstances()
  613. {
  614. CObjInfo *pInfo = m_pOpView->GetObjInfo(GetCurrentItem());
  615. if (pInfo)
  616. {
  617. CString strClass;
  618. strClass = pInfo->GetStringPropValue(L"__CLASS");
  619. m_pOpView->AddOpItem(WMI_ENUM_OBJ, strClass, FALSE);
  620. }
  621. }
  622. void CWMITestDoc::OnClassSuperclass()
  623. {
  624. CObjInfo *pInfo = m_pOpView->GetObjInfo(GetCurrentItem());
  625. if (pInfo)
  626. {
  627. CString strClass;
  628. strClass = pInfo->GetStringPropValue(L"__SUPERCLASS");
  629. if (!strClass.IsEmpty())
  630. m_pOpView->AddOpItem(WMI_GET_CLASS, strClass);
  631. else
  632. AfxMessageBox(IDS_NO_SUPERCLASS);
  633. }
  634. }
  635. void CWMITestDoc::OnClassInstancesDeep()
  636. {
  637. CObjInfo *pInfo = m_pOpView->GetObjInfo(GetCurrentItem());
  638. if (pInfo)
  639. {
  640. CString strClass;
  641. strClass = pInfo->GetStringPropValue(L"__CLASS");
  642. m_pOpView->AddOpItem(WMI_ENUM_OBJ, strClass, TRUE);
  643. }
  644. }
  645. void CWMITestDoc::OnClassSubclasses()
  646. {
  647. CObjInfo *pInfo = m_pOpView->GetObjInfo(GetCurrentItem());
  648. if (pInfo)
  649. {
  650. CString strClass;
  651. strClass = pInfo->GetStringPropValue(L"__CLASS");
  652. m_pOpView->AddOpItem(WMI_ENUM_CLASS, strClass, FALSE);
  653. }
  654. }
  655. void CWMITestDoc::OnClassSubclassesDeep()
  656. {
  657. CObjInfo *pInfo = m_pOpView->GetObjInfo(GetCurrentItem());
  658. if (pInfo)
  659. {
  660. CString strClass;
  661. strClass = pInfo->GetStringPropValue(L"__CLASS");
  662. m_pOpView->AddOpItem(WMI_ENUM_CLASS, strClass, TRUE);
  663. }
  664. }
  665. void CWMITestDoc::OnOptions()
  666. {
  667. CPrefDlg dlg;
  668. dlg.m_bLoadLast = theApp.m_bLoadLastFile;
  669. dlg.m_bShowInherited = theApp.m_bShowInheritedProperties;
  670. dlg.m_bShowSystem = theApp.m_bShowSystemProperties;
  671. dlg.m_dwUpdateFlag = theApp.m_dwUpdateFlag;
  672. dlg.m_dwClassUpdateMode = theApp.m_dwClassUpdateMode;
  673. dlg.m_bEnablePrivsOnStartup = theApp.m_bEnablePrivsOnStartup;
  674. dlg.m_bPrivsEnabled = theApp.m_bPrivsEnabled;
  675. if (dlg.DoModal() == IDOK)
  676. {
  677. theApp.m_bLoadLastFile = dlg.m_bLoadLast;
  678. theApp.m_bShowInheritedProperties = dlg.m_bShowInherited;
  679. theApp.m_bShowSystemProperties = dlg.m_bShowSystem;
  680. theApp.m_dwUpdateFlag = dlg.m_dwUpdateFlag;
  681. theApp.m_dwClassUpdateMode = dlg.m_dwClassUpdateMode;
  682. theApp.m_bEnablePrivsOnStartup = dlg.m_bEnablePrivsOnStartup;
  683. theApp.m_bPrivsEnabled = dlg.m_bPrivsEnabled;
  684. //m_pOpView->UpdateCurrentObject();
  685. m_pOpView->UpdateCurrentItem();
  686. }
  687. }
  688. void CWMITestDoc::OnSystemProps()
  689. {
  690. theApp.m_bShowSystemProperties ^= 1;
  691. m_pOpView->UpdateCurrentItem();
  692. }
  693. void CWMITestDoc::OnUpdateSystemProps(CCmdUI* pCmdUI)
  694. {
  695. pCmdUI->SetCheck(theApp.m_bShowSystemProperties);
  696. }
  697. void CWMITestDoc::OnInheritedProps()
  698. {
  699. theApp.m_bShowInheritedProperties ^= 1;
  700. m_pOpView->UpdateCurrentItem();
  701. }
  702. void CWMITestDoc::OnUpdateInheritedProps(CCmdUI* pCmdUI)
  703. {
  704. pCmdUI->SetCheck(theApp.m_bShowInheritedProperties);
  705. }
  706. BOOL CWMITestDoc::GetSelectedObjPath(CString &strPath)
  707. {
  708. CMainFrame *pFrame = (CMainFrame*) AfxGetMainWnd();
  709. if (pFrame->GetActiveView() == m_pOpView)
  710. return m_pOpView->GetSelectedObjPath(strPath);
  711. else
  712. return m_pObjView->GetSelectedObjPath(strPath);
  713. }
  714. BOOL CWMITestDoc::GetSelectedClass(CString &strClass)
  715. {
  716. CMainFrame *pFrame = (CMainFrame*) AfxGetMainWnd();
  717. if (pFrame->GetActiveView() == m_pOpView)
  718. return m_pOpView->GetSelectedClass(strClass);
  719. else
  720. return m_pObjView->GetSelectedClass(strClass);
  721. }
  722. void CWMITestDoc::OnReconnect()
  723. {
  724. Disconnect();
  725. // If we weren't able to connect without prompting for information,
  726. // then display the connect dialog and try again.
  727. if (FAILED(Connect(TRUE, FALSE)))
  728. OnConnect();
  729. OnRefreshAll();
  730. }
  731. void CWMITestDoc::OnUpdateReconnect(CCmdUI* pCmdUI)
  732. {
  733. pCmdUI->Enable(m_pNamespace != NULL);
  734. }
  735. void CWMITestDoc::OnTranslateValues()
  736. {
  737. theApp.m_bTranslateValues ^= 1;
  738. m_pOpView->UpdateCurrentItem();
  739. }
  740. void CWMITestDoc::OnUpdateTranslateValues(CCmdUI* pCmdUI)
  741. {
  742. pCmdUI->SetCheck(theApp.m_bTranslateValues);
  743. }
  744. void CWMITestDoc::OnSave()
  745. {
  746. CObjInfo *pObj = GetCurrentObj();
  747. COpWrap *pWrap = m_pOpView->GetCurrentOp();
  748. if (pObj)
  749. {
  750. HRESULT hr;
  751. IWbemCallResult *pResult = NULL;
  752. if (pObj->IsInstance())
  753. hr = m_pNamespace->PutInstance(
  754. pObj->m_pObj,
  755. theApp.m_dwUpdateFlag | WBEM_FLAG_USE_AMENDED_QUALIFIERS,
  756. NULL,
  757. &pResult);
  758. else
  759. hr = m_pNamespace->PutClass(
  760. pObj->m_pObj,
  761. theApp.m_dwUpdateFlag | theApp.m_dwClassUpdateMode |
  762. WBEM_FLAG_USE_AMENDED_QUALIFIERS,
  763. NULL,
  764. &pResult);
  765. if (SUCCEEDED(hr))
  766. {
  767. if (pWrap && pWrap->m_type == WMI_CREATE_OBJ)
  768. {
  769. _variant_t var;
  770. hr = pObj->m_pObj->Get(L"__RELPATH", 0, &var, NULL, NULL);
  771. if (SUCCEEDED(hr))
  772. {
  773. // We created an instance, but since the key fields were
  774. // generated by WMI we'll have to use GetResultString to
  775. // get the path.
  776. if (var.vt == VT_NULL)
  777. {
  778. BSTR bstr = NULL;
  779. if (SUCCEEDED(hr =
  780. pResult->GetResultString(WBEM_INFINITE, &bstr)))
  781. {
  782. var = bstr;
  783. SysFreeString(bstr);
  784. }
  785. }
  786. if (var.vt != VT_NULL)
  787. {
  788. pWrap->m_type = WMI_GET_OBJ;
  789. pWrap->m_strOpText = var.bstrVal;
  790. pObj->SetModified(FALSE);
  791. m_pOpView->RefreshItem(pWrap);
  792. m_pOpView->UpdateCurrentObject(TRUE);
  793. }
  794. }
  795. }
  796. else if (pWrap && pWrap->m_type == WMI_CREATE_CLASS)
  797. {
  798. _variant_t var;
  799. hr = pObj->m_pObj->Get(L"__CLASS", 0, &var, NULL, NULL);
  800. if (SUCCEEDED(hr))
  801. {
  802. pWrap->m_type = WMI_GET_CLASS;
  803. pWrap->m_strOpText = var.bstrVal;
  804. pObj->SetModified(FALSE);
  805. m_pOpView->RefreshItem(pWrap);
  806. m_pOpView->UpdateCurrentObject(TRUE);
  807. }
  808. }
  809. else
  810. {
  811. pObj->SetModified(FALSE);
  812. m_pOpView->UpdateCurrentObject(TRUE);
  813. }
  814. }
  815. if (FAILED(hr))
  816. DisplayWMIErrorBox(hr);
  817. //DisplayWMIErrorBox(hr, pResult);
  818. if (pResult)
  819. pResult->Release();
  820. }
  821. }
  822. void CWMITestDoc::OnUpdateSave(CCmdUI* pCmdUI)
  823. {
  824. CObjInfo *pObj = GetCurrentObj();
  825. pCmdUI->Enable(pObj && pObj->IsModified());
  826. }
  827. void CWMITestDoc::DisplayWMIErrorBox(
  828. HRESULT hres,
  829. //IWbemCallResult *pResult,
  830. IWbemClassObject *pObj)
  831. {
  832. CErrorDlg dlg;
  833. dlg.m_hr = hres;
  834. //dlg.m_pResult = pResult;
  835. if (!pObj)
  836. pObj = GetWMIErrorObject();
  837. dlg.m_pObj.Attach(pObj, FALSE);
  838. dlg.DoModal();
  839. }
  840. BOOL CWMITestDoc::EditGenericObject(DWORD dwPrompt, IWbemClassObject *pObj)
  841. {
  842. CPropertySheet sheet(dwPrompt);
  843. CPropsPg pgProps;
  844. CPropQualsPg pgQuals;
  845. CObjInfo info;
  846. info.SetObj(pObj);
  847. info.SetBaseImage(IMAGE_OBJECT);
  848. info.LoadProps(NULL);
  849. pgProps.m_pNamespace = NULL;
  850. pgProps.m_pObj = &info;
  851. pgQuals.m_pObj = pObj;
  852. pgQuals.m_bIsInstance = TRUE;
  853. pgQuals.m_mode = CPropQualsPg::QMODE_CLASS;
  854. sheet.AddPage(&pgProps);
  855. sheet.AddPage(&pgQuals);
  856. sheet.DoModal();
  857. // This looks bad, but normally this is done by a controlling COpWrap. In
  858. // this case we faked one, so we have to get rid of it ourselves.
  859. delete info.GetProps();
  860. // TODO: We need to see if this object changed before returning TRUE.
  861. return TRUE;
  862. }
  863. void CWMITestDoc::DisplayWMIErrorDetails(IWbemClassObject *pObj)
  864. {
  865. EditGenericObject(IDS_VIEW_ERROR_INFO, pObj);
  866. }
  867. void CWMITestDoc::OnClassCreateInstance()
  868. {
  869. CObjInfo *pInfo = m_pOpView->GetObjInfo(GetCurrentItem());
  870. if (pInfo)
  871. {
  872. CString strClass;
  873. strClass = pInfo->GetStringPropValue(L"__CLASS");
  874. m_pOpView->AddOpItem(WMI_CREATE_OBJ, strClass, FALSE);
  875. }
  876. }
  877. void CWMITestDoc::OnErrorDetails()
  878. {
  879. COpWrap *pOp = m_pOpView->GetCurrentOp();
  880. if (pOp && pOp->m_pErrorObj != NULL)
  881. DisplayWMIErrorDetails(pOp->m_pErrorObj);
  882. }
  883. void CWMITestDoc::OnUpdateErrorDetails(CCmdUI* pCmdUI)
  884. {
  885. COpWrap *pOp = m_pOpView->GetCurrentOp();
  886. pCmdUI->Enable(pOp && pOp->m_pErrorObj != NULL);
  887. }
  888. void CWMITestDoc::OnExecuteMethod(UINT uiCmd)
  889. {
  890. CObjInfo *pInfo = GetCurrentObj();
  891. if (pInfo)
  892. {
  893. CPropInfoArray *pProps = pInfo->GetProps();
  894. int iWhich = uiCmd - IDC_EXECUTE_METHOD_FIRST;
  895. if (iWhich < pProps->GetMethodCount())
  896. {
  897. CMethodInfo &info = pProps->m_listMethods.GetAt(
  898. pProps->m_listMethods.FindIndex(iWhich));
  899. ExecuteMethod(pInfo, info.m_strName);
  900. }
  901. }
  902. }
  903. void CWMITestDoc::ExecuteMethod(CObjInfo *pObj, LPCTSTR szMethod)
  904. {
  905. CExecMethodDlg dlg;
  906. dlg.m_strDefaultMethod = szMethod;
  907. dlg.m_pInfo = pObj;
  908. dlg.DoModal();
  909. }
  910. void CWMITestDoc::OnExecMethod()
  911. {
  912. CGetTextDlg dlg;
  913. dlg.m_dwPromptID = IDS_PROMPT_EXEC_METHOD;
  914. dlg.m_dwTitleID = IDS_EXEC_METHOD_TITLE;
  915. dlg.m_bAllowClassBrowse = TRUE;
  916. dlg.m_bEmptyOK = FALSE;
  917. dlg.m_pNamespace = m_pNamespace;
  918. dlg.LoadListViaReg(_T("ExecObjHistory"));
  919. if (dlg.DoModal() == IDOK)
  920. {
  921. IWbemClassObjectPtr pObj;
  922. HRESULT hr;
  923. hr =
  924. m_pNamespace->GetObject(
  925. _bstr_t(dlg.m_strText),
  926. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  927. NULL,
  928. &pObj,
  929. NULL);
  930. if (SUCCEEDED(hr))
  931. {
  932. CObjInfo info;
  933. info.SetObj(pObj);
  934. info.SetBaseImage(IMAGE_OBJECT);
  935. info.LoadProps(m_pNamespace);
  936. // The method is blank so the user can choose when the dialog
  937. // comes up.
  938. ExecuteMethod(&info, _T(""));
  939. // This looks bad, but normally this is done by a controlling COpWrap. In
  940. // this case we faked one, so we have to get rid of it ourselves.
  941. delete info.GetProps();
  942. }
  943. else
  944. DisplayWMIErrorBox(hr);
  945. }
  946. }
  947. void CWMITestDoc::OnShowMof()
  948. {
  949. CObjInfo *pObj = GetCurrentObj();
  950. CMofDlg dlg;
  951. dlg.m_pObj = pObj->m_pObj;
  952. dlg.DoModal();
  953. }
  954. void CWMITestDoc::OnUpdateShowMof(CCmdUI* pCmdUI)
  955. {
  956. CObjInfo *pObj = GetCurrentObj();
  957. pCmdUI->Enable(pObj != NULL);
  958. }
  959. void CWMITestDoc::ExportItem(HTREEITEM hitem)
  960. {
  961. CExportDlg dlg(FALSE);
  962. dlg.m_bTranslate = theApp.GetProfileInt(_T("Settings"), _T("ExportTrans"), TRUE);
  963. dlg.m_bShowSystemProps = theApp.GetProfileInt(_T("Settings"), _T("ExportShowSys"), TRUE);
  964. if (dlg.DoModal() == IDOK)
  965. {
  966. theApp.WriteProfileInt(_T("Settings"), _T("ExportTrans"), dlg.m_bTranslate);
  967. theApp.WriteProfileInt(_T("Settings"), _T("ExportShowSys"), dlg.m_bShowSystemProps);
  968. m_pOpView->ExportItemToFile(
  969. dlg.GetFileName(),
  970. hitem,
  971. dlg.m_bShowSystemProps,
  972. dlg.m_bTranslate);
  973. }
  974. }
  975. void CWMITestDoc::OnExportTree()
  976. {
  977. ExportItem(m_pOpView->m_hitemRoot);
  978. }
  979. void CWMITestDoc::OnExportItem()
  980. {
  981. ExportItem(GetCurrentItem());
  982. }
  983. void CWMITestDoc::OnFilterBindings()
  984. {
  985. CBindingSheet sheet(IDS_FILTER_TO_CONSUMER_BINDINGS);
  986. CFilterPg pgFilters;
  987. CConsumerPg pgConsumers;
  988. CBindingPg pgBindings;
  989. sheet.AddPage(&pgFilters);
  990. sheet.AddPage(&pgConsumers);
  991. sheet.AddPage(&pgBindings);
  992. sheet.DoModal();
  993. }
  994. void CWMITestDoc::OnStopCurrent()
  995. {
  996. COpWrap *pWrap = m_pOpView->GetCurrentOp();
  997. if (pWrap)
  998. pWrap->CancelOp(m_pNamespace);
  999. }
  1000. void CWMITestDoc::OnUpdateRefreshCurrent(CCmdUI* pCmdUI)
  1001. {
  1002. pCmdUI->Enable(m_pOpView->GetCurrentOp() != NULL);
  1003. }
  1004. void CWMITestDoc::OnUpdateStopCurrent(CCmdUI* pCmdUI)
  1005. {
  1006. pCmdUI->Enable(m_pOpView->GetCurrentOp() != NULL);
  1007. }