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.

5035 lines
130 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. WBEMTEST.CPP
  5. Abstract:
  6. Test for WBEM
  7. History:
  8. --*/
  9. #include "precomp.h"
  10. #include <stdio.h>
  11. #include <time.h>
  12. #include <locale.h>
  13. #include <mbstring.h>
  14. #include <wbemidl.h>
  15. #include <cominit.h>
  16. #include "objedit.h"
  17. #include "wbemntfy.h"
  18. #include "resource.h"
  19. #include "resrc1.h"
  20. #include "bstring.h"
  21. #include "wbemtest.h"
  22. #include "initguid.h"
  23. //#include <wbemcomn.h>
  24. #include "notsink.h"
  25. #include "method.h"
  26. #include "textconv.h"
  27. #include <string.h>
  28. #include <tchar.h>
  29. #include <Htmlhelp.h>
  30. //#include <wbemerror.h>
  31. //#include <genutils.h>
  32. DWORD gdwAuthLevel = RPC_C_AUTHN_LEVEL_PKT;
  33. DWORD gdwImpLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
  34. WCHAR gNameSpace[2048] = L"";
  35. WCHAR gPassword[1024];
  36. WCHAR * gpPassword = NULL;
  37. WCHAR gUser[1024] = L"";
  38. WCHAR * gpUser = NULL;
  39. WCHAR gAuthority[1024] = L"";
  40. WCHAR * gpAuthority = NULL;
  41. // These will be established at ConnectServer time and should remain valid for
  42. // the life of the namespace and all its proxies, refreshers, etc.
  43. BSTR gpPrincipal = NULL;
  44. COAUTHIDENTITY* gpAuthIdentity = NULL;
  45. WCHAR gTransport[1024] = L"";
  46. WCHAR gLocale[1024] = L"";
  47. long gLoginType = 0;
  48. BOOL gbSecured = FALSE;
  49. //BOOL gbAdvanced = FALSE;
  50. class CCreateInstanceDlg : public CWbemDialog
  51. {
  52. protected:
  53. LPWSTR m_wszClass;
  54. long m_lMaxLen;
  55. LONG* m_plQryFlags;
  56. public:
  57. CCreateInstanceDlg(HWND hParent, LPWSTR wszClass, long lMaxLen, LONG* plQryFlags)
  58. : m_wszClass(wszClass), m_lMaxLen(lMaxLen), m_plQryFlags(plQryFlags),
  59. CWbemDialog(IDD_CREATEINSTANCE, hParent)
  60. {}
  61. protected:
  62. BOOL OnInitDialog();
  63. BOOL Verify();
  64. };
  65. //#pragma warning(4270:disable)
  66. int __cdecl CmpFunc( const void *arg1, const void *arg2 )
  67. {
  68. if ((arg1 == NULL) || (*(int*)arg1 == 0))
  69. {
  70. return 1;
  71. }
  72. if ((arg2 == NULL) || (*(int*)arg2 == 0))
  73. {
  74. return -1;
  75. }
  76. VARIANT t_v1;
  77. VariantInit(&t_v1);
  78. VARIANT t_v2;
  79. VariantInit(&t_v2);
  80. int retVal = 0;
  81. if (SUCCEEDED( (*(IWbemClassObject **)arg1)->Get(L"__RELPATH", 0, &t_v1, NULL, NULL) ))
  82. {
  83. if (SUCCEEDED( (*(IWbemClassObject **)arg2)->Get(L"__RELPATH", 0, &t_v2, NULL, NULL) ))
  84. {
  85. if ( (VT_BSTR == t_v1.vt) && (VT_BSTR == t_v2.vt) )
  86. {
  87. char buffA[5120];
  88. char buffB[5120];
  89. wcstombs( buffA, t_v1.bstrVal, 5120 );
  90. wcstombs( buffB, t_v2.bstrVal, 5120 );
  91. int t_i = CompareStringA(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, buffA, -1, buffB, -1 );
  92. switch (t_i)
  93. {
  94. case CSTR_LESS_THAN:
  95. {
  96. retVal = -1;
  97. break;
  98. }
  99. case CSTR_EQUAL:
  100. {
  101. retVal = 0;
  102. break;
  103. }
  104. case CSTR_GREATER_THAN:
  105. default:
  106. {
  107. retVal = 1;
  108. break;
  109. }
  110. }
  111. }
  112. VariantClear(&t_v2);
  113. }
  114. VariantClear(&t_v1);
  115. }
  116. return retVal;
  117. }
  118. UINT WINAPI GetDlgItemTextX(
  119. HWND hDlg,
  120. int nDlgItem,
  121. LPWSTR pStr,
  122. int nMaxCount
  123. )
  124. {
  125. char Tmp[5120];
  126. *Tmp = 0;
  127. *pStr = 0;
  128. UINT uRes = GetDlgItemTextA(hDlg, nDlgItem, Tmp, 5120);
  129. if (uRes == 0 || strlen(Tmp) == 0)
  130. return uRes;
  131. if (mbstowcs(pStr, Tmp, nMaxCount) == (unsigned) nMaxCount)
  132. *(pStr + nMaxCount - 1) = 0;
  133. return uRes;
  134. }
  135. BOOL GetServerNamespace(IWbemClassObject *pObj, WCHAR *szResult, int nMaxSize);
  136. INT_PTR GetSuperclassInfo(HWND hDlg, LPWSTR pClass, LONG lMaxBuf, LONG *plQryFlags);
  137. INT_PTR GetClassInfo(HWND hDlg, LPWSTR pClass, LONG lMaxBuf, LONG *plQryFlags);
  138. /////////////////////////////////////////////////////////////////////////////
  139. IWbemLocator *g_pLocator = 0;
  140. IWbemConnection *g_pConnection = 0;
  141. IWbemServices *g_pNamespace = 0;
  142. IWbemServicesEx *g_pServicesEx=NULL;
  143. BSTR g_strNamespace = NULL;
  144. CAppOwner g_Owner;
  145. CContext g_Context;
  146. void Fatal(UINT uMsg)
  147. {
  148. CBasicWbemDialog::MessageBox(0, uMsg, IDS_CRITICAL_ERROR, MB_OK | MB_SYSTEMMODAL);
  149. }
  150. /////////////////////////////////////////////////////////////////////////////
  151. BOOL RegisterEventSink();
  152. BOOL UnregisterEventSink();
  153. /////////////////////////////////////////////////////////////////////////////
  154. IUnsecuredApartment* CUnsecWrap::mstatic_pApartment = NULL;
  155. IUnsecuredApartment* CUnsecWrapEx::mstatic_pApartment = NULL;
  156. class CMainDlg : public CWbemDialog
  157. {
  158. protected:
  159. void Connect(BOOL bBind);
  160. void ConnectButtons(BOOL b);
  161. void OpenHelp(void);
  162. void GetClasses();
  163. void EditClass();
  164. void CreateClass();
  165. void DeleteClass();
  166. void GetInstances();
  167. void EditInstance();
  168. void CreateInstance();
  169. void DeleteInstance();
  170. void ExecQuery();
  171. void ExecNotificationQuery();
  172. void ExecMethod();
  173. void OpenNs();
  174. void CreateRefresher();
  175. void EditContext();
  176. void Open();
  177. void AddObject();
  178. void DeleteObject();
  179. void RenameObject();
  180. void GetObjectSecurity();
  181. void SetObjectSecurity();
  182. void DoServicesExGenObject(int iOpt);
  183. LONG Timeout();
  184. ULONG BatchCount();
  185. LONG m_lGenFlags; // generic flags (i.e., WBEM_FLAG_ .. used in IWbemServices methods)
  186. LONG m_lSync; // sync, async, semisync
  187. public:
  188. BOOL m_fNotificationQueryResultDlg;
  189. CMainDlg() : CWbemDialog(IDD_MAIN, (HWND)NULL)
  190. {
  191. m_lGenFlags = 0;
  192. m_lSync = SEMISYNC;
  193. m_fNotificationQueryResultDlg = FALSE;
  194. }
  195. ~CMainDlg();
  196. BOOL OnInitDialog();
  197. BOOL OnCommand(WORD wNotifyCode, WORD wID);
  198. BOOL OnOK();
  199. };
  200. BOOL CMainDlg::OnOK()
  201. {
  202. if(m_lRefCount > 0)
  203. {
  204. MessageBox(IDS_MUST_CLOSE_WINDOWS, IDS_ERROR, MB_OK | MB_ICONSTOP);
  205. return FALSE;
  206. }
  207. return CBasicWbemDialog::OnOK();
  208. }
  209. long CAppOwner::Release()
  210. {
  211. if(CRefCountable::Release() == 0)
  212. {
  213. //CoUninitialize();
  214. }
  215. return m_lRefCount;
  216. }
  217. int WINAPI WinMain(
  218. HINSTANCE hInst,
  219. HINSTANCE hPrevInst,
  220. PSTR szCmdLine,
  221. int iCmdShow
  222. )
  223. {
  224. mbstowcs(gNameSpace, "", 1000);
  225. g_strNamespace = SysAllocString(gNameSpace);
  226. setlocale(LC_ALL, "");
  227. // OLE initialization.
  228. // ===================
  229. HRESULT hr = InitializeCom();
  230. if (hr != S_OK)
  231. {
  232. Fatal(IDS_OLE_INIT_FAILED);
  233. return -1;
  234. }
  235. gbSecured = (strstr(szCmdLine, "/sec") != NULL);
  236. //gbAdvanced = (strstr(szCmdLine, "/test") != NULL);
  237. if (strstr(szCmdLine, "/setp") != NULL)
  238. {
  239. if (FAILED(EnableAllPrivileges(TOKEN_PROCESS)))
  240. {
  241. Fatal(IDS_FAIL_ENABLE_ALL_PRIV);
  242. return -1;
  243. }
  244. }
  245. hr = InitializeSecurity(NULL, -1, NULL, NULL,
  246. RPC_C_AUTHN_LEVEL_NONE,
  247. RPC_C_IMP_LEVEL_IMPERSONATE,
  248. NULL, EOAC_NONE, 0);
  249. /// // Get a session object.
  250. /// // =======================
  251. ///
  252. /// dwRes = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
  253. /// IID_IWbemLocator, (LPVOID *) &g_pLocator);
  254. ///
  255. ///
  256. /// if (dwRes != S_OK)
  257. /// {
  258. /// Fatal(IDS_FAILED_IWBEMLOCATOR);
  259. /// OleUninitialize();
  260. /// Fatal(IDS_ABNORMAL_TERMINATION);
  261. /// return -1;
  262. /// }
  263. ///
  264. /// dwRes = CoCreateInstance(CLSID_WbemConnection, 0, CLSCTX_INPROC_SERVER,
  265. /// IID_IWbemConnection, (LPVOID *) &g_pConnection);
  266. ///
  267. ///
  268. /// if (dwRes != S_OK)
  269. /// {
  270. /// Fatal(IDS_FAILED_IWBEMBINDER);
  271. /// OleUninitialize();
  272. /// Fatal(IDS_ABNORMAL_TERMINATION);
  273. /// return -1;
  274. /// }
  275. CMainDlg* pDlg = new CMainDlg;
  276. pDlg->SetDeleteOnClose();
  277. pDlg->SetOwner(&g_Owner);
  278. pDlg->Create();
  279. MSG msg;
  280. while (GetMessage(&msg, (HWND) NULL, 0, 0))
  281. {
  282. if(CBasicWbemDialog::IsDialogMessage(&msg))
  283. continue;
  284. TranslateMessage(&msg);
  285. DispatchMessage(&msg);
  286. }
  287. if (g_pNamespace) g_pNamespace->Release();
  288. if (g_pServicesEx) g_pServicesEx->Release();
  289. if (g_pLocator) g_pLocator->Release();
  290. if (g_pConnection) g_pConnection->Release();
  291. g_Context.Clear();
  292. CoUninitialize();
  293. // Terminating normally.
  294. // =====================
  295. return 0;
  296. }
  297. //***************************************************************************
  298. //
  299. //***************************************************************************
  300. class CErrorDlg : public CWbemDialog
  301. {
  302. protected:
  303. HRESULT m_hres;
  304. IWbemClassObject* m_pErrorObj;
  305. public:
  306. CErrorDlg(HWND hParent, HRESULT hres, IWbemClassObject* pErrorObj = NULL)
  307. : CWbemDialog(IDD_ERROR,hParent), m_hres(hres), m_pErrorObj(pErrorObj)
  308. {
  309. if(pErrorObj) pErrorObj->AddRef();
  310. }
  311. ~CErrorDlg()
  312. {
  313. if(m_pErrorObj) m_pErrorObj->Release();
  314. }
  315. BOOL OnInitDialog();
  316. BOOL OnCommand(WORD wNotifyCode, WORD wID);
  317. };
  318. BOOL CErrorDlg::OnInitDialog()
  319. {
  320. CenterOnParent();
  321. WCHAR szError[TEMP_BUF];
  322. szError[0] = 0;
  323. WCHAR szFacility[TEMP_BUF];
  324. szFacility[0] = 0;
  325. char szMsg[TEMP_BUF];
  326. char szFormat[100];
  327. IWbemStatusCodeText * pStatus = NULL;
  328. SCODE sc = CoCreateInstance(CLSID_WbemStatusCodeText, 0, CLSCTX_INPROC_SERVER,
  329. IID_IWbemStatusCodeText, (LPVOID *) &pStatus);
  330. if(sc == S_OK)
  331. {
  332. BSTR bstr = 0;
  333. sc = pStatus->GetErrorCodeText(m_hres, 0, 0, &bstr);
  334. if(sc == S_OK)
  335. {
  336. wcsncpy(szError, bstr, TEMP_BUF-1);
  337. SysFreeString(bstr);
  338. bstr = 0;
  339. }
  340. sc = pStatus->GetFacilityCodeText(m_hres, 0, 0, &bstr);
  341. if(sc == S_OK)
  342. {
  343. wcsncpy(szFacility, bstr, TEMP_BUF-1);
  344. SysFreeString(bstr);
  345. bstr = 0;
  346. }
  347. pStatus->Release();
  348. }
  349. if(wcslen(szFacility) == 0 || wcslen(szError) == 0)
  350. {
  351. LoadString(GetModuleHandle(NULL), ERROR_FORMAT_EX, szFormat, 99);
  352. sprintf(szMsg, szFormat, m_hres);
  353. }
  354. else
  355. {
  356. LoadString(GetModuleHandle(NULL), ERROR_FORMAT_LONG, szFormat, 99);
  357. sprintf(szMsg, szFormat, m_hres, szFacility, szError);
  358. }
  359. SetDlgItemText(IDC_MESSAGE, szMsg);
  360. EnableWindow(GetDlgItem(IDC_SHOWOBJECT), (m_pErrorObj != NULL));
  361. return TRUE;
  362. }
  363. BOOL CErrorDlg::OnCommand(WORD wNotifyCode, WORD wID)
  364. {
  365. // Show object is the only command out there
  366. if(wID == IDC_SHOWOBJECT)
  367. {
  368. CObjectEditor Ed(m_hDlg, 0, CObjectEditor::readonly, SYNC, m_pErrorObj);
  369. Ed.Edit();
  370. }
  371. return TRUE;
  372. }
  373. void FormatError(HRESULT res, HWND hParent, IWbemClassObject* pErrorObj)
  374. {
  375. BOOL bOwn = FALSE;
  376. if(pErrorObj == NULL)
  377. {
  378. IErrorInfo* pEI;
  379. if(GetErrorInfo(0, &pEI) == S_OK)
  380. {
  381. pEI->QueryInterface(IID_IWbemClassObject, (void**)&pErrorObj);
  382. pEI->Release();
  383. if(pErrorObj)
  384. bOwn = TRUE;
  385. }
  386. }
  387. CErrorDlg Dlg(hParent, res, pErrorObj);
  388. Dlg.Run(NULL, true); // no disablement
  389. if(bOwn)
  390. pErrorObj->Release();
  391. }
  392. static wchar_t *g_pTargetClassBuf;
  393. static int g_nMaxTargetClassBuf;
  394. void SetDlgItemTextWC(HWND hDlg, int ID, WCHAR * pwc)
  395. {
  396. int iLen = 2*(wcslen(pwc))+1;
  397. char * pTemp = new char[iLen];
  398. if(pTemp == NULL)
  399. return;
  400. wcstombs(pTemp, pwc, iLen);
  401. SetDlgItemText(hDlg, ID, pTemp);
  402. delete pTemp;
  403. }
  404. void GetDlgItemTextWC(HWND hDlg, int ID, WCHAR * pwc,int iwcSize)
  405. {
  406. char * pTemp = new char[iwcSize * 2];
  407. if(pTemp == NULL)
  408. return;
  409. if(GetDlgItemText(hDlg, ID, pTemp, iwcSize *2))
  410. {
  411. mbstowcs(pwc, pTemp, iwcSize);
  412. }
  413. else *pwc = 0;
  414. delete pTemp;
  415. }
  416. class CConnectDlg : public CWbemDialog
  417. {
  418. protected:
  419. BOOL m_bBind;
  420. LONG m_lGenFlags; // generic WBEM_FLAG_ .. flags
  421. LONG m_lTimeout; // used in semisync only
  422. public:
  423. CConnectDlg(HWND hParent, BOOL bBind, LONG lGenFlags, LONG lTimeout) : CWbemDialog((bBind) ? IDD_WBEM_BIND : IDD_WBEM_CONNECT, hParent),
  424. m_lGenFlags(lGenFlags), m_lTimeout(lTimeout)
  425. { m_bBind = bBind;}
  426. protected:
  427. enum {CONNECT_IWBEMLOCATOR, CONNECT_IWBEMCONNECTION};
  428. enum {INTERFACE_IWBEMSERVICES, INTERFACE_IWBEMSERVICESEX, INTERFACE_IWBEMCLASSOBJECT};
  429. BOOL OnInitDialog();
  430. BOOL Verify();
  431. virtual BOOL OnSelChange(int nID);
  432. };
  433. BOOL CConnectDlg::OnInitDialog()
  434. {
  435. if (gNameSpace[0]==L'\0')
  436. SetDlgItemTextX(IDC_NAMESPACE, L"root\\default");
  437. else
  438. SetDlgItemTextX(IDC_NAMESPACE, gNameSpace);
  439. SetDlgItemTextX(IDC_USER, gUser);
  440. SetDlgItemTextX(IDC_PASSWORD, gPassword);
  441. SetDlgItemTextX(IDC_LOCALE, gLocale);
  442. SetDlgItemTextX(IDC_AUTHORITY, gAuthority);
  443. BOOL bRet = CheckRadioButton(m_hDlg, IDC_NULL, IDC_BLANK, IDC_NULL);
  444. if (gdwImpLevel == RPC_C_IMP_LEVEL_IDENTIFY)
  445. {
  446. CheckRadioButton(m_hDlg, IDC_IMP_IDENTIFY, IDC_IMP_DELEGATE, IDC_IMP_IDENTIFY);
  447. }
  448. else if (gdwImpLevel == RPC_C_IMP_LEVEL_IMPERSONATE)
  449. {
  450. CheckRadioButton(m_hDlg, IDC_IMP_IDENTIFY, IDC_IMP_DELEGATE, IDC_IMP_IMPERSONATE);
  451. }
  452. else if (gdwImpLevel == RPC_C_IMP_LEVEL_DELEGATE)
  453. {
  454. CheckRadioButton(m_hDlg, IDC_IMP_IDENTIFY, IDC_IMP_DELEGATE, IDC_IMP_DELEGATE);
  455. }
  456. if (gdwAuthLevel == RPC_C_AUTHN_LEVEL_NONE)
  457. {
  458. CheckRadioButton(m_hDlg, IDC_AUTH_NONE, IDC_AUTH_PACKET_PRIVACY, IDC_AUTH_NONE);
  459. }
  460. else if (gdwAuthLevel == RPC_C_AUTHN_LEVEL_CONNECT)
  461. {
  462. CheckRadioButton(m_hDlg, IDC_AUTH_NONE, IDC_AUTH_PACKET_PRIVACY, IDC_AUTH_CONNECTION);
  463. }
  464. else if (gdwAuthLevel == RPC_C_AUTHN_LEVEL_CALL)
  465. {
  466. CheckRadioButton(m_hDlg, IDC_AUTH_NONE, IDC_AUTH_PACKET_PRIVACY, IDC_AUTH_CALL);
  467. }
  468. else if (gdwAuthLevel == RPC_C_AUTHN_LEVEL_PKT)
  469. {
  470. CheckRadioButton(m_hDlg, IDC_AUTH_NONE, IDC_AUTH_PACKET_PRIVACY, IDC_AUTH_PACKET);
  471. }
  472. else if (gdwAuthLevel == RPC_C_AUTHN_LEVEL_PKT_INTEGRITY)
  473. {
  474. CheckRadioButton(m_hDlg, IDC_AUTH_NONE, IDC_AUTH_PACKET_PRIVACY, IDC_AUTH_PACKET_INTEGRITY);
  475. }
  476. else if (gdwAuthLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
  477. {
  478. CheckRadioButton(m_hDlg, IDC_AUTH_NONE, IDC_AUTH_PACKET_PRIVACY, IDC_AUTH_PACKET_PRIVACY);
  479. }
  480. AddStringToCombo (IDC_CONNECTUSING, "IWbemLocator (Namespaces)", CONNECT_IWBEMLOCATOR);
  481. #if 0
  482. RAID 167868
  483. Support for EX temporary removed, default IWbemLocator
  484. AddStringToCombo (IDC_CONNECTUSING, "IWbemConnection (Namespaces, Scopes, Objects)", CONNECT_IWBEMCONNECTION);
  485. #endif
  486. SetComboSelection (IDC_CONNECTUSING, g_pConnection ? CONNECT_IWBEMCONNECTION : CONNECT_IWBEMLOCATOR );
  487. SendMessage (m_hDlg, WM_COMMAND, MAKEWPARAM(IDC_CONNECTUSING, CBN_SELCHANGE), (LPARAM)GetDlgItem (IDC_CONNECTUSING));
  488. AddStringToCombo (IDC_COMPLETION, "Synchronous", SYNC);
  489. AddStringToCombo (IDC_COMPLETION, "Semisynchronous", SEMISYNC);
  490. AddStringToCombo (IDC_COMPLETION, "Asynchronous", ASYNC);
  491. SetComboSelection (IDC_COMPLETION, SYNC);
  492. return TRUE;
  493. }
  494. BOOL CConnectDlg::OnSelChange(int nID)
  495. {
  496. int sel=(int)SendDlgItemMessage (m_hDlg, IDC_CONNECTUSING, CB_GETCURSEL, 0, 0L);
  497. int connectusing=(int)SendDlgItemMessage (m_hDlg, IDC_CONNECTUSING, CB_GETITEMDATA, sel, 0L);
  498. sel=(int)SendDlgItemMessage (m_hDlg, IDC_INTERFACERETURNED, CB_GETCURSEL, 0, 0L);
  499. int interfacereturned=(int)SendDlgItemMessage (m_hDlg, IDC_INTERFACERETURNED, CB_GETITEMDATA, sel, 0L);
  500. switch (nID)
  501. {
  502. case IDC_CONNECTUSING:
  503. {
  504. bool authority=(connectusing==CONNECT_IWBEMLOCATOR);
  505. EnableWindow (GetDlgItem (IDC_AUTHORITY), authority);
  506. EnableWindow (GetDlgItem (IDC_AUTHORITYLABEL), authority);
  507. bool asynchronous=(connectusing==CONNECT_IWBEMCONNECTION);
  508. EnableWindow (GetDlgItem (IDC_COMPLETION), asynchronous);
  509. EnableWindow (GetDlgItem (IDC_COMPLETIONLABEL), asynchronous);
  510. if (!asynchronous)
  511. SetComboSelection (IDC_COMPLETION, SYNC);
  512. SendDlgItemMessage (m_hDlg, IDC_INTERFACERETURNED, CB_RESETCONTENT, 0, 0L);
  513. AddStringToCombo (IDC_INTERFACERETURNED, "IWbemServices", INTERFACE_IWBEMSERVICES);
  514. #if 0
  515. RAID 167868
  516. Support for EX temporary removed
  517. AddStringToCombo (IDC_INTERFACERETURNED, "IWbemServicesEx", INTERFACE_IWBEMSERVICESEX);
  518. #endif
  519. if (connectusing==CONNECT_IWBEMCONNECTION)
  520. AddStringToCombo (IDC_INTERFACERETURNED, "IWbemClassObject", INTERFACE_IWBEMCLASSOBJECT);
  521. SetComboSelection (IDC_INTERFACERETURNED, ( g_pServicesEx ? INTERFACE_IWBEMSERVICESEX : INTERFACE_IWBEMSERVICES ));
  522. SendMessage (m_hDlg, WM_COMMAND, MAKEWPARAM(IDC_INTERFACERETURNED, CBN_SELCHANGE), (LPARAM)GetDlgItem (IDC_INTERFACERETURNED));
  523. break;
  524. }
  525. case IDC_INTERFACERETURNED:
  526. {
  527. if (connectusing==CONNECT_IWBEMLOCATOR)
  528. SetDlgItemText (IDC_PATHLABEL, "Namespace");
  529. else
  530. {
  531. if (interfacereturned==INTERFACE_IWBEMCLASSOBJECT)
  532. SetDlgItemText (IDC_PATHLABEL, "Object Path");
  533. else
  534. SetDlgItemText (IDC_PATHLABEL, "Namespace/Scope");
  535. }
  536. break;
  537. }
  538. }
  539. return TRUE;
  540. }
  541. BOOL CConnectDlg::Verify()
  542. {
  543. WCHAR NameSpace[512], User[64], Password[64], Locale[128];
  544. WCHAR Authority[64];
  545. User[0] = Password[0] = Locale[0] = Authority[0] = NameSpace[0] = 0;
  546. bool bBoundToObject = false;
  547. GetDlgItemTextX(IDC_NAMESPACE, NameSpace, 512);
  548. GetDlgItemTextX(IDC_USER, User, 64);
  549. GetDlgItemTextX(IDC_PASSWORD, Password, 64);
  550. GetDlgItemTextX(IDC_AUTHORITY, Authority, 64);
  551. GetDlgItemTextX(IDC_LOCALE, Locale, 128);
  552. bool bTreatEmptyAsBlank = (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_BLANK));
  553. gdwAuthLevel = RPC_C_AUTHN_LEVEL_NONE;
  554. gdwImpLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
  555. if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_IMP_IDENTIFY))
  556. {
  557. gdwImpLevel = RPC_C_IMP_LEVEL_IDENTIFY ;
  558. }
  559. else if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_IMP_IMPERSONATE))
  560. {
  561. gdwImpLevel = RPC_C_IMP_LEVEL_IMPERSONATE ;
  562. }
  563. else if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_IMP_DELEGATE))
  564. {
  565. gdwImpLevel = RPC_C_IMP_LEVEL_DELEGATE ;
  566. }
  567. if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_AUTH_NONE))
  568. {
  569. gdwAuthLevel = RPC_C_AUTHN_LEVEL_NONE;
  570. }
  571. else if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_AUTH_CONNECTION))
  572. {
  573. gdwAuthLevel = RPC_C_AUTHN_LEVEL_CONNECT;
  574. }
  575. else if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_AUTH_CALL))
  576. {
  577. gdwAuthLevel = RPC_C_AUTHN_LEVEL_CALL ;
  578. }
  579. else if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_AUTH_PACKET))
  580. {
  581. gdwAuthLevel = RPC_C_AUTHN_LEVEL_PKT ;
  582. }
  583. else if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_AUTH_PACKET_INTEGRITY))
  584. {
  585. gdwAuthLevel = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY ;
  586. }
  587. else if (BST_CHECKED == IsDlgButtonChecked(m_hDlg, IDC_AUTH_PACKET_PRIVACY))
  588. {
  589. gdwAuthLevel = RPC_C_AUTHN_LEVEL_PKT_PRIVACY ;
  590. }
  591. if(wcslen(NameSpace) < 1)
  592. {
  593. MessageBox(m_hDlg, IDS_INVALID_NAMESPACE, IDS_ERROR, MB_OK | MB_ICONSTOP);
  594. return FALSE;
  595. }
  596. CBString bsNamespace(NameSpace);
  597. CBString bsLocale(Locale);
  598. HCURSOR hWait, hSave = NULL;
  599. CBString bsUser;
  600. CBString bsAuthority;
  601. CBString bsPassword;
  602. if(wcslen(User) > 0)
  603. bsUser = User;
  604. if(wcslen(Password) > 0)
  605. bsPassword = Password;
  606. else if (bTreatEmptyAsBlank)
  607. bsPassword = L"";
  608. if(wcslen(Authority) > 0)
  609. bsAuthority = Authority;
  610. // Cleanup a preexisting principal and AuthIdentity as necessary
  611. if ( NULL != gpPrincipal )
  612. {
  613. SysFreeString( gpPrincipal );
  614. gpPrincipal = NULL;
  615. }
  616. if ( NULL != gpAuthIdentity )
  617. {
  618. WbemFreeAuthIdentity( gpAuthIdentity );
  619. gpAuthIdentity = NULL;
  620. }
  621. hWait = LoadCursor(NULL, IDC_WAIT);
  622. SCODE sRes=S_OK;
  623. if(hWait)
  624. hSave = SetCursor(hWait);
  625. // Determine which connection method and interface is requested.
  626. int cursel=(int)SendDlgItemMessage (m_hDlg, IDC_CONNECTUSING, CB_GETCURSEL, 0, 0);
  627. int ConnectUsing=(int)SendDlgItemMessage (m_hDlg, IDC_CONNECTUSING, CB_GETITEMDATA, cursel, 0);
  628. cursel=(int)SendDlgItemMessage (m_hDlg, IDC_INTERFACERETURNED, CB_GETCURSEL, 0, 0);
  629. int InterfaceReturned=(int)SendDlgItemMessage (m_hDlg, IDC_INTERFACERETURNED, CB_GETITEMDATA, cursel, 0);
  630. int sel=(int)SendDlgItemMessage (m_hDlg, IDC_COMPLETION, CB_GETCURSEL, 0, 0);
  631. LONG lSync=(LONG)SendDlgItemMessage (m_hDlg, IDC_COMPLETION, CB_GETITEMDATA, sel, 0);
  632. IID riid;
  633. switch (InterfaceReturned)
  634. {
  635. case INTERFACE_IWBEMSERVICES:
  636. riid = IID_IWbemServices;
  637. break;
  638. case INTERFACE_IWBEMSERVICESEX:
  639. riid = IID_IWbemServicesEx;
  640. break;
  641. case INTERFACE_IWBEMCLASSOBJECT:
  642. riid = IID_IWbemClassObject;
  643. }
  644. // Obtain a connection object of the requested type.
  645. if (g_pLocator) g_pLocator->Release();
  646. if (g_pConnection) g_pConnection->Release();
  647. g_pLocator=NULL;
  648. g_pConnection=NULL;
  649. switch (ConnectUsing)
  650. {
  651. case CONNECT_IWBEMLOCATOR:
  652. {
  653. sRes=CoCreateInstance (CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (void **)&g_pLocator);
  654. break;
  655. }
  656. case CONNECT_IWBEMCONNECTION:
  657. {
  658. sRes=CoCreateInstance (CLSID_WbemConnection, 0, CLSCTX_INPROC_SERVER, IID_IWbemConnection, (void **)&g_pConnection);
  659. break;
  660. }
  661. }
  662. // Connect and obtain requested interfaces.
  663. if (g_pNamespace) g_pNamespace->Release();
  664. if (g_pServicesEx) g_pServicesEx->Release();
  665. g_pNamespace=NULL;
  666. g_pServicesEx=NULL;
  667. IWbemClassObject *pObject=NULL;
  668. if (g_pLocator)
  669. {
  670. sRes=g_pLocator->ConnectServer (bsNamespace.GetString(),
  671. bsUser.GetString(), bsPassword.GetString(),
  672. bsLocale.GetString(), 0, bsAuthority.GetString(), g_Context, &g_pNamespace);
  673. if (SUCCEEDED(sRes))
  674. {
  675. switch (InterfaceReturned)
  676. {
  677. case INTERFACE_IWBEMSERVICESEX:
  678. {
  679. sRes=g_pNamespace->QueryInterface (IID_IWbemServicesEx, (void **)&g_pServicesEx);
  680. break;
  681. }
  682. case INTERFACE_IWBEMCLASSOBJECT:
  683. {
  684. // This should always fail. It's here so that wbemtest can display the actual
  685. // return code as generated by QI in such a case.
  686. sRes=g_pNamespace->QueryInterface (IID_IWbemClassObject, (void **)&pObject);
  687. break;
  688. }
  689. }
  690. }
  691. }
  692. else if (g_pConnection)
  693. {
  694. IUnknown *pUnknown=NULL;
  695. if (lSync & ASYNC)
  696. {
  697. CTestNotify* pSink=new CTestNotify();
  698. sRes=g_pConnection->OpenAsync(bsNamespace.GetString(),
  699. bsUser.GetString(), bsPassword.GetString(),
  700. bsLocale.GetString(), 0, g_Context,
  701. riid, pSink);
  702. if (SUCCEEDED(sRes))
  703. {
  704. pSink->WaitForSignal(INFINITE);
  705. sRes = pSink->GetStatusCode();
  706. if(SUCCEEDED(sRes))
  707. {
  708. pUnknown=pSink->GetInterface();
  709. pSink->Release();
  710. }
  711. }
  712. }
  713. else if (lSync & SEMISYNC)
  714. {
  715. IWbemCallResultEx *pResult=NULL;
  716. sRes=g_pConnection->Open (bsNamespace.GetString(),
  717. bsUser.GetString(), bsPassword.GetString(),
  718. bsLocale.GetString(), 0, g_Context,
  719. riid, NULL, &pResult);
  720. if (SUCCEEDED(sRes))
  721. {
  722. SetInterfaceSecurityEx (pResult, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  723. LONG lStatus;
  724. while ((sRes=pResult->GetCallStatus (WBEM_INFINITE, &lStatus))==WBEM_S_TIMEDOUT)
  725. {
  726. Sleep(200); // give the other threads a chance
  727. }
  728. if (SUCCEEDED(sRes))
  729. {
  730. sRes=(HRESULT)lStatus;
  731. if (sRes==WBEM_S_NO_ERROR)
  732. {
  733. sRes=pResult->GetResult (0, 0, IID_IWbemServices, (void**)&pUnknown);
  734. }
  735. }
  736. pResult->Release();
  737. }
  738. }
  739. else
  740. {
  741. sRes=g_pConnection->Open (bsNamespace.GetString(),
  742. bsUser.GetString(), bsPassword.GetString(),
  743. bsLocale.GetString(), 0, g_Context,
  744. riid, (void **)&pUnknown, NULL);
  745. }
  746. if (SUCCEEDED(sRes))
  747. {
  748. switch (InterfaceReturned)
  749. {
  750. case INTERFACE_IWBEMSERVICES:
  751. {
  752. sRes=pUnknown->QueryInterface (IID_IWbemServices, (void **)&g_pNamespace);
  753. break;
  754. }
  755. case INTERFACE_IWBEMSERVICESEX:
  756. {
  757. // A-DAVCOO: We temporarily need both interfaces when an IWbemServicesEx is requested.
  758. sRes=pUnknown->QueryInterface (IID_IWbemServices, (void **)&g_pNamespace);
  759. if (SUCCEEDED(sRes))
  760. sRes=pUnknown->QueryInterface (IID_IWbemServicesEx, (void **)&g_pServicesEx);
  761. break;
  762. }
  763. case INTERFACE_IWBEMCLASSOBJECT:
  764. {
  765. sRes=pUnknown->QueryInterface (IID_IWbemClassObject, (void **)&pObject);
  766. break;
  767. }
  768. }
  769. pUnknown->Release();
  770. pUnknown=NULL;
  771. }
  772. }
  773. // If an IWbemClassObject was obtained, display the object editor.
  774. if (pObject)
  775. {
  776. bBoundToObject=true;
  777. CObjectEditor ed(m_hDlg, m_lGenFlags, CObjectEditor::foreign, lSync, pObject, m_lTimeout);
  778. ed.Edit();
  779. pObject->Release();
  780. pObject=NULL;
  781. }
  782. if(hSave)
  783. SetCursor(hSave);
  784. if(SUCCEEDED(sRes))
  785. {
  786. if(!bBoundToObject)
  787. wcscpy(gNameSpace, NameSpace);
  788. wcscpy(gLocale, Locale);
  789. wcscpy(gAuthority, Authority);
  790. wcscpy(gUser, User);
  791. if(wcslen(gUser) > 0)
  792. gpUser = gUser;
  793. else
  794. gpUser = NULL;
  795. wcscpy(gPassword, Password);
  796. if(wcslen(gPassword) > 0)
  797. gpPassword = gPassword;
  798. else if (bTreatEmptyAsBlank)
  799. gpPassword = L"";
  800. else
  801. gpPassword = NULL;
  802. if(wcslen(gAuthority) > 0)
  803. gpAuthority = gAuthority;
  804. else
  805. gpAuthority = NULL;
  806. if (g_pNamespace)
  807. {
  808. SetInterfaceSecurityEx(g_pNamespace, gpAuthority, gpUser, gpPassword,
  809. gdwAuthLevel, gdwImpLevel, EOAC_NONE, &gpAuthIdentity, &gpPrincipal );
  810. }
  811. if (g_pServicesEx)
  812. {
  813. SetInterfaceSecurityEx(g_pNamespace, gpAuthority, gpUser, gpPassword,
  814. gdwAuthLevel, gdwImpLevel, EOAC_NONE, &gpAuthIdentity, &gpPrincipal );
  815. }
  816. }
  817. if (FAILED(sRes))
  818. {
  819. FormatError(sRes, m_hDlg);
  820. SetDlgItemText(IDC_PASSWORD, "");
  821. return FALSE;
  822. }
  823. if(bBoundToObject)
  824. return FALSE;
  825. else
  826. {
  827. SysFreeString(g_strNamespace);
  828. g_strNamespace = SysAllocString(bsNamespace.GetString());
  829. return TRUE;
  830. }
  831. }
  832. //***************************************************************************
  833. //
  834. //***************************************************************************
  835. void CMainDlg::Connect(BOOL bBind)
  836. {
  837. if(m_lRefCount > 0)
  838. {
  839. MessageBox(IDS_MUST_CLOSE_WINDOWS, IDS_ERROR, MB_OK | MB_ICONSTOP);
  840. return;
  841. }
  842. CConnectDlg ConnectDlg(m_hDlg, bBind, m_lGenFlags, Timeout());
  843. INT_PTR nRes = ConnectDlg.Run();
  844. ConnectButtons(g_pNamespace!=NULL);
  845. if(nRes != IDOK)
  846. {
  847. if(g_pNamespace == NULL)
  848. SetDlgItemText(IDC_NAMESPACE, "");
  849. return;
  850. }
  851. // If here, we succeeded.
  852. // ======================
  853. SetDlgItemTextWC(m_hDlg, IDC_NAMESPACE, gNameSpace);
  854. ConnectButtons(TRUE);
  855. }
  856. //***************************************************************************
  857. //
  858. //***************************************************************************
  859. void CMainDlg::OpenHelp(void)
  860. {
  861. TCHAR helpFile[]=TEXT("\\HELP\\WBEMTEST.CHM");
  862. const int maxPath = MAX_PATH+sizeof(helpFile)/sizeof(TCHAR);
  863. TCHAR sysLoc[maxPath];
  864. const int pathLen = GetWindowsDirectory (sysLoc,MAX_PATH);
  865. if (pathLen == 0 || pathLen >=MAX_PATH)
  866. return;
  867. _tcscat(sysLoc,helpFile);
  868. HtmlHelp(m_hDlg,sysLoc,HH_DISPLAY_TOPIC,0);
  869. }
  870. //***************************************************************************
  871. //
  872. //***************************************************************************
  873. LRESULT CALLBACK GetClassNameDlgProc(
  874. HWND hDlg,
  875. UINT uMsg,
  876. WPARAM wParam,
  877. LPARAM lParam
  878. )
  879. {
  880. switch (uMsg)
  881. {
  882. case WM_INITDIALOG:
  883. return TRUE;
  884. case WM_COMMAND:
  885. switch (LOWORD(wParam))
  886. {
  887. case IDOK:
  888. *g_pTargetClassBuf = 0;
  889. GetDlgItemTextX(hDlg, IDC_CLASS_NAME, g_pTargetClassBuf,
  890. g_nMaxTargetClassBuf);
  891. EndDialog(hDlg, IDOK);
  892. return TRUE;
  893. case IDCANCEL:
  894. EndDialog(hDlg, IDCANCEL);
  895. return TRUE;
  896. }
  897. }
  898. return FALSE;
  899. }
  900. //***************************************************************************
  901. //
  902. //***************************************************************************
  903. INT_PTR GetClassName(HWND hDlg, wchar_t *pszClassName, int nLimit)
  904. {
  905. g_pTargetClassBuf = pszClassName;
  906. g_nMaxTargetClassBuf = nLimit;
  907. INT_PTR nRes = DialogBox(GetModuleHandle(0),
  908. MAKEINTRESOURCE(IDD_CLASS_NAME), hDlg,
  909. (DLGPROC) GetClassNameDlgProc
  910. );
  911. return nRes;
  912. }
  913. BOOL _GetObject(HWND hDlg, LONG lGenFlags, wchar_t *pszPath, LONG lSync, IWbemClassObject*& pClass,
  914. LONG lTimeout, bool bSuppressErrors = false)
  915. {
  916. CBString bsPath(pszPath);
  917. pClass = NULL;
  918. HRESULT res;
  919. IWbemClassObject* pErrorObj = NULL;
  920. // Asynchronous
  921. if (lSync & ASYNC)
  922. {
  923. CHourGlass hg;
  924. CTestNotify* pNtfy = new CTestNotify(1);
  925. res = g_pNamespace->GetObjectAsync(bsPath.GetString(),
  926. lGenFlags | WBEM_FLAG_SEND_STATUS,
  927. g_Context, CUnsecWrap(pNtfy));
  928. if(SUCCEEDED(res))
  929. {
  930. pNtfy->WaitForSignal(INFINITE);
  931. res = pNtfy->GetStatusCode(&pErrorObj);
  932. if(SUCCEEDED(res))
  933. {
  934. CFlexArray* pArray = pNtfy->GetObjectArray();
  935. if(pArray && pArray->Size() > 0)
  936. {
  937. pClass = (IWbemClassObject*)pArray->GetAt(0);
  938. if (pClass)
  939. pClass->AddRef();
  940. }
  941. }
  942. }
  943. pNtfy->Release();
  944. }
  945. // Semisynchronous
  946. else if (lSync & SEMISYNC)
  947. {
  948. IWbemCallResult* pCallRes = NULL;
  949. CHourGlass hg;
  950. res = g_pNamespace->GetObject(bsPath.GetString(),
  951. lGenFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  952. g_Context, &pClass, &pCallRes);
  953. if (SUCCEEDED(res))
  954. {
  955. LONG lStatus;
  956. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  957. while ((res = pCallRes->GetCallStatus(lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  958. {
  959. // wait
  960. }
  961. if (res == WBEM_S_NO_ERROR)
  962. {
  963. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::GetObject call
  964. if ( SUCCEEDED (res) )
  965. {
  966. res = pCallRes->GetResultObject(0, &pClass); // don't use timeout since object should be available
  967. }
  968. else
  969. {
  970. if(!bSuppressErrors)
  971. FormatError(res, hDlg, pErrorObj);
  972. return FALSE;
  973. }
  974. }
  975. pCallRes->Release();
  976. }
  977. }
  978. // Synchronous
  979. else
  980. {
  981. CHourGlass hg;
  982. res = g_pNamespace->GetObject(bsPath.GetString(),
  983. lGenFlags,
  984. g_Context, &pClass, NULL);
  985. }
  986. if (FAILED(res))
  987. {
  988. if(!bSuppressErrors)
  989. FormatError(res, hDlg, pErrorObj);
  990. return FALSE;
  991. }
  992. return TRUE;
  993. }
  994. BOOL _PutClass(HWND hDlg, LONG lGenFlags, LONG lChgFlags, LONG lSync,
  995. IWbemClassObject* pClass, LONG lTimeout)
  996. {
  997. HRESULT res;
  998. IWbemClassObject* pErrorObj = NULL;
  999. // Asynchronous
  1000. if(lSync & ASYNC)
  1001. {
  1002. CHourGlass hg;
  1003. CTestNotify* pNtfy = new CTestNotify(1);
  1004. res = g_pNamespace->PutClassAsync(pClass,
  1005. lGenFlags | lChgFlags | WBEM_FLAG_SEND_STATUS,
  1006. g_Context, CUnsecWrap(pNtfy));
  1007. if(SUCCEEDED(res))
  1008. {
  1009. pNtfy->WaitForSignal(INFINITE);
  1010. res = pNtfy->GetStatusCode(&pErrorObj);
  1011. }
  1012. pNtfy->Release();
  1013. }
  1014. // Semisynchronous
  1015. else if (lSync & SEMISYNC)
  1016. {
  1017. IWbemCallResult* pCallRes = NULL;
  1018. CHourGlass hg;
  1019. res = g_pNamespace->PutClass(pClass,
  1020. lGenFlags | lChgFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  1021. g_Context, &pCallRes);
  1022. if (SUCCEEDED(res))
  1023. {
  1024. LONG lStatus;
  1025. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1026. while ((res = pCallRes->GetCallStatus(lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  1027. {
  1028. // wait
  1029. }
  1030. if (res == WBEM_S_NO_ERROR)
  1031. {
  1032. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::PutClass call
  1033. }
  1034. pCallRes->Release();
  1035. }
  1036. }
  1037. // Synchronous
  1038. else
  1039. {
  1040. CHourGlass hg;
  1041. res = g_pNamespace->PutClass(pClass,
  1042. lGenFlags | lChgFlags,
  1043. g_Context, NULL);
  1044. }
  1045. if (FAILED(res))
  1046. {
  1047. FormatError(res, hDlg, pErrorObj);
  1048. return FALSE;
  1049. }
  1050. return TRUE;
  1051. }
  1052. void ShowClass(HWND hDlg, LONG lGenFlags, LPWSTR wszClass, LONG lSync,
  1053. CRefCountable* pOwner, LONG lTimeout)
  1054. {
  1055. IWbemClassObject* pClass;
  1056. if(!_GetObject(hDlg, lGenFlags, wszClass, lSync, pClass, lTimeout))
  1057. return;
  1058. DWORD dwEditMode;
  1059. if(wcschr(wszClass, L'\\') || wcschr(wszClass, L':'))
  1060. dwEditMode = CObjectEditor::foreign;
  1061. else
  1062. dwEditMode = CObjectEditor::readwrite;
  1063. CObjectEditor* ped = new CObjectEditor(hDlg, lGenFlags, dwEditMode, lSync, pClass,
  1064. lTimeout);
  1065. ped->RunDetached(pOwner);
  1066. pClass->Release();
  1067. }
  1068. void CMainDlg::EditClass()
  1069. {
  1070. wchar_t Class[2048];
  1071. *Class = 0;
  1072. INT_PTR nRes = GetClassName(m_hDlg, Class, 2048);
  1073. if (nRes == IDCANCEL || wcslen(Class) == 0)
  1074. return;
  1075. // Create the requested class; the timeout is only used if semisync
  1076. IWbemClassObject* pClass;
  1077. if (!_GetObject(m_hDlg, m_lGenFlags, Class, m_lSync, pClass, Timeout()))
  1078. return;
  1079. DWORD dwEditMode = CObjectEditor::readwrite;
  1080. // Create an object that is guarenteed to be in the local server\namespace
  1081. IWbemClassObject *pLocalObj = 0;
  1082. if (_GetObject(m_hDlg, m_lGenFlags, L"__SystemClass", m_lSync, pLocalObj, Timeout(), true))
  1083. {
  1084. // Extract the server\namespace path for the objects
  1085. WCHAR szSNS1[2048], szSNS2[2048];
  1086. GetServerNamespace(pClass, szSNS1, 2048);
  1087. GetServerNamespace(pLocalObj, szSNS2, 2048);
  1088. // Set the edit permissions based on whether the requested
  1089. // object is in the same namespace
  1090. if (_wcsicmp(szSNS1, szSNS2))
  1091. dwEditMode = CObjectEditor::foreign;
  1092. }
  1093. if (pLocalObj)
  1094. pLocalObj->Release();
  1095. CObjectEditor* ped = new CObjectEditor(m_hDlg, m_lGenFlags, dwEditMode, m_lSync,
  1096. pClass, Timeout(), BatchCount());
  1097. ped->RunDetached(this);
  1098. pClass->Release();
  1099. }
  1100. //***************************************************************************
  1101. //
  1102. //***************************************************************************
  1103. static wchar_t *g_pSupClassBuf = 0;
  1104. static int g_nMaxSupClassBuf = 0;
  1105. LRESULT CALLBACK SuperclassDlgProc(
  1106. HWND hDlg,
  1107. UINT uMsg,
  1108. WPARAM wParam,
  1109. LPARAM lParam
  1110. )
  1111. {
  1112. switch (uMsg)
  1113. {
  1114. case WM_INITDIALOG:
  1115. return TRUE;
  1116. case WM_COMMAND:
  1117. switch (LOWORD(wParam))
  1118. {
  1119. case IDOK:
  1120. *g_pSupClassBuf = 0;
  1121. GetDlgItemTextX(hDlg, IDC_SUPERCLASS, g_pSupClassBuf,
  1122. g_nMaxSupClassBuf);
  1123. EndDialog(hDlg, IDOK);
  1124. return TRUE;
  1125. case IDCANCEL:
  1126. EndDialog(hDlg, IDCANCEL);
  1127. return TRUE;
  1128. }
  1129. }
  1130. return FALSE;
  1131. }
  1132. //***************************************************************************
  1133. //
  1134. //***************************************************************************
  1135. INT_PTR GetSuperclass(HWND hDlg, wchar_t *pszClassName, int nLimit)
  1136. {
  1137. g_pSupClassBuf = pszClassName;
  1138. g_nMaxSupClassBuf = nLimit;
  1139. INT_PTR nRes = DialogBox(GetModuleHandle(0),
  1140. MAKEINTRESOURCE(IDD_SUPERCLASS), hDlg,
  1141. (DLGPROC) SuperclassDlgProc
  1142. );
  1143. return nRes;
  1144. }
  1145. //***************************************************************************
  1146. //
  1147. //***************************************************************************
  1148. BOOL PreCreateClass(HWND hDlg, LONG lGenFlags, WCHAR* Superclass, LONG lSync,
  1149. IWbemClassObject*& pNewClass, LONG lTimeout)
  1150. {
  1151. // Get the parent class, if any
  1152. // ============================
  1153. IWbemClassObject* pParentClass = 0;
  1154. if (!_GetObject(hDlg, lGenFlags, Superclass, lSync, pParentClass, lTimeout))
  1155. {
  1156. return FALSE;
  1157. }
  1158. // Create an empty child class
  1159. // ===========================
  1160. HRESULT hres = WBEM_S_NO_ERROR;
  1161. if (wcslen(Superclass) > 0)
  1162. {
  1163. hres = pParentClass->SpawnDerivedClass(0, &pNewClass);
  1164. if (hres != WBEM_S_NO_ERROR)
  1165. {
  1166. FormatError(hres, hDlg);
  1167. pParentClass->Release();
  1168. return FALSE;
  1169. }
  1170. }
  1171. else
  1172. {
  1173. pNewClass = pParentClass;
  1174. pNewClass->AddRef();
  1175. }
  1176. pParentClass->Release();
  1177. if (FAILED(hres))
  1178. {
  1179. FormatError(hres, hDlg);
  1180. return FALSE;
  1181. }
  1182. VARIANT v;
  1183. V_VT(&v) = VT_BSTR;
  1184. V_BSTR(&v) = SysAllocString(L"");
  1185. pNewClass->Put(L"__CLASS", 0, &v, 0);
  1186. VariantClear(&v);
  1187. return TRUE;
  1188. }
  1189. BOOL _CreateClass(HWND hDlg, LONG lGenFlags, WCHAR* Superclass, LONG lSync,
  1190. IWbemClassObject** ppClass, LONG lTimeout)
  1191. {
  1192. IWbemClassObject* pNewClass;
  1193. if (!PreCreateClass(hDlg, lGenFlags, Superclass, lSync, pNewClass, lTimeout))
  1194. return FALSE;
  1195. // Start editing
  1196. // =============
  1197. CObjectEditor ed(hDlg, lGenFlags, CObjectEditor::readwrite, lSync, pNewClass,
  1198. lTimeout);
  1199. if (ed.Edit() == IDCANCEL)
  1200. {
  1201. pNewClass->Release();
  1202. return FALSE;
  1203. }
  1204. // Finally, commit the class definition if the user
  1205. // did not hit 'cancel'.
  1206. // =================================================
  1207. BOOL bRes = _PutClass(hDlg, lGenFlags, WBEM_FLAG_CREATE_ONLY, lSync, pNewClass, lTimeout);
  1208. if (bRes && ppClass)
  1209. *ppClass = pNewClass;
  1210. else
  1211. pNewClass->Release();
  1212. return bRes;
  1213. }
  1214. void CMainDlg::CreateClass()
  1215. {
  1216. wchar_t Superclass[2048];
  1217. *Superclass = 0;
  1218. // Allow user to specify a superclass.
  1219. // ====================================
  1220. INT_PTR nRes = GetSuperclass(m_hDlg, Superclass, 2048);
  1221. if ((nRes == IDCANCEL) || (nRes == 0))
  1222. return;
  1223. IWbemClassObject* pClass = NULL;
  1224. PreCreateClass(m_hDlg, m_lGenFlags, Superclass, m_lSync, pClass, Timeout());
  1225. if(pClass == NULL)
  1226. return;
  1227. CObjectEditor* ped = new CObjectEditor(m_hDlg, m_lGenFlags, CObjectEditor::readwrite,
  1228. m_lSync, pClass, Timeout(), BatchCount());
  1229. ped->RunDetached(this);
  1230. pClass->Release();
  1231. }
  1232. void CMainDlg::DeleteClass()
  1233. {
  1234. wchar_t ClassName[2048];
  1235. *ClassName = 0;
  1236. INT_PTR nRes = GetClassName(m_hDlg, ClassName, 2048);
  1237. if (nRes == IDCANCEL || wcslen(ClassName) == 0)
  1238. return;
  1239. nRes = MessageBox(IDS_DELETING_CLASS, IDS_WARNING, MB_YESNO | MB_ICONWARNING);
  1240. if(nRes != IDYES)
  1241. return;
  1242. CBString bsClassName(ClassName);
  1243. HRESULT res;
  1244. IWbemClassObject* pErrorObj = NULL;
  1245. // Asynchronous
  1246. if(m_lSync & ASYNC)
  1247. {
  1248. CHourGlass hg;
  1249. CTestNotify* pNtfy = new CTestNotify(1);
  1250. res = g_pNamespace->DeleteClassAsync(bsClassName.GetString(),
  1251. m_lGenFlags | WBEM_FLAG_SEND_STATUS,
  1252. g_Context, CUnsecWrap(pNtfy));
  1253. if (SUCCEEDED(res))
  1254. {
  1255. pNtfy->WaitForSignal(INFINITE);
  1256. res = pNtfy->GetStatusCode(&pErrorObj);
  1257. }
  1258. pNtfy->Release();
  1259. }
  1260. // Semisynchronous
  1261. else if (m_lSync & SEMISYNC)
  1262. {
  1263. IWbemCallResult* pCallRes = NULL;
  1264. CHourGlass hg;
  1265. res = g_pNamespace->DeleteClass(bsClassName.GetString(),
  1266. m_lGenFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  1267. g_Context, &pCallRes);
  1268. if (SUCCEEDED(res))
  1269. {
  1270. LONG lStatus;
  1271. LONG lTimeout = Timeout();
  1272. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1273. while ((res = pCallRes->GetCallStatus(lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  1274. {
  1275. // wait
  1276. }
  1277. if (res == WBEM_S_NO_ERROR)
  1278. {
  1279. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::DeleteClass call
  1280. }
  1281. pCallRes->Release();
  1282. }
  1283. }
  1284. // Synchronous
  1285. else
  1286. {
  1287. CHourGlass hg;
  1288. res = g_pNamespace->DeleteClass(bsClassName.GetString(),
  1289. m_lGenFlags,
  1290. g_Context, NULL);
  1291. }
  1292. if (FAILED(res))
  1293. {
  1294. FormatError(res, m_hDlg, pErrorObj);
  1295. return;
  1296. }
  1297. }
  1298. BOOL _PutInstance(HWND hDlg, LONG lGenFlags, LONG lChgFlags, LONG lSync,
  1299. IWbemClassObject* pInstance, LONG lTimeout)
  1300. {
  1301. HRESULT res;
  1302. IWbemClassObject* pErrorObj = NULL;
  1303. // Asynchronous
  1304. if(lSync & ASYNC)
  1305. {
  1306. CHourGlass hg;
  1307. CTestNotify* pNtfy = new CTestNotify(1);
  1308. res = g_pNamespace->PutInstanceAsync(pInstance,
  1309. lGenFlags | lChgFlags | WBEM_FLAG_SEND_STATUS,
  1310. g_Context, CUnsecWrap(pNtfy));
  1311. if (SUCCEEDED(res))
  1312. {
  1313. pNtfy->WaitForSignal(INFINITE);
  1314. res = pNtfy->GetStatusCode(&pErrorObj);
  1315. }
  1316. pNtfy->Release();
  1317. }
  1318. // Semisynchronous
  1319. else if (lSync & SEMISYNC)
  1320. {
  1321. IWbemCallResult* pCallRes = NULL;
  1322. CHourGlass hg;
  1323. res = g_pNamespace->PutInstance(pInstance,
  1324. lGenFlags | lChgFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  1325. g_Context, &pCallRes);
  1326. if (SUCCEEDED(res))
  1327. {
  1328. LONG lStatus;
  1329. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1330. while ((res = pCallRes->GetCallStatus(lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  1331. {
  1332. // wait
  1333. }
  1334. if (res == WBEM_S_NO_ERROR)
  1335. {
  1336. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::PutInstance call
  1337. }
  1338. pCallRes->Release();
  1339. }
  1340. }
  1341. // Synchronous
  1342. else
  1343. {
  1344. CHourGlass hg;
  1345. res = g_pNamespace->PutInstance(pInstance,
  1346. lGenFlags | lChgFlags,
  1347. g_Context, NULL);
  1348. }
  1349. if (FAILED(res))
  1350. {
  1351. FormatError(res, hDlg, pErrorObj);
  1352. return FALSE;
  1353. }
  1354. return TRUE;
  1355. }
  1356. IWbemClassObject* PreCreateInstance(HWND hDlg, LONG lGenFlags, LONG lSync, LONG lTimeout)
  1357. {
  1358. // Get the class.
  1359. // ==============
  1360. wchar_t ClassName[2048];
  1361. *ClassName = 0;
  1362. long lFlags = 0;
  1363. CCreateInstanceDlg InfoDlg(hDlg, ClassName, 2048, &lFlags);
  1364. INT_PTR nRes = InfoDlg.Run();
  1365. if (nRes == IDCANCEL || wcslen(ClassName) == 0)
  1366. return NULL;
  1367. IWbemClassObject* pInst = 0;
  1368. if(lFlags == 1)
  1369. {
  1370. IWbemClassObject* pClass = 0;
  1371. if (!_GetObject(hDlg, lGenFlags, ClassName, lSync, pClass, lTimeout))
  1372. return NULL;
  1373. // Get a clean instance of the class.
  1374. // ==================================
  1375. HRESULT hres = pClass->SpawnInstance(0, &pInst);
  1376. if (FAILED(hres))
  1377. {
  1378. FormatError(hres, hDlg);
  1379. return NULL;
  1380. }
  1381. }
  1382. else
  1383. {
  1384. if (!_GetObject(hDlg, WBEM_FLAG_SPAWN_INSTANCE, ClassName, lSync, pInst, lTimeout))
  1385. return NULL;
  1386. }
  1387. return pInst;
  1388. }
  1389. IWbemClassObject* _CreateInstance(HWND hDlg, LONG lGenFlags, LONG lSync, LONG lTimeout)
  1390. {
  1391. IWbemClassObject* pInst = PreCreateInstance(hDlg, lGenFlags, lSync, lTimeout);
  1392. if(pInst == NULL)
  1393. return NULL;
  1394. // If here, we have an instance we can edit.
  1395. // =========================================
  1396. CObjectEditor ed(hDlg, lGenFlags, CObjectEditor::readwrite, lSync, pInst, lTimeout);
  1397. if (ed.Edit() == IDCANCEL)
  1398. {
  1399. pInst->Release();
  1400. return NULL;
  1401. }
  1402. return pInst;
  1403. }
  1404. void CMainDlg::CreateInstance()
  1405. {
  1406. IWbemClassObject* pInst = PreCreateInstance(m_hDlg, m_lGenFlags, m_lSync, Timeout());
  1407. if(pInst == NULL)
  1408. return;
  1409. CObjectEditor* ped = new CObjectEditor(m_hDlg, m_lGenFlags, CObjectEditor::readwrite,
  1410. m_lSync, pInst, Timeout(), BatchCount());
  1411. ped->RunDetached(this);
  1412. pInst->Release();
  1413. }
  1414. /////////////////////////////////////////////////////////////////////////////
  1415. //
  1416. // Refresher Object Path Dialog
  1417. //
  1418. /////////////////////////////////////////////////////////////////////////////
  1419. static wchar_t *g_pszRefrObjectPath = 0;
  1420. static int g_nMaxRefrObjectPath = 0;
  1421. LRESULT CALLBACK RefrObjectPathDlgProc(
  1422. HWND hDlg,
  1423. UINT uMsg,
  1424. WPARAM wParam,
  1425. LPARAM lParam
  1426. )
  1427. {
  1428. switch (uMsg)
  1429. {
  1430. case WM_INITDIALOG:
  1431. return TRUE;
  1432. case WM_COMMAND:
  1433. switch (LOWORD(wParam))
  1434. {
  1435. case IDOK:
  1436. *g_pszRefrObjectPath = 0;
  1437. GetDlgItemTextX(hDlg, IDC_REFR_OBJECT_PATH, g_pszRefrObjectPath,
  1438. g_nMaxRefrObjectPath);
  1439. EndDialog(hDlg, IDOK);
  1440. return TRUE;
  1441. case IDC_ADDENUM:
  1442. *g_pszRefrObjectPath = 0;
  1443. GetDlgItemTextX(hDlg, IDC_REFR_OBJECT_PATH, g_pszRefrObjectPath,
  1444. g_nMaxRefrObjectPath);
  1445. EndDialog(hDlg, IDC_ADDENUM);
  1446. return TRUE;
  1447. case IDCANCEL:
  1448. EndDialog(hDlg, IDCANCEL);
  1449. return TRUE;
  1450. }
  1451. }
  1452. return FALSE;
  1453. }
  1454. INT_PTR GetRefrObjectPath(HWND hDlg, LPWSTR pStr, int nMax)
  1455. {
  1456. g_pszRefrObjectPath = pStr;
  1457. g_nMaxRefrObjectPath = nMax;
  1458. INT_PTR nRes = DialogBox(GetModuleHandle(0),
  1459. MAKEINTRESOURCE(IDD_REFRESHER_OBJECT_PATH), hDlg,
  1460. (DLGPROC) RefrObjectPathDlgProc
  1461. );
  1462. return nRes;
  1463. }
  1464. /////////////////////////////////////////////////////////////////////////////
  1465. //
  1466. // Object Path Dialog
  1467. //
  1468. /////////////////////////////////////////////////////////////////////////////
  1469. static wchar_t *g_pszObjectPath = 0;
  1470. static int g_nMaxObjectPath = 0;
  1471. LRESULT CALLBACK ObjectPathDlgProc(
  1472. HWND hDlg,
  1473. UINT uMsg,
  1474. WPARAM wParam,
  1475. LPARAM lParam
  1476. )
  1477. {
  1478. switch (uMsg)
  1479. {
  1480. case WM_INITDIALOG:
  1481. return TRUE;
  1482. case WM_COMMAND:
  1483. switch (LOWORD(wParam))
  1484. {
  1485. case IDOK:
  1486. *g_pszObjectPath = 0;
  1487. GetDlgItemTextX(hDlg, IDC_OBJECT_PATH, g_pszObjectPath,
  1488. g_nMaxObjectPath);
  1489. EndDialog(hDlg, IDOK);
  1490. return TRUE;
  1491. case IDCANCEL:
  1492. EndDialog(hDlg, IDCANCEL);
  1493. return TRUE;
  1494. }
  1495. }
  1496. return FALSE;
  1497. }
  1498. INT_PTR GetObjectPath(HWND hDlg, LPWSTR pStr, int nMax)
  1499. {
  1500. g_pszObjectPath = pStr;
  1501. g_nMaxObjectPath = nMax;
  1502. INT_PTR nRes = DialogBox(GetModuleHandle(0),
  1503. MAKEINTRESOURCE(IDD_OBJECT_PATH), hDlg,
  1504. (DLGPROC) ObjectPathDlgProc
  1505. );
  1506. return nRes;
  1507. }
  1508. BOOL GetServerNamespace(IWbemClassObject *pObj, WCHAR *szResult, int nMaxSize)
  1509. ///////////////////////////////////////////////////////////////////
  1510. //
  1511. // Calls Get on the object's path property, removes the colon
  1512. // and object name which results in a string representing the
  1513. // server\namespace.
  1514. //
  1515. // Parameters: a pointer to a Class Object, a pointer to the output
  1516. // string, and the maximum size of the output string
  1517. //
  1518. // Returns : True if success.
  1519. //
  1520. ///////////////////////////////////////////////////////////////////
  1521. {
  1522. VARIANT vObjNS;
  1523. VariantInit(&vObjNS);
  1524. // Get the full object path
  1525. HRESULT hRes = pObj->Get(L"__PATH", 0, &vObjNS, NULL, NULL);
  1526. if (FAILED(hRes))
  1527. return FALSE;
  1528. // Identify the colon location
  1529. int len = wcscspn(V_BSTR(&vObjNS), L":");
  1530. // Check boundary of the result string
  1531. if (len + 1 > nMaxSize)
  1532. return FALSE;
  1533. // Copy the server\namespace
  1534. wcsncpy(szResult, V_BSTR(&vObjNS), len);
  1535. szResult[len] = L'\0';
  1536. VariantClear(&vObjNS);
  1537. return TRUE;
  1538. }
  1539. void CMainDlg::EditInstance()
  1540. {
  1541. wchar_t ObjPath[2560];
  1542. // Get the path for the object to be created
  1543. INT_PTR iRet = GetObjectPath(m_hDlg, ObjPath, 2560);
  1544. if(iRet == IDCANCEL)
  1545. return;
  1546. if (wcslen(ObjPath) == 0)
  1547. {
  1548. MessageBox(IDS_NO_PATH, IDS_ERROR, MB_OK);
  1549. return;
  1550. }
  1551. // Create the requested object; the timeout is only used if semisync
  1552. IWbemClassObject *pInst = 0;
  1553. if (!_GetObject(m_hDlg, m_lGenFlags, ObjPath, m_lSync, pInst, Timeout()))
  1554. return;
  1555. DWORD dwEditMode = CObjectEditor::readwrite;
  1556. // Create an object that is guarenteed to be in the server\namespace
  1557. // This is used so that we can gray out the "save" buttons in the case
  1558. // where an a GetObject is passed another namespace and we dont have the
  1559. // IWbemServices pointer to it.
  1560. IWbemClassObject *pLocalObj = 0;
  1561. if (_GetObject(m_hDlg, m_lGenFlags, L"__SystemClass", m_lSync, pLocalObj,
  1562. Timeout(), true))
  1563. {
  1564. // Extract the server\namespace path for the objects
  1565. WCHAR szSNS1[2048], szSNS2[2048];
  1566. GetServerNamespace(pInst, szSNS1, 2048);
  1567. GetServerNamespace(pLocalObj, szSNS2, 2048);
  1568. // Set the edit permissions based on whether the requested
  1569. // object is in the same namespace
  1570. if(_wcsicmp(szSNS1, szSNS2))
  1571. dwEditMode = CObjectEditor::foreign;
  1572. if (pLocalObj)
  1573. pLocalObj->Release();
  1574. }
  1575. CObjectEditor* ped = new CObjectEditor(m_hDlg, m_lGenFlags, dwEditMode, m_lSync,
  1576. pInst, Timeout(), BatchCount());
  1577. ped->RunDetached(this);
  1578. pInst->Release();
  1579. }
  1580. void CMainDlg::DeleteInstance()
  1581. {
  1582. wchar_t ObjPath[2560];
  1583. *ObjPath = 0;
  1584. INT_PTR iRet = GetObjectPath(m_hDlg, ObjPath, 2560);
  1585. if (iRet == IDCANCEL)
  1586. return;
  1587. if (wcslen(ObjPath) == 0)
  1588. return;
  1589. CBString bsObjPath(ObjPath);
  1590. HRESULT res;
  1591. IWbemClassObject* pErrorObj = NULL;
  1592. // Asynchronous
  1593. if (m_lSync & ASYNC)
  1594. {
  1595. CHourGlass hg;
  1596. CTestNotify* pNtfy = new CTestNotify(1);
  1597. res = g_pNamespace->DeleteInstanceAsync(bsObjPath.GetString(),
  1598. m_lGenFlags | WBEM_FLAG_SEND_STATUS,
  1599. g_Context, CUnsecWrap(pNtfy));
  1600. if (SUCCEEDED(res))
  1601. {
  1602. pNtfy->WaitForSignal(INFINITE);
  1603. res = pNtfy->GetStatusCode(&pErrorObj);
  1604. }
  1605. pNtfy->Release();
  1606. }
  1607. // Semisynchronous
  1608. else if (m_lSync & SEMISYNC)
  1609. {
  1610. IWbemCallResult* pCallRes = NULL;
  1611. CHourGlass hg;
  1612. res = g_pNamespace->DeleteInstance(bsObjPath.GetString(),
  1613. m_lGenFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  1614. g_Context, &pCallRes);
  1615. if (SUCCEEDED(res))
  1616. {
  1617. LONG lStatus;
  1618. LONG lTimeout = Timeout();
  1619. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1620. while ((res = pCallRes->GetCallStatus(lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  1621. {
  1622. // wait
  1623. }
  1624. if (res == WBEM_S_NO_ERROR)
  1625. {
  1626. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::DeleteIntance call
  1627. }
  1628. pCallRes->Release();
  1629. }
  1630. }
  1631. // Synchronous
  1632. else
  1633. {
  1634. CHourGlass hg;
  1635. res = g_pNamespace->DeleteInstance(bsObjPath.GetString(), m_lGenFlags,
  1636. g_Context, NULL);
  1637. }
  1638. if (FAILED(res))
  1639. {
  1640. FormatError(res, m_hDlg, pErrorObj);
  1641. }
  1642. }
  1643. //***************************************************************************
  1644. //
  1645. //***************************************************************************
  1646. class CNotificationQueryResultDlg : public CQueryResultDlg
  1647. {
  1648. public:
  1649. CNotificationQueryResultDlg(HWND hParent, LONG lGenFlags, LONG lQryFlags, LONG lSync )
  1650. : CQueryResultDlg(hParent, lGenFlags, lQryFlags, FALSE)
  1651. {
  1652. if ( SEMISYNC == lSync )
  1653. {
  1654. CMainDlg * pMainDlg = (CMainDlg*)GetWindowLongPtr(hParent, DWLP_USER);
  1655. pMainDlg->m_fNotificationQueryResultDlg = TRUE;
  1656. }
  1657. };
  1658. ~CNotificationQueryResultDlg( )
  1659. {
  1660. if ( SEMISYNC == m_lSync )
  1661. {
  1662. CMainDlg * pMainDlg = (CMainDlg*)GetWindowLongPtr(m_hParent, DWLP_USER);
  1663. if ( NULL != pMainDlg )
  1664. {
  1665. pMainDlg->m_fNotificationQueryResultDlg = FALSE;
  1666. }
  1667. else
  1668. {
  1669. // this means that the main dlg has been yanked away, so reset owner, because owner is GONE
  1670. m_pOwner = NULL;
  1671. }
  1672. }
  1673. }
  1674. };
  1675. //***************************************************************************
  1676. //
  1677. //***************************************************************************
  1678. class CInstanceListDlg : public CQueryResultDlg
  1679. {
  1680. protected:
  1681. BSTR m_strClass;
  1682. BOOL CanAdd() { return !m_bReadOnly && m_lQryFlags & WBEM_FLAG_SHALLOW; }
  1683. IWbemClassObject* AddNewElement();
  1684. public:
  1685. CInstanceListDlg(HWND hParent, LONG lGenFlags, LONG lQryFlags, LPWSTR wszClass,
  1686. LONG lSync, BOOL bReadOnly, LONG lTimeout, ULONG nBatch)
  1687. : CQueryResultDlg(hParent, lGenFlags, lQryFlags), m_strClass(SysAllocString(wszClass))
  1688. {
  1689. SetReadOnly(bReadOnly);
  1690. // Pass on invocation method (sync, async..) related settings for use in this
  1691. // enumeration and by any further operations (editing/deleting/etc. of an instance).
  1692. SetCallMethod(lSync);
  1693. SetTimeout(lTimeout);
  1694. SetBatchCount(nBatch);
  1695. }
  1696. ~CInstanceListDlg()
  1697. {
  1698. SysFreeString(m_strClass);
  1699. }
  1700. BOOL Initialize();
  1701. };
  1702. BOOL CInstanceListDlg::Initialize()
  1703. {
  1704. char szTitle[1024];
  1705. char szFormat[1024];
  1706. LoadString(GetModuleHandle(NULL), IDS_INSTANCES_OF, szFormat, 1024);
  1707. sprintf(szTitle, szFormat, m_strClass);
  1708. if(m_lQryFlags & WBEM_FLAG_DEEP)
  1709. {
  1710. LoadString(GetModuleHandle(NULL), IDS_RECURSIVE_PARENS, szFormat, 1024);
  1711. strcat(szTitle, szFormat);
  1712. }
  1713. SetTitle(szTitle);
  1714. // Asynchronous
  1715. if(m_lSync & ASYNC)
  1716. {
  1717. CNotSink* pHandler = new CNotSink(this); // contructor starts ref count at 1
  1718. SetNotify(pHandler);
  1719. HRESULT hres;
  1720. {
  1721. CHourGlass hg;
  1722. hres = g_pNamespace->CreateInstanceEnumAsync(m_strClass,
  1723. m_lGenFlags | m_lQryFlags | WBEM_FLAG_SEND_STATUS,
  1724. g_Context, GetWrapper());
  1725. }
  1726. pHandler->Release();
  1727. if (FAILED(hres))
  1728. {
  1729. FormatError(hres, m_hParent);
  1730. return FALSE;
  1731. }
  1732. }
  1733. // Semisynchronous
  1734. else if (m_lSync & SEMISYNC)
  1735. {
  1736. IEnumWbemClassObject* pEnum = NULL;
  1737. HRESULT hres;
  1738. {
  1739. CHourGlass hg;
  1740. hres = g_pNamespace->CreateInstanceEnum(m_strClass,
  1741. m_lGenFlags | m_lQryFlags | WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  1742. g_Context, &pEnum);
  1743. }
  1744. if (FAILED(hres))
  1745. {
  1746. FormatError(hres, m_hParent);
  1747. return FALSE;
  1748. }
  1749. // Create sink for use in NextAsync calls.
  1750. if (m_lSync & USE_NEXTASYNC)
  1751. {
  1752. CNotSink* pHandler = new CNotSink(this); // contructor starts ref count at 1
  1753. SetNotify(pHandler);
  1754. pHandler->Release();
  1755. }
  1756. SetInterfaceSecurityEx(pEnum, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1757. SetEnum(pEnum);
  1758. pEnum->Release();
  1759. }
  1760. // Synchronous
  1761. else
  1762. {
  1763. IEnumWbemClassObject* pEnum = NULL;
  1764. HRESULT hres;
  1765. {
  1766. CHourGlass hg;
  1767. hres = g_pNamespace->CreateInstanceEnum(m_strClass,
  1768. m_lGenFlags | m_lQryFlags | WBEM_FLAG_FORWARD_ONLY,
  1769. g_Context, &pEnum);
  1770. }
  1771. if (FAILED(hres))
  1772. {
  1773. FormatError(hres, m_hParent);
  1774. return FALSE;
  1775. }
  1776. SetInterfaceSecurityEx(pEnum, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1777. SetEnum(pEnum,hres);
  1778. pEnum->Release();
  1779. }
  1780. return TRUE;
  1781. }
  1782. IWbemClassObject* CInstanceListDlg::AddNewElement()
  1783. {
  1784. IWbemClassObject* pClass = 0;
  1785. if(!_GetObject(m_hDlg, m_lGenFlags, m_strClass, m_lSync, pClass, m_lTimeout))
  1786. return NULL;
  1787. // Get a clean instance of the class.
  1788. // ==================================
  1789. IWbemClassObject* pInst = 0;
  1790. HRESULT hres = pClass->SpawnInstance(0, &pInst);
  1791. pClass->Release();
  1792. if (FAILED(hres))
  1793. {
  1794. FormatError(hres, m_hDlg);
  1795. return NULL;
  1796. }
  1797. CObjectEditor ed(m_hDlg, m_lGenFlags, CObjectEditor::readwrite, m_lSync, pInst,
  1798. m_lTimeout, m_nBatch);
  1799. if (ed.Edit() == IDCANCEL)
  1800. {
  1801. pInst->Release();
  1802. return NULL;
  1803. }
  1804. if(_PutInstance(m_hDlg, m_lGenFlags, WBEM_FLAG_CREATE_ONLY, m_lSync, pInst, m_lTimeout))
  1805. return pInst;
  1806. else
  1807. {
  1808. pInst->Release();
  1809. return NULL;
  1810. }
  1811. }
  1812. void ShowInstances(HWND hDlg, LONG lGenFlags, LONG lQryFlags, LPWSTR wszClass, LONG lSync,
  1813. CRefCountable* pOwner, LONG lTimeout, ULONG nBatch)
  1814. {
  1815. CInstanceListDlg* pDlg = new CInstanceListDlg(hDlg, lGenFlags, lQryFlags, wszClass, lSync,
  1816. FALSE, lTimeout, nBatch);
  1817. pDlg->RunDetached(pOwner);
  1818. }
  1819. void CMainDlg::GetInstances()
  1820. {
  1821. wchar_t ClassName[2560];
  1822. *ClassName = 0;
  1823. LONG lQryFlags = 0;
  1824. INT_PTR nRes = GetClassInfo(m_hDlg, ClassName, 2560, &lQryFlags);
  1825. if ((nRes == IDCANCEL) || (nRes == 0))
  1826. return;
  1827. CInstanceListDlg* pDlg = new CInstanceListDlg(m_hDlg, m_lGenFlags, lQryFlags, ClassName, m_lSync,
  1828. FALSE, Timeout(), BatchCount());
  1829. pDlg->RunDetached(this);
  1830. }
  1831. //********************************************************************
  1832. class CClassListDlg : public CQueryResultDlg
  1833. {
  1834. protected:
  1835. BSTR m_strParentClass;
  1836. BOOL CanAdd() { return !m_bReadOnly; /* && m_lQryFlags & WBEM_FLAG_SHALLOW; */ }
  1837. IWbemClassObject* AddNewElement();
  1838. public:
  1839. CClassListDlg(HWND hParent, LONG lGenFlags, LONG lQryFlags, LPWSTR wszParentClass, LONG lSync,
  1840. BOOL bReadOnly, LONG lTimeout, ULONG nBatch)
  1841. : CQueryResultDlg(hParent, lGenFlags, lQryFlags, TRUE, IDD_QUERY_RESULT_SORTED),
  1842. m_strParentClass(SysAllocString(wszParentClass))
  1843. {
  1844. SetReadOnly(bReadOnly);
  1845. // Pass on invocation method (sync, async..) related settings for use in this
  1846. // enumeration and by any further operations (editing/deleting/etc. of an instance).
  1847. SetCallMethod(lSync);
  1848. SetTimeout(lTimeout);
  1849. SetBatchCount(nBatch);
  1850. }
  1851. ~CClassListDlg()
  1852. {
  1853. SysFreeString(m_strParentClass);
  1854. }
  1855. BOOL Initialize();
  1856. };
  1857. BOOL CClassListDlg::Initialize()
  1858. {
  1859. const TitleSize = 1000;
  1860. char szTitle[TitleSize];
  1861. char szFormat[1024];
  1862. if (m_strParentClass == NULL || wcslen(m_strParentClass) == 0)
  1863. {
  1864. LoadString(GetModuleHandle(NULL), IDS_TOP_LEVEL_CLASSES, szTitle, TitleSize);
  1865. }
  1866. else
  1867. {
  1868. LoadString(GetModuleHandle(NULL), IDS_CHILD_CLASSES_OF, szFormat, 1024);
  1869. sprintf(szTitle, szFormat, m_strParentClass);
  1870. }
  1871. szTitle[TitleSize-1] = '\0';
  1872. if (m_lQryFlags & WBEM_FLAG_DEEP)
  1873. {
  1874. LoadString(GetModuleHandle(NULL), IDS_RECURSIVE_PARENS, szFormat, 1024);
  1875. strncat(szTitle, szFormat, sizeof(szTitle) - strlen(szTitle) - 1);
  1876. }
  1877. SetTitle(szTitle);
  1878. // Asynchronous
  1879. if (m_lSync & ASYNC)
  1880. {
  1881. CHourGlass hg;
  1882. CNotSink* pHandler = new CNotSink(this);
  1883. SetNotify(pHandler);
  1884. HRESULT hres = g_pNamespace->CreateClassEnumAsync(m_strParentClass,
  1885. m_lGenFlags | m_lQryFlags | WBEM_FLAG_SEND_STATUS,
  1886. g_Context, GetWrapper());
  1887. if (FAILED(hres))
  1888. {
  1889. FormatError(hres, m_hParent);
  1890. return FALSE;
  1891. }
  1892. pHandler->Release();
  1893. }
  1894. // Semisynchronous
  1895. else if (m_lSync & SEMISYNC)
  1896. {
  1897. IEnumWbemClassObject* pEnum = NULL;
  1898. HRESULT hres;
  1899. {
  1900. CHourGlass hg;
  1901. hres = g_pNamespace->CreateClassEnum(m_strParentClass,
  1902. m_lGenFlags | m_lQryFlags | WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  1903. g_Context, &pEnum);
  1904. }
  1905. if (FAILED(hres))
  1906. {
  1907. FormatError(hres, m_hParent);
  1908. return FALSE;
  1909. }
  1910. // Create sink for use in NextAsync calls.
  1911. if (m_lSync & USE_NEXTASYNC)
  1912. {
  1913. CNotSink* pHandler = new CNotSink(this); // contructor starts ref count at 1
  1914. SetNotify(pHandler);
  1915. pHandler->Release();
  1916. }
  1917. SetInterfaceSecurityEx(pEnum, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1918. SetEnum(pEnum);
  1919. pEnum->Release();
  1920. }
  1921. // Synchronous
  1922. else
  1923. {
  1924. IEnumWbemClassObject* pEnum = NULL;
  1925. HRESULT hres;
  1926. {
  1927. CHourGlass hg;
  1928. hres = g_pNamespace->CreateClassEnum(m_strParentClass,
  1929. m_lGenFlags | m_lQryFlags | WBEM_FLAG_FORWARD_ONLY,
  1930. g_Context, &pEnum);
  1931. }
  1932. if (FAILED(hres))
  1933. {
  1934. FormatError(hres, m_hParent);
  1935. return FALSE;
  1936. }
  1937. SetInterfaceSecurityEx(pEnum, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1938. SetEnum(pEnum,hres);
  1939. pEnum->Release();
  1940. }
  1941. return TRUE;
  1942. }
  1943. IWbemClassObject* CClassListDlg::AddNewElement()
  1944. {
  1945. IWbemClassObject* pClass;
  1946. if(_CreateClass(m_hDlg, m_lGenFlags, m_strParentClass, m_lSync, &pClass, m_lTimeout))
  1947. return pClass;
  1948. else
  1949. return NULL;
  1950. }
  1951. void ShowClasses(HWND hDlg, LONG lGenFlags, LONG lQryFlags, LPWSTR wszParentClass, LONG lSync,
  1952. CRefCountable* pOwner, LONG lTimeout, ULONG nBatch)
  1953. {
  1954. CClassListDlg* pDlg = new CClassListDlg(hDlg, lGenFlags, lQryFlags, wszParentClass,
  1955. lSync, FALSE, lTimeout, nBatch);
  1956. pDlg->RunDetached(pOwner);
  1957. }
  1958. void CMainDlg::GetClasses()
  1959. {
  1960. wchar_t Superclass[2048];
  1961. *Superclass = 0;
  1962. LONG lQryFlags = 0;
  1963. INT_PTR nRes = GetSuperclassInfo(m_hDlg, Superclass, 2048, &lQryFlags);
  1964. if ((nRes == IDCANCEL) || (nRes == 0))
  1965. return;
  1966. CClassListDlg* pDlg = new CClassListDlg(m_hDlg, m_lGenFlags, lQryFlags, Superclass, m_lSync,
  1967. FALSE, Timeout(), BatchCount());
  1968. pDlg->RunDetached(this);
  1969. }
  1970. //***************************************************************************
  1971. //
  1972. //***************************************************************************
  1973. void CMainDlg::OpenNs()
  1974. {
  1975. wchar_t ObjPath[2560];
  1976. INT_PTR iRet = GetObjectPath(m_hDlg, ObjPath, 2560);
  1977. if(iRet == IDCANCEL)
  1978. return;
  1979. if (wcslen(ObjPath) == 0)
  1980. {
  1981. MessageBox(IDS_NO_PATH, IDS_ERROR, MB_OK);
  1982. return;
  1983. }
  1984. if (g_pNamespace == 0)
  1985. {
  1986. MessageBox(IDS_NO_INITIAL_CONNECTION, IDS_ERROR, MB_OK);
  1987. return;
  1988. }
  1989. IWbemServices* pNewNs = NULL;
  1990. CBString bsObjPath(ObjPath);
  1991. HRESULT res;
  1992. IWbemClassObject* pErrorObj = NULL;
  1993. // Asynchronous
  1994. if (m_lSync & ASYNC)
  1995. {
  1996. MessageBox(IDS_ASYNC_NOT_SUPPORTED, IDS_ERROR, MB_OK);
  1997. return;
  1998. }
  1999. // Semisynchronous
  2000. else if (m_lSync & SEMISYNC)
  2001. {
  2002. IWbemCallResult* pCallRes = NULL;
  2003. CHourGlass hg;
  2004. res = g_pNamespace->OpenNamespace(bsObjPath.GetString(),
  2005. m_lGenFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  2006. g_Context, NULL, &pCallRes);
  2007. if (SUCCEEDED(res))
  2008. {
  2009. LONG lStatus;
  2010. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  2011. while ((res = pCallRes->GetCallStatus(Timeout(), &lStatus)) == WBEM_S_TIMEDOUT)
  2012. {
  2013. // wait
  2014. }
  2015. if (res == WBEM_S_NO_ERROR)
  2016. {
  2017. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::OpenNamespace call
  2018. if (res == WBEM_S_NO_ERROR)
  2019. {
  2020. res = pCallRes->GetResultServices(0, &pNewNs); // don't use timeout since object should be available
  2021. }
  2022. }
  2023. pCallRes->Release();
  2024. }
  2025. }
  2026. // Synchronous
  2027. else
  2028. {
  2029. CHourGlass hg;
  2030. res = g_pNamespace->OpenNamespace(bsObjPath.GetString(),
  2031. m_lGenFlags,
  2032. g_Context, &pNewNs, NULL);
  2033. }
  2034. if (FAILED(res))
  2035. {
  2036. FormatError(res, m_hDlg, pErrorObj);
  2037. return;
  2038. }
  2039. g_pNamespace->Release();
  2040. pNewNs->QueryInterface(IID_IWbemServices, (void **)&g_pNamespace);
  2041. pNewNs->Release();
  2042. SetInterfaceSecurityEx(g_pNamespace, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  2043. wcscat(gNameSpace, L"\\");
  2044. wcscat(gNameSpace, bsObjPath.GetString());
  2045. SetDlgItemTextWC(m_hDlg, IDC_NAMESPACE, gNameSpace);
  2046. SysFreeString(g_strNamespace);
  2047. g_strNamespace = SysAllocString(gNameSpace);
  2048. }
  2049. void CMainDlg::CreateRefresher()
  2050. {
  2051. CRefresherDlg* pDlg = new CRefresherDlg(m_hDlg, m_lGenFlags);
  2052. pDlg->RunDetached(NULL);
  2053. }
  2054. void CMainDlg::EditContext()
  2055. {
  2056. g_Context.Edit(m_hDlg);
  2057. }
  2058. //***************************************************************************
  2059. //
  2060. //***************************************************************************
  2061. void CenterOnScreen(HWND hDlg)
  2062. {
  2063. HWND hScreen = GetDesktopWindow();
  2064. RECT rScreen;
  2065. GetClientRect(hScreen, &rScreen);
  2066. RECT rDlg;
  2067. GetWindowRect(hDlg, &rDlg);
  2068. int nX = (rScreen.right - (rDlg.right - rDlg.left))/2;
  2069. int nY = (rScreen.bottom - (rDlg.bottom - rDlg.top))/2;
  2070. MoveWindow(hDlg, nX, nY, rDlg.right - rDlg.left,
  2071. rDlg.bottom - rDlg.top, TRUE);
  2072. }
  2073. void CMainDlg::ConnectButtons(BOOL b)
  2074. {
  2075. EnableWindow(GetDlgItem(IDC_CREATE_CLASS), b);
  2076. EnableWindow(GetDlgItem(IDC_GET_CLASS), b);
  2077. EnableWindow(GetDlgItem(IDC_GET_CLASSES), b);
  2078. EnableWindow(GetDlgItem(IDC_EDIT_CLASS), b);
  2079. EnableWindow(GetDlgItem(IDC_DELETE_CLASS), b);
  2080. EnableWindow(GetDlgItem(IDC_CREATE_INSTANCE), b);
  2081. EnableWindow(GetDlgItem(IDC_GET_INSTANCE), b);
  2082. EnableWindow(GetDlgItem(IDC_GET_INSTANCES), b);
  2083. EnableWindow(GetDlgItem(IDC_EDIT_INSTANCE), b);
  2084. EnableWindow(GetDlgItem(IDC_DELETE_INSTANCE), b);
  2085. EnableWindow(GetDlgItem(IDC_QUERY), b);
  2086. /// EnableWindow(GetDlgItem(IDC_ASYNC), TRUE);
  2087. /// EnableWindow(GetDlgItem(IDC_SYNC), TRUE);
  2088. /// EnableWindow(GetDlgItem(IDC_SEMISYNC), TRUE);
  2089. EnableWindow(GetDlgItem(IDC_ASYNC), b);
  2090. EnableWindow(GetDlgItem(IDC_SYNC), b);
  2091. EnableWindow(GetDlgItem(IDC_SEMISYNC), b);
  2092. EnableWindow(GetDlgItem(IDC_BATCH), b);
  2093. EnableWindow(GetDlgItem(IDC_TIMEOUT), b);
  2094. EnableWindow(GetDlgItem(IDC_BATCH), b && !(m_lSync & ASYNC));
  2095. EnableWindow(GetDlgItem(IDC_TIMEOUT), b && m_lSync & SEMISYNC && !(m_lSync & USE_NEXTASYNC));
  2096. EnableWindow(GetDlgItem(IDC_USE_NEXTASYNC), b && m_lSync & SEMISYNC);
  2097. EnableWindow(GetDlgItem(IDC_USE_AMENDED), b);
  2098. EnableWindow(GetDlgItem(IDC_DIRECT_READ), b);
  2099. EnableWindow(GetDlgItem(IDC_REGISTER), b);
  2100. EnableWindow(GetDlgItem(IDC_VIEW_REG), b);
  2101. EnableWindow(GetDlgItem(IDC_EVENTS), b);
  2102. EnableWindow(GetDlgItem(IDC_EXEC_METHOD), b);
  2103. EnableWindow(GetDlgItem(IDC_OPEN_NS), b);
  2104. EnableWindow(GetDlgItem(IDC_REFRESH), b);
  2105. // IWbemServicesEx
  2106. #if 0
  2107. RAID 167868
  2108. bool ex=(b && g_pServicesEx!=NULL);
  2109. EnableWindow(GetDlgItem(IDC_OPEN), ex);
  2110. EnableWindow(GetDlgItem(IDC_ADDOBJECT), ex);
  2111. EnableWindow(GetDlgItem(IDC_DELETEOBJECT), ex);
  2112. EnableWindow(GetDlgItem(IDC_RENAMEOBJECT), ex);
  2113. EnableWindow(GetDlgItem(IDC_GETOBJECTSECURITY), ex);
  2114. EnableWindow(GetDlgItem(IDC_SETOBJECTSECURITY), ex);
  2115. #endif
  2116. }
  2117. //////////////////
  2118. //////////////////
  2119. CQueryResultDlg::CQueryResultDlg(HWND hParent, LONG lGenFlags, LONG lQryFlags, BOOL fCanDelete, int tID)
  2120. : CWbemDialog(tID, hParent), m_pHandler(NULL), m_bReadOnly(FALSE), m_fDeletesAllowed(fCanDelete),
  2121. m_pEnum(NULL), m_pWrapper(NULL), m_bComplete(FALSE),
  2122. m_lGenFlags(lGenFlags), m_lQryFlags(lQryFlags),
  2123. m_lTimeout(0), m_nBatch(1),
  2124. m_partial_result(false)
  2125. {
  2126. m_szTitle = 0;
  2127. m_nReturnedMax = 0;
  2128. if (tID == IDD_QUERY_RESULT_SORTED)
  2129. {
  2130. m_bSort = TRUE;
  2131. }
  2132. else
  2133. {
  2134. m_bSort = FALSE;
  2135. }
  2136. }
  2137. void CQueryResultDlg::SetEnum(IEnumWbemClassObject* pEnum, HRESULT hres)
  2138. {
  2139. set_partial (hres==WBEM_S_PARTIAL_RESULTS);
  2140. m_pEnum = pEnum;
  2141. if(m_pEnum)
  2142. m_pEnum->AddRef();
  2143. if(m_hDlg)
  2144. {
  2145. PostUserMessage(m_hDlg, 0, 0); // starts processing of enumeration
  2146. }
  2147. }
  2148. // This function is called once (via PostMessage) and will loop until the
  2149. // synchronous enumeration is complete. It repeatedly requests batches of
  2150. // objects (without a timeout) and adds them to the list.
  2151. void CQueryResultDlg::ProcessEnum()
  2152. {
  2153. ULONG uIdx;
  2154. IWbemClassObject** aObjects = new IWbemClassObject*[m_nBatch];
  2155. for (uIdx = 0; uIdx < m_nBatch; uIdx++)
  2156. {
  2157. aObjects[uIdx] = NULL;
  2158. }
  2159. ULONG uRet = 0;
  2160. HRESULT hres;
  2161. // Do not use the timeout since the synchronous enumeration was
  2162. // started without WBEM_FLAG_RETURN_IMMEDIATELY.
  2163. while (SUCCEEDED(hres = m_pEnum->Next(WBEM_NO_WAIT, m_nBatch, aObjects, &uRet)))
  2164. {
  2165. for (uIdx = 0; uIdx < uRet; uIdx++)
  2166. {
  2167. if (aObjects[uIdx])
  2168. {
  2169. IWbemClassObject* pObj = aObjects[uIdx];
  2170. AddObject(pObj);
  2171. pObj->Release();
  2172. aObjects[uIdx] = NULL;
  2173. }
  2174. }
  2175. if (uRet)
  2176. SetNumBatchItems(uRet);
  2177. if (hres == WBEM_S_FALSE)
  2178. {
  2179. InvalidateRect(GetDlgItem(IDC_OBJECT_LIST), NULL, FALSE);
  2180. UpdateWindow(GetDlgItem(IDC_OBJECT_LIST));
  2181. break;
  2182. }
  2183. }
  2184. SetComplete(hres, NULL, NULL);
  2185. m_pEnum->Release();
  2186. m_pEnum = NULL;
  2187. delete [] aObjects;
  2188. }
  2189. // This function is called repeatedly (via PostMessage) until the
  2190. // semisync enumeration is complete. This function adds the objects
  2191. // to the list and requests another batch of objects with a timeout.
  2192. void CQueryResultDlg::ProcessEnumSemisync()
  2193. {
  2194. ULONG uIdx;
  2195. IWbemClassObject** aObjects = new IWbemClassObject*[m_nBatch];
  2196. for (uIdx = 0; uIdx < m_nBatch; uIdx++)
  2197. {
  2198. aObjects[uIdx] = NULL;
  2199. }
  2200. ULONG uRet = 0;
  2201. HRESULT hres = m_pEnum->Next(m_lTimeout, m_nBatch, aObjects, &uRet);
  2202. if (SUCCEEDED(hres))
  2203. {
  2204. for (uIdx = 0; uIdx < uRet; uIdx++)
  2205. {
  2206. if (aObjects[uIdx])
  2207. {
  2208. IWbemClassObject* pObj = aObjects[uIdx];
  2209. AddObject(pObj);
  2210. pObj->Release();
  2211. aObjects[uIdx] = NULL;
  2212. }
  2213. }
  2214. if (uRet)
  2215. {
  2216. SetNumBatchItems(uRet);
  2217. InvalidateRect(GetDlgItem(IDC_OBJECT_LIST), NULL, FALSE);
  2218. UpdateWindow(GetDlgItem(IDC_OBJECT_LIST));
  2219. }
  2220. }
  2221. if (hres == WBEM_S_NO_ERROR || hres == WBEM_S_TIMEDOUT)
  2222. {
  2223. PostUserMessage(m_hDlg, 0, 0); // continues processing of enumeration
  2224. }
  2225. else
  2226. {
  2227. // Stop if an error or if WBEM_S_FALSE is returned.
  2228. // WBEM_S_FALSE indicates that the enumeration is complete.
  2229. SetComplete(hres, NULL, NULL);
  2230. m_pEnum->Release();
  2231. m_pEnum = NULL;
  2232. }
  2233. delete [] aObjects;
  2234. }
  2235. // This function is called repeatedly (via PostMessage) until the semisync
  2236. // enumeration using NextAsync is complete. This function simply requests
  2237. // another (or the first) batch of objects using the same sink.
  2238. void CQueryResultDlg::SemisyncNextAsync()
  2239. {
  2240. if (m_pEnum)
  2241. {
  2242. HRESULT hres = m_pEnum->NextAsync(m_nBatch, m_pHandler);
  2243. // If WBEM_S_FALSE or error then we are done with the enumerator.
  2244. // For NextAsync, SetStatus is called after every Indicate so that
  2245. // we must call SetComplete here.
  2246. if (hres != WBEM_S_NO_ERROR)
  2247. {
  2248. SetComplete(hres, NULL, NULL);
  2249. m_pEnum->Release();
  2250. m_pEnum = NULL;
  2251. }
  2252. }
  2253. }
  2254. void CQueryResultDlg::SetNotify(CNotSink* pNotify)
  2255. {
  2256. m_pHandler = pNotify;
  2257. if (m_pHandler)
  2258. m_pHandler->AddRef();
  2259. CUnsecWrap Wrap(m_pHandler);
  2260. m_pWrapper = (IWbemObjectSink*)Wrap;
  2261. if(m_pWrapper)
  2262. m_pWrapper->AddRef();
  2263. }
  2264. CQueryResultDlg::~CQueryResultDlg()
  2265. {
  2266. if (m_szTitle)
  2267. delete [] m_szTitle;
  2268. if(m_pHandler)
  2269. {
  2270. m_pHandler->ResetViewer();
  2271. if(!m_bComplete)
  2272. {
  2273. CHourGlass hg;
  2274. g_pNamespace->CancelAsyncCall(m_pWrapper);
  2275. }
  2276. m_pHandler->Release();
  2277. }
  2278. if(m_pWrapper)
  2279. m_pWrapper->Release();
  2280. if(m_pEnum)
  2281. m_pEnum->Release();
  2282. for(int i = 0; i < m_InternalArray.Size(); i++)
  2283. {
  2284. // Check the pointer
  2285. if ( NULL != m_InternalArray[i] )
  2286. {
  2287. ((IWbemClassObject*)m_InternalArray[i])->Release();
  2288. }
  2289. }
  2290. }
  2291. void CQueryResultDlg::SetTitle(char* szTitle)
  2292. {
  2293. m_szTitle = new char[strlen(szTitle)+2];
  2294. strcpy(m_szTitle, szTitle);
  2295. unsigned char* pc = (unsigned char*)m_szTitle;
  2296. while(*pc)
  2297. {
  2298. if(*pc < 32) *pc = 32;
  2299. pc = _mbsinc(pc);
  2300. }
  2301. if(m_hDlg)
  2302. {
  2303. SetDlgItemText(IDC_REQUEST, m_szTitle);
  2304. }
  2305. }
  2306. void CQueryResultDlg::PostObject(IWbemClassObject* pObject)
  2307. {
  2308. if(m_hDlg)
  2309. {
  2310. pObject->AddRef();
  2311. PostUserMessage(m_hDlg, 1, LPARAM(pObject));
  2312. }
  2313. else
  2314. {
  2315. AddObject(pObject);
  2316. }
  2317. }
  2318. void CQueryResultDlg::AddObject(IWbemClassObject* pObject)
  2319. {
  2320. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  2321. if(pObject == NULL)
  2322. return;
  2323. pObject->AddRef();
  2324. m_InternalArray.Add(pObject);
  2325. WString wsText;
  2326. MakeListEntry(pObject, wsText);
  2327. long len = wsText.Length()*2+20;
  2328. char* szTemp = new char[len];
  2329. wcstombs(szTemp, LPWSTR(wsText), len);
  2330. SendMessage(hList, LB_ADDSTRING, 0, LPARAM(szTemp));
  2331. delete [] szTemp;
  2332. SetNumItems(SendMessage(hList, LB_GETCOUNT, 0, 0));
  2333. }
  2334. void CQueryResultDlg::MakeListEntry(IWbemClassObject* pObj, WString& ListEntry)
  2335. {
  2336. ListEntry = L"";
  2337. HRESULT hres;
  2338. if ( NULL != pObj )
  2339. {
  2340. VARIANT v;
  2341. VariantInit(&v);
  2342. pObj->Get(L"__GENUS", 0, &v, NULL, NULL);
  2343. if (V_I4(&v) == 1)
  2344. {
  2345. // Class def.
  2346. VariantClear(&v);
  2347. hres = pObj->Get(L"__CLASS", 0, &v, NULL, NULL);
  2348. if(SUCCEEDED(hres))
  2349. ListEntry += V_BSTR(&v);
  2350. else
  2351. ListEntry += L"Unknown class";
  2352. VariantClear(&v);
  2353. ListEntry += L"\t(";
  2354. hres = pObj->Get(L"__SUPERCLASS", 0, &v, NULL, NULL);
  2355. if (SUCCEEDED(hres) && V_VT(&v) == VT_BSTR)
  2356. ListEntry += V_BSTR(&v);
  2357. VariantClear(&v);
  2358. ListEntry += L')';
  2359. }
  2360. else
  2361. {
  2362. // Instance
  2363. VariantClear(&v);
  2364. hres = pObj->Get(L"__RELPATH", 0, &v, NULL, NULL);
  2365. if(FAILED(hres) || V_VT(&v) == VT_NULL)
  2366. {
  2367. hres = pObj->Get(L"__CLASS", 0, &v, NULL, NULL);
  2368. if(SUCCEEDED(hres))
  2369. ListEntry += V_BSTR(&v);
  2370. ListEntry += L"=<no key>";
  2371. }
  2372. else
  2373. {
  2374. ListEntry += V_BSTR(&v);
  2375. }
  2376. VariantClear(&v);
  2377. }
  2378. } // IF NULL != pObj
  2379. else
  2380. {
  2381. ListEntry = L"NULL Object";
  2382. }
  2383. }
  2384. void CQueryResultDlg::PostCount(long nCount)
  2385. {
  2386. if(m_hDlg)
  2387. {
  2388. PostUserMessage(m_hDlg, 3, LPARAM(nCount));
  2389. }
  2390. }
  2391. void CQueryResultDlg::RefreshItem(int nItem)
  2392. {
  2393. IWbemClassObject* pObj = (IWbemClassObject*)m_InternalArray.GetAt(nItem);
  2394. WString wsListEntry;
  2395. MakeListEntry(pObj, wsListEntry);
  2396. // Convert to ANSI (Win95 requires this)
  2397. char* szTemp = new char[wsListEntry.Length()*2+10];
  2398. wcstombs(szTemp, LPWSTR(wsListEntry), wsListEntry.Length()+1);
  2399. szTemp[wsListEntry.Length()] = '\0';
  2400. SendMessage(GetDlgItem(IDC_OBJECT_LIST), LB_DELETESTRING, nItem, 0);
  2401. SendMessage(GetDlgItem(IDC_OBJECT_LIST), LB_INSERTSTRING, nItem,
  2402. LPARAM(szTemp));
  2403. delete [] szTemp;
  2404. }
  2405. void CQueryResultDlg::RunDetached(CRefCountable* pOwner)
  2406. {
  2407. SetDeleteOnClose();
  2408. SetOwner(pOwner);
  2409. Create(FALSE);
  2410. if(!Initialize())
  2411. {
  2412. PostMessage(m_hDlg, WM_CLOSE, 0, 0);
  2413. }
  2414. }
  2415. void CQueryResultDlg::PostComplete(long lParam, BSTR strParam,
  2416. IWbemClassObject* pObjParam)
  2417. {
  2418. if(m_hDlg)
  2419. {
  2420. if (m_lSync & SEMISYNC)
  2421. {
  2422. // For semisync using NextAsync, SetStatus indicates the batch is
  2423. // complete so we continue with the sink and another NextAsync call.
  2424. PostUserMessage(m_hDlg, 0, 0);
  2425. }
  2426. else
  2427. {
  2428. // For async, the final SetStatus has been called so we are done
  2429. // with the sink.
  2430. CStatus* pStatus = new CStatus(lParam, strParam, pObjParam);
  2431. PostUserMessage(m_hDlg, 2, (LPARAM)pStatus);
  2432. }
  2433. }
  2434. else
  2435. {
  2436. SetComplete(lParam, strParam, pObjParam);
  2437. }
  2438. }
  2439. void CQueryResultDlg::SetComplete(HRESULT hres, BSTR strParam,
  2440. IWbemClassObject* pErrorObj)
  2441. {
  2442. if(m_bComplete)
  2443. return;
  2444. set_partial(hres==WBEM_S_PARTIAL_RESULTS);
  2445. m_bComplete = TRUE;
  2446. if(FAILED(hres))
  2447. {
  2448. FormatError(hres, m_hDlg, pErrorObj);
  2449. PostMessage(m_hDlg, WM_CLOSE, 0, 0);
  2450. return;
  2451. }
  2452. if(m_pHandler)
  2453. {
  2454. m_pHandler->Release();
  2455. m_pHandler = NULL;
  2456. }
  2457. if(m_hDlg)
  2458. {
  2459. char szBuffer[1024]="";
  2460. LoadString(GetModuleHandle(NULL), m_partial_result==true?IDS_PARTIALRESULTS:IDS_DONE, szBuffer, 1024);
  2461. SetDlgItemText(IDC_STATUS, szBuffer);
  2462. EnableWindow(GetDlgItem(IDC_DELETE), CanDelete());
  2463. EnableWindow(GetDlgItem(IDC_ADD), CanAdd());
  2464. if (m_bSort)
  2465. {
  2466. qsort((void*)m_InternalArray.GetArrayPtr(), m_InternalArray.Size(), sizeof(void*), CmpFunc);
  2467. }
  2468. }
  2469. }
  2470. BOOL CQueryResultDlg::OnInitDialog()
  2471. {
  2472. CenterOnParent();
  2473. static LONG Tabs[] = { 80, 120, 170 };
  2474. int TabCount = 3;
  2475. SendMessage(GetDlgItem(IDC_OBJECT_LIST), LB_SETTABSTOPS,
  2476. (WPARAM) TabCount, (LPARAM) Tabs);
  2477. SendMessage(GetDlgItem(IDC_OBJECT_LIST), LB_SETHORIZONTALEXTENT,
  2478. 10000, 0);
  2479. IWbemClassObject *pObj;
  2480. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  2481. WString wsListEntry;
  2482. SetDlgItemText(IDC_REQUEST, m_szTitle);
  2483. if(m_bComplete)
  2484. {
  2485. char szBuffer[1024];
  2486. LoadString(GetModuleHandle(NULL), IDS_DONE, szBuffer, 1024);
  2487. SetDlgItemText(IDC_STATUS, szBuffer);
  2488. }
  2489. else
  2490. {
  2491. char szBuffer[1024];
  2492. LoadString(GetModuleHandle(NULL), IDS_IN_PROGRESS, szBuffer, 1024);
  2493. SetDlgItemText(IDC_STATUS, szBuffer);
  2494. }
  2495. EnableWindow(GetDlgItem(IDC_ADD), CanAdd());
  2496. EnableWindow(GetDlgItem(IDC_DELETE), CanDelete());
  2497. for(int i = 0; i < m_InternalArray.Size(); i++)
  2498. {
  2499. pObj = (IWbemClassObject*)m_InternalArray.GetAt(i);
  2500. MakeListEntry(pObj, wsListEntry);
  2501. // Convert to ANSI (Win95 requires this)
  2502. char* szTemp = new char[wsListEntry.Length()*2+10];
  2503. wcstombs(szTemp, LPWSTR(wsListEntry), wsListEntry.Length()+1);
  2504. SendMessage(hList, LB_ADDSTRING, 0, LPARAM(szTemp));
  2505. delete [] szTemp;
  2506. }
  2507. if(m_pEnum)
  2508. {
  2509. PostUserMessage(m_hDlg, 0, 0);
  2510. }
  2511. SetNumItems(m_InternalArray.Size());
  2512. if(m_bReadOnly)
  2513. {
  2514. EnableWindow(GetDlgItem(IDC_DELETE), FALSE);
  2515. EnableWindow(GetDlgItem(IDC_ADD), FALSE);
  2516. }
  2517. return TRUE;
  2518. }
  2519. // Total number of items
  2520. void CQueryResultDlg::SetNumItems(LRESULT nNum)
  2521. {
  2522. char szBuffer[512];
  2523. char szFormat[500];
  2524. LoadString(GetModuleHandle(NULL), IDS_NUM_OBJECTS_FORMAT, szFormat, 500);
  2525. sprintf(szBuffer, szFormat, nNum);
  2526. SetWindowText(GetDlgItem(IDC_NUM_OBJECTS), szBuffer);
  2527. }
  2528. // Number returned in most recent sink Indicate or enum Next.
  2529. void CQueryResultDlg::SetNumBatchItems(ULONG nNum)
  2530. {
  2531. // Show only maximum returned batch size.
  2532. if (nNum > m_nReturnedMax)
  2533. {
  2534. char szBuffer[512];
  2535. char szFormat[500];
  2536. LoadString(GetModuleHandle(NULL), IDS_NUM_BATCH_FORMAT, szFormat, 500);
  2537. sprintf(szBuffer, szFormat, nNum);
  2538. SetWindowText(GetDlgItem(IDC_NUM_BATCH), szBuffer);
  2539. m_nReturnedMax = nNum;
  2540. }
  2541. }
  2542. BOOL CQueryResultDlg::ViewListElement(LRESULT nSel)
  2543. {
  2544. IWbemClassObject *pObj = 0;
  2545. pObj = (IWbemClassObject*)m_InternalArray.GetAt((DWORD)nSel);
  2546. if ( NULL != pObj )
  2547. {
  2548. CObjectEditor* ped = new CObjectEditor(m_hDlg, m_lGenFlags,
  2549. m_bReadOnly ? CObjectEditor::readonly : CObjectEditor::readwrite,
  2550. m_lSync, pObj, m_lTimeout, m_nBatch);
  2551. ped->RunDetached(m_pOwner);
  2552. }
  2553. return TRUE;
  2554. }
  2555. BOOL CQueryResultDlg::OnDoubleClick(int nID)
  2556. {
  2557. if(nID != IDC_OBJECT_LIST) return FALSE;
  2558. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  2559. LRESULT nSel = SendMessage(hList, LB_GETCURSEL, 0, 0);
  2560. if (nSel == LB_ERR)
  2561. return TRUE;
  2562. ViewListElement(nSel);
  2563. SendMessage(hList, LB_SETCURSEL, nSel, 0);
  2564. return TRUE;
  2565. }
  2566. BOOL CQueryResultDlg::OnUser(WPARAM wParam, LPARAM lParam)
  2567. {
  2568. if(wParam == 0)
  2569. {
  2570. if (m_lSync & SEMISYNC)
  2571. {
  2572. if (m_lSync & USE_NEXTASYNC)
  2573. SemisyncNextAsync();
  2574. else
  2575. ProcessEnumSemisync();
  2576. }
  2577. else // synchronous
  2578. {
  2579. ProcessEnum();
  2580. }
  2581. return TRUE;
  2582. }
  2583. else if(wParam == 1)
  2584. {
  2585. IWbemClassObject* pObj = (IWbemClassObject*)lParam;
  2586. AddObject(pObj);
  2587. pObj->Release();
  2588. return TRUE;
  2589. }
  2590. else if(wParam == 2)
  2591. {
  2592. CStatus* pStatus = (CStatus*)lParam;
  2593. SetComplete(pStatus->m_hres, pStatus->m_str, pStatus->m_pObj);
  2594. delete pStatus;
  2595. return TRUE;
  2596. }
  2597. else if(wParam == 3)
  2598. {
  2599. SetNumBatchItems(ULONG(lParam));
  2600. return TRUE;
  2601. }
  2602. else return FALSE;
  2603. }
  2604. BOOL CQueryResultDlg::OnCommand(WORD wCode, WORD nID)
  2605. {
  2606. if(wCode == 0xFFFF && nID == 0xFFFF)
  2607. {
  2608. if (m_lSync & SEMISYNC)
  2609. {
  2610. if (m_lSync & USE_NEXTASYNC)
  2611. SemisyncNextAsync();
  2612. else
  2613. ProcessEnumSemisync();
  2614. }
  2615. else // synchronous
  2616. {
  2617. ProcessEnum();
  2618. }
  2619. return TRUE;
  2620. }
  2621. else if(nID == IDC_DELETE)
  2622. {
  2623. OnDelete();
  2624. return TRUE;
  2625. }
  2626. else if(nID == IDC_ADD)
  2627. {
  2628. OnAdd();
  2629. return TRUE;
  2630. }
  2631. else if(nID == IDC_COPY_SEL)
  2632. {
  2633. OnCopy();
  2634. return TRUE;
  2635. }
  2636. return FALSE;
  2637. }
  2638. void CQueryResultDlg::OnDelete()
  2639. {
  2640. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  2641. LRESULT nSel = SendMessage(hList, LB_GETCURSEL, 0, 0);
  2642. if (nSel == LB_ERR)
  2643. return;
  2644. if(DeleteListElement(nSel))
  2645. {
  2646. SendMessage(hList, LB_DELETESTRING, nSel, 0);
  2647. SetNumItems(SendMessage(hList, LB_GETCOUNT, 0, 0));
  2648. IWbemClassObject* pObj = (IWbemClassObject*)m_InternalArray.GetAt((DWORD)nSel);
  2649. m_InternalArray.RemoveAt((DWORD)nSel);
  2650. // Verify the pointer
  2651. if ( NULL != pObj )
  2652. {
  2653. pObj->Release();
  2654. }
  2655. }
  2656. }
  2657. BOOL CQueryResultDlg::DeleteListElement(LRESULT nSel)
  2658. {
  2659. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  2660. IWbemClassObject *pObj = 0;
  2661. pObj = (IWbemClassObject*)m_InternalArray.GetAt((DWORD)nSel);
  2662. VARIANT vPath;
  2663. VariantInit(&vPath);
  2664. pObj->Get(L"__RELPATH", 0, &vPath, NULL, NULL);
  2665. VARIANT vGenus;
  2666. VariantInit(&vGenus);
  2667. pObj->Get(L"__GENUS", 0, &vGenus, NULL, NULL);
  2668. HRESULT hres;
  2669. if(V_I4(&vGenus) == WBEM_GENUS_CLASS)
  2670. {
  2671. CHourGlass hg;
  2672. hres = g_pNamespace->DeleteClass(V_BSTR(&vPath), m_lGenFlags, g_Context, NULL);
  2673. }
  2674. else
  2675. {
  2676. CHourGlass hg;
  2677. hres = g_pNamespace->DeleteInstance(V_BSTR(&vPath), m_lGenFlags, g_Context, NULL);
  2678. }
  2679. if(FAILED(hres))
  2680. {
  2681. FormatError(hres, m_hDlg);
  2682. return FALSE;
  2683. }
  2684. return TRUE;
  2685. }
  2686. void CQueryResultDlg::OnAdd()
  2687. {
  2688. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  2689. IWbemClassObject* pNewObj;
  2690. pNewObj = AddNewElement();
  2691. if(pNewObj)
  2692. {
  2693. // Search for the object with the same PATH
  2694. // ========================================
  2695. VARIANT vNewPath;
  2696. HRESULT hres = pNewObj->Get(L"__RELPATH", 0, &vNewPath, NULL, NULL);
  2697. if(SUCCEEDED(hres) && V_VT(&vNewPath) == VT_BSTR)
  2698. {
  2699. for(int i = 0; i < m_InternalArray.Size(); i++)
  2700. {
  2701. IWbemClassObject* pThis = (IWbemClassObject*)m_InternalArray[i];
  2702. // Check for a NULL pointer
  2703. if ( NULL != pThis )
  2704. {
  2705. VARIANT vThisPath;
  2706. hres = pThis->Get(L"__RELPATH", 0, &vThisPath, NULL, NULL);
  2707. if(SUCCEEDED(hres) && V_VT(&vThisPath) == VT_BSTR &&
  2708. !_wcsicmp(V_BSTR(&vThisPath), V_BSTR(&vNewPath)))
  2709. {
  2710. // Found a duplicate
  2711. // =================
  2712. pThis->Release();
  2713. m_InternalArray.SetAt(i, pNewObj);
  2714. RefreshItem(i);
  2715. return;
  2716. }
  2717. }
  2718. }
  2719. }
  2720. AddObject(pNewObj);
  2721. pNewObj->Release();
  2722. if (m_bSort)
  2723. {
  2724. qsort((void*)m_InternalArray.GetArrayPtr(), m_InternalArray.Size(), sizeof(void*), CmpFunc);
  2725. }
  2726. }
  2727. }
  2728. // TODO: WM_COPY doesn't work for listbox so we need to code
  2729. // OpenClipboard & SetClipboardData. Also need way to copy all
  2730. // items to clipboard. Buttons changed to invisible for now.
  2731. void CQueryResultDlg::OnCopy()
  2732. {
  2733. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  2734. SendMessage(hList, WM_COPY, 0, 0);
  2735. }
  2736. IWbemClassObject* CQueryResultDlg::AddNewElement()
  2737. {
  2738. return NULL;
  2739. }
  2740. ////////////////////////////
  2741. char *CQueryDlg::m_szLastQueryType = NULL;
  2742. char *CQueryDlg::m_szLastQuery = NULL;
  2743. BOOL CQueryDlg::OnInitDialog()
  2744. {
  2745. AddStringToCombo(IDC_QUERY_TYPE, "WQL");
  2746. // Initialize Query Type
  2747. if(NULL == m_szLastQueryType)
  2748. {
  2749. m_szLastQueryType = new char[8];
  2750. strcpy(m_szLastQueryType, "WQL");
  2751. }
  2752. // Initialize Query String
  2753. if (NULL == m_szLastQuery)
  2754. {
  2755. m_szLastQuery = new char[1];
  2756. *m_szLastQuery = 0;
  2757. }
  2758. LRESULT uRes = SendMessage(GetDlgItem(IDC_QUERY_TYPE), CB_SELECTSTRING,
  2759. WPARAM(-1), LPARAM(m_szLastQueryType));
  2760. SetDlgItemText(IDC_QUERY_STRING, m_szLastQuery);
  2761. SendMessage(GetDlgItem(IDC_QUERY_STRING), EM_SETSEL, 0, -1);
  2762. EnableWindow(GetDlgItem(IDC_PROTOTYPE), NULL != m_plQryFlags);
  2763. return TRUE;
  2764. }
  2765. BOOL CQueryDlg::Verify()
  2766. {
  2767. UINT uRes = 0;
  2768. UINT uStringSize = 0;
  2769. // Query String Processing
  2770. if (*m_pwszQueryString)
  2771. delete [] *m_pwszQueryString;
  2772. // Determine the required size of the buffer
  2773. uRes = GetDlgItemTextX(IDC_QUERY_STRING, *m_pwszQueryString, uStringSize);
  2774. if(0 == uRes)
  2775. {
  2776. MessageBox(IDS_MUST_SPECIFY_QUERY, IDS_ERROR,
  2777. MB_OK | MB_ICONHAND);
  2778. return FALSE;
  2779. }
  2780. // Create the buffer
  2781. uStringSize = uRes + 10;
  2782. *m_pwszQueryString = new wchar_t[uStringSize];
  2783. // Fetch the text
  2784. uRes = GetDlgItemTextX(IDC_QUERY_STRING, *m_pwszQueryString, uStringSize);
  2785. if(wcslen(*m_pwszQueryString) == 0)
  2786. {
  2787. MessageBox(IDS_MUST_SPECIFY_QUERY, IDS_ERROR,
  2788. MB_OK | MB_ICONHAND);
  2789. return FALSE;
  2790. }
  2791. // Set up last query member
  2792. if (m_szLastQuery)
  2793. delete [] m_szLastQuery;
  2794. m_szLastQuery = new char[uStringSize];
  2795. wcstombs(m_szLastQuery, *m_pwszQueryString, uStringSize);
  2796. m_szLastQuery[uStringSize - 1] = '\0';
  2797. // Query Type Processing
  2798. if (*m_pwszQueryType)
  2799. delete [] *m_pwszQueryType;
  2800. uStringSize = 0;
  2801. // Determine the required size of the buffer
  2802. uRes = GetDlgItemTextX(IDC_QUERY_TYPE, *m_pwszQueryType, uStringSize);
  2803. if(0 == uRes)
  2804. {
  2805. MessageBox(IDS_MUST_SPECIFY_QUERY_LANGUAGE, IDS_ERROR,
  2806. MB_OK | MB_ICONHAND);
  2807. return FALSE;
  2808. }
  2809. // Create the buffer
  2810. uStringSize = uRes + 10;
  2811. *m_pwszQueryType = new wchar_t[uStringSize];
  2812. // Fetch the text
  2813. uRes = GetDlgItemTextX(IDC_QUERY_TYPE, *m_pwszQueryType, uStringSize);
  2814. if(wcslen(*m_pwszQueryType) == 0)
  2815. {
  2816. MessageBox(IDS_MUST_SPECIFY_QUERY_LANGUAGE, IDS_ERROR,
  2817. MB_OK | MB_ICONHAND);
  2818. return FALSE;
  2819. }
  2820. // Set up last query type
  2821. if (m_szLastQueryType)
  2822. delete [] m_szLastQueryType;
  2823. m_szLastQueryType = new char[uStringSize];
  2824. wcstombs(m_szLastQueryType, *m_pwszQueryType, uStringSize);
  2825. m_szLastQueryType[uStringSize - 1] = '\0';
  2826. if(m_plQryFlags)
  2827. {
  2828. if(GetCheck(IDC_PROTOTYPE) == BST_CHECKED)
  2829. *m_plQryFlags |= WBEM_FLAG_PROTOTYPE;
  2830. else
  2831. *m_plQryFlags &= ~WBEM_FLAG_PROTOTYPE;
  2832. }
  2833. return TRUE;
  2834. }
  2835. // ****************************************************************************
  2836. BOOL _ExecQuery(HWND hDlg, LONG lGenFlags, LONG lQryFlags, LPWSTR wszQuery, LPWSTR wszLanguage,
  2837. LONG lSync, CQueryResultDlg* pRes, char* pWindowTitle, LONG lTimeout, ULONG nBatch)
  2838. {
  2839. CBString bsQueryType(wszLanguage);
  2840. CBString bsQueryStr(wszQuery);
  2841. if(pWindowTitle)
  2842. pRes->SetTitle(pWindowTitle);
  2843. HRESULT hres;
  2844. // Asynchronous
  2845. if(lSync & ASYNC)
  2846. {
  2847. CNotSink* pHandler = new CNotSink(pRes); // constructor starts ref count at 1
  2848. pRes->SetNotify(pHandler);
  2849. {
  2850. CHourGlass hg;
  2851. hres = g_pNamespace->ExecQueryAsync(bsQueryType.GetString(), bsQueryStr.GetString(),
  2852. lGenFlags | lQryFlags | WBEM_FLAG_SEND_STATUS,
  2853. g_Context, pRes->GetWrapper());
  2854. }
  2855. pHandler->Release();
  2856. if (FAILED(hres))
  2857. {
  2858. FormatError(hres, hDlg);
  2859. return FALSE;
  2860. }
  2861. }
  2862. // Semisynchronous
  2863. else if (lSync & SEMISYNC)
  2864. {
  2865. IEnumWbemClassObject* pEnum = NULL;
  2866. {
  2867. CHourGlass hg;
  2868. hres = g_pNamespace->ExecQuery(bsQueryType.GetString(), bsQueryStr.GetString(),
  2869. lGenFlags | lQryFlags | WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  2870. g_Context, &pEnum);
  2871. }
  2872. if (FAILED(hres))
  2873. {
  2874. FormatError(hres, hDlg);
  2875. return FALSE;
  2876. }
  2877. // Create sink for use in NextAsync calls.
  2878. if (lSync & USE_NEXTASYNC)
  2879. {
  2880. CNotSink* pHandler = new CNotSink(pRes); // contructor starts ref count at 1
  2881. pRes->SetNotify(pHandler);
  2882. pHandler->Release();
  2883. }
  2884. SetInterfaceSecurityEx(pEnum, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  2885. pRes->SetEnum(pEnum);
  2886. pEnum->Release();
  2887. }
  2888. // Synchronous
  2889. else
  2890. {
  2891. IEnumWbemClassObject* pEnum = NULL;
  2892. {
  2893. CHourGlass hg;
  2894. hres = g_pNamespace->ExecQuery(bsQueryType.GetString(), bsQueryStr.GetString(),
  2895. lGenFlags | lQryFlags | WBEM_FLAG_FORWARD_ONLY,
  2896. g_Context, &pEnum);
  2897. }
  2898. if (FAILED(hres))
  2899. {
  2900. FormatError(hres, hDlg);
  2901. return FALSE;
  2902. }
  2903. SetInterfaceSecurityEx(pEnum, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  2904. pRes->SetEnum(pEnum,hres);
  2905. pEnum->Release();
  2906. }
  2907. return TRUE;
  2908. }
  2909. void CMainDlg::ExecQuery()
  2910. {
  2911. wchar_t *pQueryStr = 0;
  2912. wchar_t *pQueryType = 0;
  2913. LONG lQryFlags = 0;
  2914. CQueryDlg QD(m_hDlg, &lQryFlags, &pQueryStr, &pQueryType);
  2915. INT_PTR nRes = QD.Run();
  2916. if ((nRes == IDCANCEL) || (nRes == 0) || (!pQueryStr) || (!pQueryType))
  2917. return;
  2918. // If here, we are ready to execute the query.
  2919. // ===========================================
  2920. CQueryResultDlg* pResDlg = new CQueryResultDlg(m_hDlg, m_lGenFlags, lQryFlags);
  2921. // Pass on invocation method (sync, async..) and related settings for this
  2922. // query and by any further operations (editing/deleting/etc. of an instance).
  2923. pResDlg->SetCallMethod(m_lSync);
  2924. pResDlg->SetTimeout(Timeout());
  2925. pResDlg->SetBatchCount(BatchCount());
  2926. wchar_t szDest[512];
  2927. szDest[511] = L'\0';
  2928. char szTitle[1000];
  2929. sprintf(szTitle, "%S: %S", pQueryType, wcsncpy(szDest, pQueryStr, 511));
  2930. if (_ExecQuery(m_hDlg, m_lGenFlags, lQryFlags, pQueryStr, pQueryType, m_lSync,
  2931. pResDlg, szTitle, Timeout(), BatchCount()))
  2932. {
  2933. pResDlg->RunDetached(this);
  2934. }
  2935. else
  2936. {
  2937. delete pResDlg;
  2938. }
  2939. delete [] pQueryStr;
  2940. delete [] pQueryType;
  2941. }
  2942. // ****************************************************************************
  2943. BOOL _ExecNotificationQuery(HWND hDlg, LONG lGenFlags, LONG lQryFlags, LPWSTR wszQuery,
  2944. LPWSTR wszLanguage, LONG lSync, CQueryResultDlg* pRes,
  2945. char* pWindowTitle, LONG lTimeout, ULONG nBatch)
  2946. {
  2947. CBString bsQueryType(wszLanguage);
  2948. CBString bsQueryStr(wszQuery);
  2949. if(pWindowTitle)
  2950. pRes->SetTitle(pWindowTitle);
  2951. HRESULT hres;
  2952. // Asynchronous
  2953. if(lSync & ASYNC)
  2954. {
  2955. CNotSink* pHandler = new CNotSink(pRes); // constructor starts ref count at 1
  2956. pRes->SetNotify(pHandler);
  2957. {
  2958. CHourGlass hg;
  2959. hres = g_pNamespace->ExecNotificationQueryAsync(bsQueryType.GetString(),
  2960. bsQueryStr.GetString(),
  2961. lGenFlags | lQryFlags,
  2962. g_Context, pRes->GetWrapper());
  2963. }
  2964. pHandler->Release();
  2965. if (FAILED(hres))
  2966. {
  2967. FormatError(hres, hDlg);
  2968. return FALSE;
  2969. }
  2970. }
  2971. // Semisynchronous
  2972. else if (lSync & SEMISYNC)
  2973. {
  2974. IEnumWbemClassObject* pEnum = NULL;
  2975. {
  2976. CHourGlass hg;
  2977. hres = g_pNamespace->ExecNotificationQuery(bsQueryType.GetString(), bsQueryStr.GetString(),
  2978. lGenFlags | lQryFlags | WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  2979. g_Context, &pEnum);
  2980. }
  2981. if (FAILED(hres))
  2982. {
  2983. FormatError(hres, hDlg);
  2984. return FALSE;
  2985. }
  2986. // Create sink for use in NextAsync calls.
  2987. if (lSync & USE_NEXTASYNC)
  2988. {
  2989. CNotSink* pHandler = new CNotSink(pRes); // contructor starts ref count at 1
  2990. pRes->SetNotify(pHandler);
  2991. pHandler->Release();
  2992. }
  2993. SetInterfaceSecurityEx(pEnum, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  2994. pRes->SetEnum(pEnum);
  2995. pEnum->Release();
  2996. }
  2997. // Synchronous
  2998. else
  2999. {
  3000. // Note, this call should fail with WBEM_E_INVALID_PARAMETER because synchronous invocation
  3001. // isn't supported for ExecNotificationQuery due to the continuous nature of events.
  3002. IEnumWbemClassObject* pEnum = NULL;
  3003. {
  3004. CHourGlass hg;
  3005. hres = g_pNamespace->ExecNotificationQuery(bsQueryType.GetString(), bsQueryStr.GetString(),
  3006. lGenFlags | lQryFlags | WBEM_FLAG_FORWARD_ONLY,
  3007. g_Context, &pEnum);
  3008. }
  3009. if (FAILED(hres))
  3010. {
  3011. FormatError(hres, hDlg);
  3012. return FALSE;
  3013. }
  3014. SetInterfaceSecurityEx(pEnum, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  3015. pRes->SetEnum(pEnum,hres);
  3016. pEnum->Release();
  3017. }
  3018. return TRUE;
  3019. }
  3020. void CMainDlg::ExecNotificationQuery()
  3021. {
  3022. // Does not allow multiple semisync notification query dlg
  3023. // ========================================================
  3024. if ( m_fNotificationQueryResultDlg && SEMISYNC == m_lSync )
  3025. {
  3026. MessageBox(IDS_MULTIPLE_SEMISYNC_NOTIFICATION_QUERY_DLG_MSG, IDS_ERROR, MB_OK | MB_ICONHAND);
  3027. return;
  3028. }
  3029. wchar_t *pQueryStr = 0;
  3030. wchar_t *pQueryType = 0;
  3031. LONG lQryFlags = 0;
  3032. CQueryDlg QD(m_hDlg, &lQryFlags, &pQueryStr, &pQueryType);
  3033. INT_PTR nRes = QD.Run();
  3034. if ((nRes == IDCANCEL) || (nRes == 0) || (!pQueryStr) || (!pQueryType))
  3035. return;
  3036. // If here, we are ready to execute the query.
  3037. // ===========================================
  3038. CNotificationQueryResultDlg* pResDlg = new CNotificationQueryResultDlg(m_hDlg, m_lGenFlags, lQryFlags, m_lSync );
  3039. // Pass on invocation method (sync, async..) and related settings for this
  3040. // query and by any further operations (editing/deleting/etc. of an instance).
  3041. pResDlg->SetCallMethod(m_lSync);
  3042. pResDlg->SetTimeout(Timeout());
  3043. pResDlg->SetBatchCount(BatchCount());
  3044. wchar_t szDest[512];
  3045. szDest[511] = L'\0';
  3046. char szTitle[1000];
  3047. sprintf(szTitle, "%S: %S", pQueryType, wcsncpy(szDest, pQueryStr, 511));
  3048. if(lQryFlags & WBEM_FLAG_PROTOTYPE)
  3049. lQryFlags = (lQryFlags & ~WBEM_FLAG_PROTOTYPE) | WBEM_FLAG_MONITOR;
  3050. if (_ExecNotificationQuery(m_hDlg, m_lGenFlags, lQryFlags, pQueryStr, pQueryType,
  3051. m_lSync, pResDlg, szTitle, Timeout(), BatchCount()))
  3052. {
  3053. pResDlg->RunDetached(this);
  3054. }
  3055. else
  3056. {
  3057. delete pResDlg;
  3058. }
  3059. delete [] pQueryStr;
  3060. delete [] pQueryType;
  3061. }
  3062. // ****************************************************************************
  3063. void CMainDlg::ExecMethod()
  3064. {
  3065. CMethodDlg * pDlg = new CMethodDlg(m_hDlg, m_lGenFlags, m_lSync, Timeout());
  3066. pDlg->RunDetached(this);
  3067. }
  3068. ////////////////////////////////////////////////////////////////////////
  3069. //
  3070. class CSuperclassInfoDlg : public CWbemDialog
  3071. {
  3072. protected:
  3073. LPWSTR m_wszSuperclass;
  3074. long m_lMaxLen;
  3075. LONG* m_plQryFlags;
  3076. BOOL m_bIsInstance;
  3077. public:
  3078. CSuperclassInfoDlg(HWND hParent, LPWSTR wszClass, long lMaxLen, LONG* plQryFlags)
  3079. : m_wszSuperclass(wszClass), m_lMaxLen(lMaxLen), m_plQryFlags(plQryFlags),
  3080. m_bIsInstance(FALSE), CWbemDialog(IDD_PARENTINFO, hParent)
  3081. {}
  3082. void SetIsInstance() {m_bIsInstance = TRUE;}
  3083. protected:
  3084. BOOL OnInitDialog();
  3085. BOOL Verify();
  3086. };
  3087. class CRenameDlg : public CWbemDialog
  3088. {
  3089. protected:
  3090. LPWSTR m_wszOld;
  3091. long m_lMaxOldLen;
  3092. LPWSTR m_wszNew;
  3093. long m_lMaxNewLen;
  3094. public:
  3095. CRenameDlg(HWND hParent, LPWSTR wszOld, long lMaxOldLen, LPWSTR wszNew, long lMaxNewLen)
  3096. : m_wszOld(wszOld), m_lMaxOldLen(lMaxOldLen), m_wszNew(wszNew), m_lMaxNewLen(lMaxNewLen),
  3097. CWbemDialog(IDD_RENAMEDIALOG, hParent)
  3098. {}
  3099. protected:
  3100. BOOL OnInitDialog(){return TRUE;};
  3101. BOOL Verify();
  3102. };
  3103. BOOL CRenameDlg::Verify()
  3104. {
  3105. *m_wszOld = 0;
  3106. GetDlgItemTextX(IDC_EDITOLD, m_wszOld, m_lMaxOldLen);
  3107. *m_wszNew = 0;
  3108. GetDlgItemTextX(IDC_EDITNEW, m_wszNew, m_lMaxNewLen);
  3109. return TRUE;
  3110. }
  3111. BOOL CSuperclassInfoDlg::OnInitDialog()
  3112. {
  3113. char szFormat[1024];
  3114. SetCheck(IDC_IMMEDIATE_SUBCLASSES, BST_CHECKED);
  3115. if(m_bIsInstance)
  3116. {
  3117. LoadString(GetModuleHandle(NULL), IDS_CLASS_INFO, szFormat, 1024);
  3118. SetWindowText(m_hDlg, szFormat);
  3119. }
  3120. return TRUE;
  3121. }
  3122. BOOL CSuperclassInfoDlg::Verify()
  3123. {
  3124. if (GetCheck(IDC_IMMEDIATE_SUBCLASSES) == BST_CHECKED)
  3125. *m_plQryFlags = WBEM_FLAG_SHALLOW;
  3126. else
  3127. *m_plQryFlags = WBEM_FLAG_DEEP;
  3128. *m_wszSuperclass = 0;
  3129. GetDlgItemTextX(IDC_SUPERCLASS, m_wszSuperclass, m_lMaxLen);
  3130. return TRUE;
  3131. }
  3132. BOOL CCreateInstanceDlg::OnInitDialog()
  3133. {
  3134. char szFormat[1024];
  3135. SetCheck(IDC_USE_CLASSOBJ, BST_CHECKED);
  3136. return TRUE;
  3137. }
  3138. BOOL CCreateInstanceDlg::Verify()
  3139. {
  3140. if (GetCheck(IDC_USE_CLASSOBJ) == BST_CHECKED)
  3141. *m_plQryFlags = 1;
  3142. else
  3143. *m_plQryFlags = 0;
  3144. *m_wszClass = 0;
  3145. GetDlgItemTextX(IDC_SUPERCLASS, m_wszClass, m_lMaxLen);
  3146. return TRUE;
  3147. }
  3148. INT_PTR GetSuperclassInfo(HWND hDlg, LPWSTR pClass, LONG lMaxBuf, LONG* plQryFlags)
  3149. {
  3150. CSuperclassInfoDlg InfoDlg(hDlg, pClass, lMaxBuf, plQryFlags);
  3151. return InfoDlg.Run();
  3152. }
  3153. INT_PTR GetClassInfo(HWND hDlg, LPWSTR pClass, LONG lMaxBuf, LONG* plQryFlags)
  3154. {
  3155. CSuperclassInfoDlg InfoDlg(hDlg, pClass, lMaxBuf, plQryFlags);
  3156. InfoDlg.SetIsInstance();
  3157. return InfoDlg.Run();
  3158. }
  3159. class CContextValueDlg : public CWbemDialog
  3160. {
  3161. protected:
  3162. BSTR* m_pstrName;
  3163. VARIANT* m_pvValue;
  3164. BOOL m_bNew;
  3165. public:
  3166. CContextValueDlg(HWND hParent, BSTR* pstrName, VARIANT* pvValue, BOOL bNew)
  3167. : CWbemDialog(IDD_CONTEXT_PROPERTY, hParent), m_pstrName(pstrName),
  3168. m_pvValue(pvValue), m_bNew(bNew)
  3169. {}
  3170. BOOL OnInitDialog();
  3171. BOOL Verify();
  3172. void Refresh();
  3173. };
  3174. BOOL CContextValueDlg::OnInitDialog()
  3175. {
  3176. // Populate the combo box with the valid prop types
  3177. // ================================================
  3178. AddStringToCombo(IDC_TYPE_LIST, "CIM_STRING");
  3179. AddStringToCombo(IDC_TYPE_LIST, "CIM_UINT8");
  3180. AddStringToCombo(IDC_TYPE_LIST, "CIM_SINT16");
  3181. AddStringToCombo(IDC_TYPE_LIST, "CIM_SINT32");
  3182. AddStringToCombo(IDC_TYPE_LIST, "CIM_REAL32");
  3183. AddStringToCombo(IDC_TYPE_LIST, "CIM_REAL64");
  3184. AddStringToCombo(IDC_TYPE_LIST, "CIM_BOOLEAN");
  3185. AddStringToCombo(IDC_TYPE_LIST, "CIM_OBJECT");
  3186. if(!m_bNew)
  3187. EnableWindow(GetDlgItem(IDC_PROPNAME), FALSE);
  3188. if(*m_pstrName)
  3189. {
  3190. SetDlgItemTextX(IDC_PROPNAME, *m_pstrName);
  3191. }
  3192. if(V_VT(m_pvValue) != VT_EMPTY)
  3193. {
  3194. LPSTR pTypeStr = TypeToString(V_VT(m_pvValue) & ~VT_ARRAY);
  3195. SendMessage(GetDlgItem(IDC_TYPE_LIST), CB_SELECTSTRING, WPARAM(-1),
  3196. LPARAM(pTypeStr));
  3197. if(V_VT(m_pvValue) & VT_ARRAY)
  3198. SetCheck(IDC_ARRAY, BST_CHECKED);
  3199. CVar v;
  3200. v.SetVariant(m_pvValue);
  3201. LPSTR szValue = ValueToString(&v);
  3202. SetDlgItemText(IDC_VALUE, szValue);
  3203. }
  3204. else
  3205. {
  3206. SendMessage(GetDlgItem(IDC_TYPE_LIST), CB_SELECTSTRING, WPARAM(-1),
  3207. LPARAM("VT_NULL"));
  3208. }
  3209. return TRUE;
  3210. }
  3211. BOOL CContextValueDlg::Verify()
  3212. {
  3213. WCHAR wszName[1024];
  3214. GetDlgItemTextX(IDC_PROPNAME, wszName, 1024);
  3215. if(*wszName == 0)
  3216. {
  3217. MessageBox(IDS_NO_PROPERTY_NAME, IDS_ERROR, MB_OK);
  3218. return FALSE;
  3219. }
  3220. StripTrailingWs(wszName);
  3221. LPSTR szType = GetCBCurSelString(IDC_TYPE_LIST);
  3222. int nType = StringToType(szType);
  3223. if(nType == VT_NULL)
  3224. {
  3225. MessageBox(IDS_INVALID_PROPERTY_TYPE, IDS_ERROR, MB_OK);
  3226. return FALSE;
  3227. }
  3228. if(GetCheck(IDC_ARRAY) == BST_CHECKED)
  3229. {
  3230. nType |= VT_ARRAY;
  3231. }
  3232. CHAR szValue[1024];
  3233. UINT uRes = GetDlgItemTextA(m_hDlg, IDC_VALUE, szValue, 1024);
  3234. CVar* pVal = StringToValue(szValue, nType);
  3235. if(pVal == NULL)
  3236. {
  3237. MessageBox(IDS_INVALID_PROPERTY_VALUE, IDS_ERROR, MB_OK);
  3238. return FALSE;
  3239. }
  3240. // All checked out. Store and succeed
  3241. // ==================================
  3242. SysFreeString(*m_pstrName);
  3243. *m_pstrName = SysAllocString(wszName);
  3244. VariantClear(m_pvValue);
  3245. pVal->FillVariant(m_pvValue);
  3246. return TRUE;
  3247. }
  3248. class CContextDlg : public CWbemDialog
  3249. {
  3250. CContext* m_pContext;
  3251. CContext m_Context;
  3252. public:
  3253. CContextDlg(HWND hParent, CContext* pContext)
  3254. : CWbemDialog(IDD_CONTEXT_EDITOR, hParent), m_pContext(pContext)
  3255. {}
  3256. protected:
  3257. BOOL OnInitDialog();
  3258. BOOL Verify();
  3259. BOOL OnCommand(WORD wCode, WORD wID);
  3260. BOOL OnDoubleClick(int nId);
  3261. void EnableControls(BOOL bEnable);
  3262. void Refresh();
  3263. void OnUse();
  3264. void OnAdd();
  3265. void OnDelete();
  3266. void OnEdit();
  3267. BSTR GetCurSelEntryName();
  3268. LPSTR MakeListEntry(LPCWSTR wszName, VARIANT& vValue);
  3269. };
  3270. CContext::CContext() : m_bNull(TRUE), m_pContext(NULL)
  3271. {
  3272. }
  3273. BOOL CContext::SetNullness(BOOL bNull)
  3274. {
  3275. m_bNull = bNull;
  3276. if(!m_bNull && m_pContext == NULL)
  3277. {
  3278. HRESULT hres = CoCreateInstance(CLSID_WbemContext, NULL,
  3279. CLSCTX_INPROC_SERVER, IID_IWbemContext, (void**)&m_pContext);
  3280. return SUCCEEDED(hres);
  3281. }
  3282. else return TRUE;
  3283. }
  3284. CContext::~CContext()
  3285. {
  3286. if(m_pContext)
  3287. m_pContext->Release();
  3288. }
  3289. void CContext::Clear()
  3290. {
  3291. if(m_pContext)
  3292. m_pContext->Release();
  3293. m_pContext = NULL;
  3294. m_bNull = TRUE;
  3295. }
  3296. INT_PTR CContext::Edit(HWND hParent)
  3297. {
  3298. CContextDlg Dlg(hParent, this);
  3299. return Dlg.Run();
  3300. }
  3301. CContext::operator IWbemContext*()
  3302. {
  3303. if(m_bNull)
  3304. return NULL;
  3305. else
  3306. return m_pContext;
  3307. }
  3308. void CContext::operator=(const CContext& Other)
  3309. {
  3310. m_bNull = Other.m_bNull;
  3311. if(m_pContext)
  3312. m_pContext->Release();
  3313. if(Other.m_pContext)
  3314. Other.m_pContext->Clone(&m_pContext);
  3315. else
  3316. m_pContext = NULL;
  3317. }
  3318. void CContextDlg::OnUse()
  3319. {
  3320. BOOL bEnable = (GetCheck(IDC_USE_CONTEXT) == BST_CHECKED);
  3321. EnableControls(bEnable);
  3322. m_Context.SetNullness(!bEnable);
  3323. }
  3324. BOOL CContextDlg::OnCommand(WORD wCode, WORD wID)
  3325. {
  3326. if(wID == IDC_USE_CONTEXT)
  3327. OnUse();
  3328. else if(wID == IDC_ADD_PROP)
  3329. OnAdd();
  3330. else if(wID == IDC_EDIT_PROP)
  3331. OnEdit();
  3332. else if(wID == IDC_DELETE_PROP)
  3333. OnDelete();
  3334. return TRUE;
  3335. }
  3336. BOOL CContextDlg::OnDoubleClick(int nID)
  3337. {
  3338. if(nID == IDC_VALUE_LIST)
  3339. OnEdit();
  3340. return TRUE;
  3341. }
  3342. void CContextDlg::OnAdd()
  3343. {
  3344. BSTR strName = NULL;
  3345. VARIANT vValue;
  3346. VariantInit(&vValue);
  3347. CContextValueDlg Dlg(m_hDlg, &strName, &vValue, TRUE);
  3348. INT_PTR nRes = Dlg.Run();
  3349. if(nRes == IDOK)
  3350. {
  3351. m_Context.SetNullness(FALSE);
  3352. m_Context->SetValue(strName, 0, &vValue);
  3353. Refresh();
  3354. }
  3355. SysFreeString(strName);
  3356. VariantClear(&vValue);
  3357. }
  3358. void CContextDlg::OnEdit()
  3359. {
  3360. BSTR strName = GetCurSelEntryName();
  3361. if(strName == NULL)
  3362. return;
  3363. VARIANT vValue;
  3364. VariantInit(&vValue);
  3365. m_Context->GetValue(strName, 0, &vValue);
  3366. CContextValueDlg Dlg(m_hDlg, &strName, &vValue, FALSE);
  3367. INT_PTR nRes = Dlg.Run();
  3368. if(nRes == IDOK)
  3369. {
  3370. m_Context->SetValue(strName, 0, &vValue);
  3371. Refresh();
  3372. }
  3373. SysFreeString(strName);
  3374. VariantClear(&vValue);
  3375. }
  3376. void CContextDlg::OnDelete()
  3377. {
  3378. BSTR strName = GetCurSelEntryName();
  3379. if(strName == NULL)
  3380. return;
  3381. m_Context->DeleteValue(strName, 0);
  3382. SysAllocString(strName);
  3383. Refresh();
  3384. }
  3385. LPSTR CContextDlg::MakeListEntry(LPCWSTR wszName, VARIANT& vValue)
  3386. {
  3387. CVar value(&vValue);
  3388. LPSTR TypeString = TypeToString(V_VT(&vValue));
  3389. LPSTR ValueString = ValueToString(&value);
  3390. char* sz = new char[strlen(TypeString) + strlen(ValueString) +
  3391. wcslen(wszName)*4 + 100];
  3392. sprintf(sz, "%S\t%s\t%s", wszName, TypeString, ValueString);
  3393. return sz;
  3394. }
  3395. BSTR CContextDlg::GetCurSelEntryName()
  3396. {
  3397. LPSTR szEntry = GetLBCurSelString(IDC_VALUE_LIST);
  3398. if(szEntry == NULL)
  3399. return NULL;
  3400. char* pc = strchr(szEntry, '\t');
  3401. if(pc) *pc = 0;
  3402. LPWSTR wszName = CreateUnicode(szEntry);
  3403. delete [] szEntry;
  3404. BSTR strName = SysAllocString(wszName);
  3405. delete [] wszName;
  3406. return strName;
  3407. }
  3408. void CContextDlg::Refresh()
  3409. {
  3410. SendMessage(GetDlgItem(IDC_VALUE_LIST), LB_RESETCONTENT, 0, 0);
  3411. IWbemContext* pContext = m_Context.GetStoredContext();
  3412. if(pContext)
  3413. {
  3414. pContext->BeginEnumeration(0);
  3415. BSTR strName;
  3416. VARIANT vValue;
  3417. VariantInit(&vValue);
  3418. while(pContext->Next(0, &strName, &vValue) == S_OK)
  3419. {
  3420. char* szEntry = MakeListEntry(strName, vValue);
  3421. AddStringToList(IDC_VALUE_LIST, szEntry);
  3422. delete [] szEntry;
  3423. VariantClear(&vValue);
  3424. }
  3425. pContext->EndEnumeration();
  3426. }
  3427. if(m_Context.IsNull())
  3428. {
  3429. SetCheck(IDC_USE_CONTEXT, BST_UNCHECKED);
  3430. EnableControls(FALSE);
  3431. }
  3432. else
  3433. {
  3434. SetCheck(IDC_USE_CONTEXT, BST_CHECKED);
  3435. }
  3436. }
  3437. BOOL CContextDlg::OnInitDialog()
  3438. {
  3439. m_Context = *m_pContext;
  3440. // Set tabs in the list box
  3441. // ========================
  3442. LONG Tabs[] = { 80, 120, 170 };
  3443. int TabCount = 3;
  3444. SendMessage(GetDlgItem(IDC_VALUE_LIST), LB_SETTABSTOPS,
  3445. (WPARAM) TabCount, (LPARAM) Tabs);
  3446. SendMessage(GetDlgItem(IDC_VALUE_LIST), LB_SETHORIZONTALEXTENT, 1000, 0);
  3447. Refresh();
  3448. return TRUE;
  3449. }
  3450. void CContextDlg::EnableControls(BOOL bEnable)
  3451. {
  3452. EnableWindow(GetDlgItem(IDC_VALUE_LIST), bEnable);
  3453. EnableWindow(GetDlgItem(IDC_ADD_PROP), bEnable);
  3454. EnableWindow(GetDlgItem(IDC_EDIT_PROP), bEnable);
  3455. EnableWindow(GetDlgItem(IDC_DELETE_PROP), bEnable);
  3456. }
  3457. CContextDlg::Verify()
  3458. {
  3459. *m_pContext = m_Context;
  3460. return TRUE;
  3461. }
  3462. CMainDlg::~CMainDlg()
  3463. {
  3464. PostQuitMessage(0);
  3465. }
  3466. BOOL CMainDlg::OnInitDialog()
  3467. {
  3468. CenterOnScreen(m_hDlg);
  3469. // if(!gbAdvanced)
  3470. // {
  3471. // ShowWindow(GetDlgItem(IDC_ENABLE_PRIVILEGES), SW_HIDE);
  3472. // }
  3473. ConnectButtons(FALSE);
  3474. SetDlgItemTextWC(m_hDlg, IDC_NAMESPACE, gNameSpace);
  3475. CheckRadioButton(m_hDlg, IDC_ASYNC, IDC_SEMISYNC,
  3476. (m_lSync & ASYNC) ? IDC_ASYNC : ((m_lSync & SEMISYNC) ? IDC_SEMISYNC : IDC_SYNC));
  3477. SetDlgItemText(IDC_TIMEOUT, "5000");
  3478. SetDlgItemText(IDC_BATCH, "10");
  3479. SetFocus(GetDlgItem(IDC_CONNECT));
  3480. return FALSE;
  3481. }
  3482. BOOL CMainDlg::OnCommand(WORD wNotifyCode, WORD wID)
  3483. {
  3484. switch (wID)
  3485. {
  3486. case IDC_MENU_EXIT:
  3487. case IDC_CONNECT:
  3488. Connect(FALSE);
  3489. return TRUE;
  3490. case IDC_WBEMHELP:
  3491. OpenHelp();
  3492. return TRUE;
  3493. case IDC_BIND:
  3494. Connect(TRUE);
  3495. return TRUE;
  3496. case IDC_CREATE_CLASS:
  3497. CreateClass();
  3498. return TRUE;
  3499. case IDC_GET_CLASSES:
  3500. GetClasses();
  3501. return TRUE;
  3502. case IDC_EDIT_CLASS:
  3503. EditClass();
  3504. return TRUE;
  3505. case IDC_DELETE_CLASS:
  3506. DeleteClass();
  3507. return TRUE;
  3508. case IDC_CREATE_INSTANCE:
  3509. CreateInstance();
  3510. return TRUE;
  3511. case IDC_GET_INSTANCES:
  3512. GetInstances();
  3513. return TRUE;
  3514. case IDC_EDIT_INSTANCE:
  3515. EditInstance();
  3516. return TRUE;
  3517. case IDC_EXEC_METHOD:
  3518. ExecMethod();
  3519. return TRUE;
  3520. case IDC_DELETE_INSTANCE:
  3521. DeleteInstance();
  3522. return TRUE;
  3523. case IDC_QUERY:
  3524. ExecQuery();
  3525. return TRUE;
  3526. case IDC_NOTIFICATION_QUERY:
  3527. ExecNotificationQuery();
  3528. return TRUE;
  3529. case IDC_OPEN_NS:
  3530. OpenNs();
  3531. return TRUE;
  3532. case IDC_REFRESH:
  3533. CreateRefresher();
  3534. return TRUE;
  3535. case IDC_CONTEXT:
  3536. EditContext();
  3537. return TRUE;
  3538. case IDC_ENABLE_PRIVILEGES:
  3539. {
  3540. // From common\genutils.cpp
  3541. HRESULT hRes = EnableAllPrivileges(TOKEN_PROCESS);
  3542. BOOL bRes = SUCCEEDED(hRes);
  3543. if (bRes)
  3544. EnableWindow(GetDlgItem(IDC_ENABLE_PRIVILEGES), FALSE);
  3545. return bRes;
  3546. }
  3547. case IDC_USE_AMENDED:
  3548. if (GetCheck(IDC_USE_AMENDED) == BST_CHECKED)
  3549. m_lGenFlags |= WBEM_FLAG_USE_AMENDED_QUALIFIERS;
  3550. else
  3551. m_lGenFlags &= ~WBEM_FLAG_USE_AMENDED_QUALIFIERS;
  3552. return TRUE;
  3553. case IDC_DIRECT_READ:
  3554. if (GetCheck(IDC_DIRECT_READ) == BST_CHECKED)
  3555. m_lGenFlags |= WBEM_FLAG_DIRECT_READ;
  3556. else
  3557. m_lGenFlags &= ~WBEM_FLAG_DIRECT_READ;
  3558. return TRUE;
  3559. case IDC_USE_NEXTASYNC:
  3560. if (GetCheck(IDC_USE_NEXTASYNC) == BST_CHECKED)
  3561. m_lSync |= USE_NEXTASYNC;
  3562. else
  3563. m_lSync &= ~USE_NEXTASYNC;
  3564. EnableWindow(GetDlgItem(IDC_TIMEOUT), !(m_lSync & USE_NEXTASYNC));
  3565. return TRUE;
  3566. case IDC_ASYNC:
  3567. case IDC_SYNC:
  3568. case IDC_SEMISYNC:
  3569. // Can ignore USE_NEXTASYNC checkbox because we are changing to or from
  3570. // semisync which means this checkbox either was clear or is now cleared.
  3571. if (GetCheck(IDC_ASYNC) == BST_CHECKED)
  3572. {
  3573. m_lSync = ASYNC;
  3574. SetCheck(IDC_USE_NEXTASYNC, BST_UNCHECKED);
  3575. }
  3576. else if (GetCheck(IDC_SYNC) == BST_CHECKED)
  3577. {
  3578. m_lSync = SYNC;
  3579. SetCheck(IDC_USE_NEXTASYNC, BST_UNCHECKED);
  3580. }
  3581. else if (GetCheck(IDC_SEMISYNC) == BST_CHECKED)
  3582. {
  3583. m_lSync = SEMISYNC;
  3584. }
  3585. EnableWindow(GetDlgItem(IDC_BATCH), !(m_lSync & ASYNC));
  3586. EnableWindow(GetDlgItem(IDC_TIMEOUT), (m_lSync & SEMISYNC));
  3587. EnableWindow(GetDlgItem(IDC_USE_NEXTASYNC), (m_lSync & SEMISYNC));
  3588. return TRUE;
  3589. case IDC_OPEN:
  3590. Open();
  3591. return TRUE;
  3592. case IDC_ADDOBJECT:
  3593. AddObject();
  3594. return TRUE;
  3595. case IDC_DELETEOBJECT:
  3596. DeleteObject();
  3597. return TRUE;
  3598. case IDC_RENAMEOBJECT:
  3599. RenameObject();
  3600. return TRUE;
  3601. case IDC_GETOBJECTSECURITY:
  3602. GetObjectSecurity();
  3603. return TRUE;
  3604. case IDC_SETOBJECTSECURITY:
  3605. SetObjectSecurity();
  3606. return TRUE;
  3607. }
  3608. return TRUE;
  3609. }
  3610. LONG CMainDlg::Timeout()
  3611. {
  3612. wchar_t wszTimeout[32];
  3613. *wszTimeout = 0;
  3614. LONG lTimeout = 0;
  3615. ::GetDlgItemTextX(m_hDlg, IDC_TIMEOUT, wszTimeout, 32);
  3616. if (*wszTimeout != 0)
  3617. lTimeout = _wtol(wszTimeout);
  3618. if (lTimeout < 0)
  3619. lTimeout = WBEM_INFINITE;
  3620. return lTimeout;
  3621. }
  3622. ULONG CMainDlg::BatchCount()
  3623. {
  3624. wchar_t wszBatch[32];
  3625. *wszBatch = 0;
  3626. ULONG nBatch = 1;
  3627. ::GetDlgItemTextX(m_hDlg, IDC_BATCH, wszBatch, 32);
  3628. if (*wszBatch != 0)
  3629. nBatch = _wtol(wszBatch);
  3630. if (nBatch == 0)
  3631. nBatch = 1;
  3632. return nBatch;
  3633. }
  3634. CRefresherDlg::CRefresherDlg(HWND hParent, LONG lGenFlags)
  3635. : CQueryResultDlg(hParent, lGenFlags, WBEM_FLAG_DEEP)
  3636. {
  3637. CoCreateInstance(CLSID_WbemRefresher, NULL,
  3638. CLSCTX_INPROC_SERVER, IID_IWbemRefresher, (void**)&m_pRefresher);
  3639. m_pRefresher->QueryInterface(IID_IWbemConfigureRefresher,
  3640. (void**)&m_pCreator);
  3641. }
  3642. CRefresherDlg::~CRefresherDlg()
  3643. {
  3644. // Release all allocated enumerators
  3645. while ( m_apEnums.Size() != 0 )
  3646. {
  3647. IEnumWbemClassObject* pEnum = (IEnumWbemClassObject*) m_apEnums.GetAt( 0 );
  3648. if ( NULL != pEnum )
  3649. {
  3650. pEnum->Release();
  3651. }
  3652. m_apEnums.RemoveAt( 0 );
  3653. }
  3654. m_pRefresher->Release();
  3655. m_pCreator->Release();
  3656. }
  3657. BOOL CRefresherDlg::OnInitDialog()
  3658. {
  3659. SetTitle("REFRESHER");
  3660. ShowWindow(GetDlgItem(IDC_STATUS), SW_HIDE);
  3661. ShowWindow(GetDlgItem(IDC_NUM_BATCH), SW_HIDE);
  3662. if(!CQueryResultDlg::OnInitDialog())
  3663. return FALSE;
  3664. ShowWindow(GetDlgItem(IDC_REFRESH), SW_SHOWNORMAL);
  3665. return TRUE;
  3666. }
  3667. BOOL CRefresherDlg::OnCommand(WORD wCode, WORD wID)
  3668. {
  3669. if(wID == IDC_REFRESH)
  3670. {
  3671. OnRefresh();
  3672. return TRUE;
  3673. }
  3674. else return CQueryResultDlg::OnCommand(wCode, wID);
  3675. }
  3676. void CRefresherDlg::OnRefresh()
  3677. {
  3678. HRESULT hres = m_pRefresher->Refresh(0);
  3679. if(FAILED(hres))
  3680. {
  3681. FormatError(hres, NULL);
  3682. }
  3683. }
  3684. BOOL CRefresherDlg::DeleteListElement(LRESULT nSel)
  3685. {
  3686. HRESULT hres = m_pCreator->Remove(PtrToLong(m_aIds[(DWORD)nSel]), 0);
  3687. if(FAILED(hres))
  3688. {
  3689. FormatError(hres, NULL);
  3690. return FALSE;
  3691. }
  3692. m_aIds.RemoveAt((DWORD)nSel);
  3693. // If this is an enumerator, we should release it before we remove it
  3694. IEnumWbemClassObject* pEnum = (IEnumWbemClassObject*) m_apEnums.GetAt( (DWORD)nSel );
  3695. if ( NULL != pEnum )
  3696. {
  3697. pEnum->Release();
  3698. }
  3699. m_apEnums.RemoveAt((DWORD)nSel);
  3700. return TRUE;
  3701. }
  3702. IWbemClassObject* CRefresherDlg::AddNewElement()
  3703. {
  3704. if(g_pNamespace == NULL)
  3705. {
  3706. MessageBox(IDS_NO_CONNECTION, IDS_ERROR, MB_OK);
  3707. return NULL;
  3708. }
  3709. wchar_t ObjPath[2560];
  3710. INT_PTR iRet = GetRefrObjectPath(m_hDlg, ObjPath, 2560);
  3711. if(iRet == IDCANCEL)
  3712. return NULL;
  3713. if (wcslen(ObjPath) == 0)
  3714. {
  3715. MessageBox(IDS_NO_PATH, IDS_ERROR, MB_OK);
  3716. return NULL;
  3717. }
  3718. IWbemClassObject* pObj = NULL;
  3719. IWbemHiPerfEnum* pEnum = NULL;
  3720. long lId;
  3721. HRESULT hres = WBEM_S_NO_ERROR;
  3722. // iRet for whether we want to add an object or an enum
  3723. if ( IDOK == iRet )
  3724. {
  3725. hres = m_pCreator->AddObjectByPath(g_pNamespace, ObjPath,
  3726. m_lGenFlags, g_Context, &pObj, &lId);
  3727. if(FAILED(hres))
  3728. {
  3729. FormatError(hres, NULL);
  3730. return NULL;
  3731. }
  3732. #ifdef _WIN64
  3733. m_aIds.Add(IntToPtr(lId)); // ok since we are really using safearray for dword
  3734. #else
  3735. m_aIds.Add((void*)lId);
  3736. #endif
  3737. // Fakes out the Enumerator List
  3738. m_apEnums.Add( NULL );
  3739. return pObj;
  3740. }
  3741. // Add an enumerator to the dialog
  3742. hres = m_pCreator->AddEnum(g_pNamespace, ObjPath,
  3743. m_lGenFlags, g_Context, &pEnum, &lId);
  3744. if(FAILED(hres))
  3745. {
  3746. FormatError(hres, NULL);
  3747. return NULL;
  3748. }
  3749. #ifdef _WIN64
  3750. m_aIds.Add(IntToPtr(lId)); // ok since we are really using safearray for dword
  3751. #else
  3752. m_aIds.Add((void*)lId);
  3753. #endif
  3754. m_apEnums.Add( (void*) pEnum );
  3755. // Fakes out the object list
  3756. m_InternalArray.Add( NULL );
  3757. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  3758. // Allocate a large enough buffer
  3759. char* szTemp = new char[(wcslen( ObjPath ) * 2) + 64];
  3760. sprintf( szTemp, "%S Enumerator, Id: %d", ObjPath, lId );
  3761. SendMessage(hList, LB_ADDSTRING, 0, LPARAM(szTemp));
  3762. delete [] szTemp;
  3763. SetNumItems(SendMessage(hList, LB_GETCOUNT, 0, 0));
  3764. return NULL;
  3765. }
  3766. BOOL CRefresherDlg::OnDoubleClick(int nID)
  3767. {
  3768. if(nID != IDC_OBJECT_LIST) return FALSE;
  3769. HWND hList = GetDlgItem(IDC_OBJECT_LIST);
  3770. LRESULT nSel = SendMessage(hList, LB_GETCURSEL, 0, 0);
  3771. if (nSel == LB_ERR)
  3772. return TRUE;
  3773. IWbemHiPerfEnum* pEnum = (IWbemHiPerfEnum*) m_apEnums.GetAt( (DWORD)nSel );
  3774. if ( NULL != pEnum )
  3775. {
  3776. char* szTemp = new char[SendMessage( hList, LB_GETTEXTLEN, nSel, 0 )+10];
  3777. SendMessage( hList, LB_GETTEXT, nSel, (LPARAM) szTemp );
  3778. // Run the dialog modal
  3779. CRefresherEnumDlg* pDlg = new CRefresherEnumDlg(m_hDlg, m_lGenFlags, pEnum, szTemp);
  3780. pDlg->Run( NULL );
  3781. delete pDlg;
  3782. delete [] szTemp;
  3783. SendMessage(hList, LB_SETCURSEL, nSel, 0);
  3784. return TRUE;
  3785. }
  3786. else
  3787. {
  3788. return CQueryResultDlg::OnDoubleClick( nID );
  3789. }
  3790. return TRUE;
  3791. }
  3792. CRefresherEnumDlg::CRefresherEnumDlg(HWND hParent, LONG lGenFlags, IWbemHiPerfEnum* pEnum,
  3793. char* pszName)
  3794. : CQueryResultDlg(hParent, lGenFlags, WBEM_FLAG_DEEP), m_pEnum(pEnum), m_pszName(pszName)
  3795. {
  3796. if ( NULL != m_pEnum )
  3797. {
  3798. m_pEnum->AddRef();
  3799. }
  3800. }
  3801. CRefresherEnumDlg::~CRefresherEnumDlg()
  3802. {
  3803. if ( NULL != m_pEnum )
  3804. {
  3805. m_pEnum->Release();
  3806. }
  3807. }
  3808. BOOL CRefresherEnumDlg::OnInitDialog()
  3809. {
  3810. // Set the dialog title
  3811. char* szTemp = new char[strlen(m_pszName)+64];
  3812. sprintf( szTemp, "REFRESHER: %s", m_pszName );
  3813. SetTitle(szTemp);
  3814. delete [] szTemp;
  3815. ShowWindow(GetDlgItem(IDC_STATUS), SW_HIDE);
  3816. ShowWindow(GetDlgItem(IDC_NUM_BATCH), SW_HIDE);
  3817. if(!CQueryResultDlg::OnInitDialog())
  3818. return FALSE;
  3819. // Iterate the enumerator and add all its objects into the list
  3820. HRESULT hr = WBEM_S_NO_ERROR;
  3821. IWbemClassObject* pObj = NULL;
  3822. ULONG nNumObjects = 0,
  3823. nNumReturned = 0;
  3824. // Find out how many objects to retrieve then get them
  3825. // and stick them in the dialog
  3826. // Don't pass flags on this call
  3827. m_pEnum->GetObjects( 0L, 0, NULL, &nNumObjects );
  3828. if ( nNumObjects > 0 )
  3829. {
  3830. IWbemObjectAccess** apObjAccess = new IWbemObjectAccess*[nNumObjects];
  3831. if ( NULL != apObjAccess )
  3832. {
  3833. // Don't pass flags on this call
  3834. hr = m_pEnum->GetObjects( 0L, nNumObjects, apObjAccess, &nNumReturned );
  3835. if ( SUCCEEDED( hr ) )
  3836. {
  3837. IWbemClassObject* pClassObj = NULL;
  3838. for ( ULONG n = 0; SUCCEEDED( hr ) && n < nNumReturned; n++ )
  3839. {
  3840. hr = apObjAccess[n]->QueryInterface( IID_IWbemClassObject, (void**) &pClassObj );
  3841. if ( SUCCEEDED( hr ) )
  3842. {
  3843. AddObject( pClassObj );
  3844. apObjAccess[n]->Release();
  3845. pClassObj->Release();
  3846. }
  3847. }
  3848. }
  3849. delete [] apObjAccess;
  3850. }
  3851. else
  3852. {
  3853. hr = WBEM_E_OUT_OF_MEMORY;
  3854. }
  3855. }
  3856. if ( FAILED( hr ) )
  3857. {
  3858. FormatError(hr, NULL);
  3859. }
  3860. return TRUE;
  3861. }
  3862. CHourGlass::CHourGlass()
  3863. {
  3864. m_hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  3865. }
  3866. CHourGlass::~CHourGlass()
  3867. {
  3868. SetCursor(m_hCursor);
  3869. }
  3870. HRESULT EnableAllPrivileges(DWORD dwTokenType)
  3871. {
  3872. // Open thread token
  3873. // =================
  3874. HANDLE hToken = NULL;
  3875. BOOL bRes;
  3876. switch (dwTokenType)
  3877. {
  3878. case TOKEN_THREAD:
  3879. bRes = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, TRUE, &hToken);
  3880. break;
  3881. case TOKEN_PROCESS:
  3882. bRes = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken);
  3883. break;
  3884. }
  3885. if(!bRes)
  3886. return WBEM_E_ACCESS_DENIED;
  3887. // Get the privileges
  3888. // ==================
  3889. DWORD dwLen;
  3890. TOKEN_USER tu;
  3891. memset(&tu,0,sizeof(TOKEN_USER));
  3892. bRes = GetTokenInformation(hToken, TokenPrivileges, &tu, sizeof(TOKEN_USER), &dwLen);
  3893. BYTE* pBuffer = new BYTE[dwLen];
  3894. if(pBuffer == NULL)
  3895. {
  3896. CloseHandle(hToken);
  3897. return WBEM_E_OUT_OF_MEMORY;
  3898. }
  3899. bRes = GetTokenInformation(hToken, TokenPrivileges, pBuffer, dwLen,
  3900. &dwLen);
  3901. if(!bRes)
  3902. {
  3903. CloseHandle(hToken);
  3904. delete [] pBuffer;
  3905. return WBEM_E_ACCESS_DENIED;
  3906. }
  3907. // Iterate through all the privileges and enable them all
  3908. // ======================================================
  3909. TOKEN_PRIVILEGES* pPrivs = (TOKEN_PRIVILEGES*)pBuffer;
  3910. for(DWORD i = 0; i < pPrivs->PrivilegeCount; i++)
  3911. {
  3912. pPrivs->Privileges[i].Attributes |= SE_PRIVILEGE_ENABLED;
  3913. }
  3914. // Store the information back into the token
  3915. // =========================================
  3916. bRes = AdjustTokenPrivileges(hToken, FALSE, pPrivs, 0, NULL, NULL);
  3917. delete [] pBuffer;
  3918. CloseHandle(hToken);
  3919. if(!bRes)
  3920. return WBEM_E_ACCESS_DENIED;
  3921. else
  3922. return WBEM_S_NO_ERROR;
  3923. }
  3924. void CMainDlg::Open()
  3925. {
  3926. wchar_t ObjPath[2560];
  3927. int iRet = (int) GetObjectPath(m_hDlg, ObjPath, 2560);
  3928. if(iRet == IDCANCEL)
  3929. return;
  3930. if (wcslen(ObjPath) == 0)
  3931. {
  3932. MessageBox(IDS_NO_PATH, IDS_ERROR, MB_OK);
  3933. return;
  3934. }
  3935. if (g_pNamespace == 0)
  3936. {
  3937. MessageBox(IDS_NO_INITIAL_CONNECTION, IDS_ERROR, MB_OK);
  3938. return;
  3939. }
  3940. IWbemServicesEx* pNewNs = NULL;
  3941. CBString bsObjPath(ObjPath);
  3942. HRESULT res;
  3943. IWbemClassObject* pErrorObj = NULL;
  3944. // Asynchronous
  3945. if (m_lSync & ASYNC)
  3946. {
  3947. IUnknown *pUnknown=NULL;
  3948. CTestNotify* pSink=new CTestNotify(1);
  3949. res=g_pServicesEx->OpenAsync(bsObjPath.GetString(), 0,
  3950. m_lGenFlags,
  3951. g_Context, CUnsecWrapEx(pSink));
  3952. if (SUCCEEDED(res))
  3953. {
  3954. pSink->WaitForSignal(INFINITE);
  3955. res = pSink->GetStatusCode();
  3956. if(SUCCEEDED(res))
  3957. {
  3958. pUnknown=pSink->GetInterface();
  3959. if(pUnknown)
  3960. {
  3961. res = pUnknown->QueryInterface(IID_IWbemServicesEx, (void**)&pNewNs);
  3962. pUnknown->Release();
  3963. }
  3964. else
  3965. res = WBEM_E_FAILED;
  3966. }
  3967. }
  3968. pSink->Release();
  3969. }
  3970. // Semisynchronous
  3971. else if (m_lSync & SEMISYNC)
  3972. {
  3973. IWbemCallResultEx* pCallRes = NULL;
  3974. CHourGlass hg;
  3975. res = g_pServicesEx->Open(bsObjPath.GetString(), 0,
  3976. m_lGenFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  3977. g_Context, NULL, &pCallRes);
  3978. if (SUCCEEDED(res))
  3979. {
  3980. LONG lStatus;
  3981. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  3982. while ((res = pCallRes->GetCallStatus(Timeout(), &lStatus)) == WBEM_S_TIMEDOUT)
  3983. {
  3984. // wait
  3985. }
  3986. if (res == WBEM_S_NO_ERROR)
  3987. {
  3988. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::OpenNamespace call
  3989. if (res == WBEM_S_NO_ERROR)
  3990. {
  3991. res = pCallRes->GetResult(0, 0, IID_IWbemServicesEx, (LPVOID *) &pNewNs); // don't use timeout since object should be available
  3992. }
  3993. }
  3994. pCallRes->Release();
  3995. }
  3996. }
  3997. // Synchronous
  3998. else
  3999. {
  4000. CHourGlass hg;
  4001. res = g_pServicesEx->Open(bsObjPath.GetString(), 0,
  4002. m_lGenFlags,
  4003. g_Context, &pNewNs, NULL);
  4004. }
  4005. if (FAILED(res))
  4006. {
  4007. FormatError(res, m_hDlg, pErrorObj);
  4008. return;
  4009. }
  4010. g_pNamespace->Release();
  4011. g_pNamespace = pNewNs;
  4012. SetInterfaceSecurityEx(g_pNamespace, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  4013. wcscat(gNameSpace, L":");
  4014. wcscat(gNameSpace, bsObjPath.GetString());
  4015. SetDlgItemTextWC(m_hDlg, IDC_NAMESPACE, gNameSpace);
  4016. SysFreeString(g_strNamespace);
  4017. g_strNamespace = SysAllocString(gNameSpace);
  4018. }
  4019. void CMainDlg::DoServicesExGenObject(int iOpt)
  4020. {
  4021. wchar_t ObjPath[2560];
  4022. *ObjPath = 0;
  4023. int iRet = (int) GetObjectPath(m_hDlg, ObjPath, 2560);
  4024. if (iRet == IDCANCEL)
  4025. return;
  4026. if (wcslen(ObjPath) == 0)
  4027. return;
  4028. CBString bsObjPath(ObjPath);
  4029. HRESULT res = 0;
  4030. IWbemClassObject* pErrorObj = NULL;
  4031. // Asynchronous
  4032. if (m_lSync & ASYNC)
  4033. {
  4034. MessageBox(IDS_ASYNC_NOT_SUPPORTED, IDS_ERROR, MB_OK);
  4035. return;
  4036. }
  4037. // Semisynchronous
  4038. else if (m_lSync & SEMISYNC)
  4039. {
  4040. IWbemCallResultEx* pCallRes = NULL;
  4041. CHourGlass hg;
  4042. switch (iOpt)
  4043. {
  4044. case IDC_ADDOBJECT:
  4045. // Link
  4046. res = E_NOTIMPL;
  4047. break;
  4048. case IDC_DELETEOBJECT:
  4049. // Unlink
  4050. res = E_NOTIMPL;
  4051. break;
  4052. }
  4053. if (SUCCEEDED(res))
  4054. {
  4055. LONG lStatus;
  4056. LONG lTimeout = Timeout();
  4057. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  4058. while ((res = pCallRes->GetCallStatus(lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  4059. {
  4060. // wait
  4061. }
  4062. if (res == WBEM_S_NO_ERROR)
  4063. {
  4064. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::DeleteIntance call
  4065. }
  4066. pCallRes->Release();
  4067. }
  4068. }
  4069. // Synchronous
  4070. else
  4071. {
  4072. CHourGlass hg;
  4073. switch (iOpt)
  4074. {
  4075. case IDC_ADDOBJECT:
  4076. // Link
  4077. res = E_NOTIMPL;
  4078. break;
  4079. case IDC_DELETEOBJECT:
  4080. // Unlink
  4081. res = E_NOTIMPL;
  4082. break;
  4083. }
  4084. }
  4085. if (FAILED(res))
  4086. {
  4087. FormatError(res, m_hDlg, pErrorObj);
  4088. }
  4089. }
  4090. void CMainDlg::AddObject()
  4091. {
  4092. DoServicesExGenObject(IDC_ADDOBJECT);
  4093. }
  4094. void CMainDlg::DeleteObject()
  4095. {
  4096. wchar_t ObjPath[2560];
  4097. *ObjPath = 0;
  4098. INT_PTR iRet = GetObjectPath(m_hDlg, ObjPath, 2560);
  4099. if (iRet == IDCANCEL)
  4100. return;
  4101. if (wcslen(ObjPath) == 0)
  4102. return;
  4103. CBString bsObjPath(ObjPath);
  4104. HRESULT res;
  4105. IWbemClassObject* pErrorObj = NULL;
  4106. // Asynchronous
  4107. if (m_lSync & ASYNC)
  4108. {
  4109. CHourGlass hg;
  4110. CTestNotify* pNtfy = new CTestNotify(1);
  4111. res = g_pServicesEx->DeleteObjectAsync(bsObjPath.GetString(),
  4112. m_lGenFlags | WBEM_FLAG_SEND_STATUS,
  4113. g_Context, CUnsecWrap(pNtfy));
  4114. if (SUCCEEDED(res))
  4115. {
  4116. pNtfy->WaitForSignal(INFINITE);
  4117. res = pNtfy->GetStatusCode(&pErrorObj);
  4118. }
  4119. pNtfy->Release();
  4120. }
  4121. // Semisynchronous
  4122. else if (m_lSync & SEMISYNC)
  4123. {
  4124. IWbemCallResult* pCallRes = NULL;
  4125. CHourGlass hg;
  4126. res = g_pServicesEx->DeleteObject(bsObjPath.GetString(),
  4127. m_lGenFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  4128. g_Context, &pCallRes);
  4129. if (SUCCEEDED(res))
  4130. {
  4131. LONG lStatus;
  4132. LONG lTimeout = Timeout();
  4133. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  4134. while ((res = pCallRes->GetCallStatus(lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  4135. {
  4136. // wait
  4137. }
  4138. if (res == WBEM_S_NO_ERROR)
  4139. {
  4140. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::DeleteIntance call
  4141. }
  4142. pCallRes->Release();
  4143. }
  4144. }
  4145. // Synchronous
  4146. else
  4147. {
  4148. CHourGlass hg;
  4149. res = g_pServicesEx->DeleteObject(bsObjPath.GetString(), m_lGenFlags,
  4150. g_Context, NULL);
  4151. }
  4152. if (FAILED(res))
  4153. {
  4154. FormatError(res, m_hDlg, pErrorObj);
  4155. }
  4156. }
  4157. void CMainDlg::GetObjectSecurity()
  4158. {
  4159. ::MessageBox(NULL, "Show me the code", "help!", MB_OK);
  4160. }
  4161. void CMainDlg::SetObjectSecurity()
  4162. {
  4163. ::MessageBox(NULL, "Show me the code", "help!", MB_OK);
  4164. }
  4165. void CMainDlg::RenameObject()
  4166. {
  4167. WCHAR wsOld[512], wsNew[512];
  4168. CRenameDlg InfoDlg(m_hDlg, wsOld, 512, wsNew, 512);
  4169. if(IDOK != InfoDlg.Run())
  4170. return;
  4171. CBString bsOld(wsOld);
  4172. CBString bsNew(wsNew);
  4173. HRESULT res;
  4174. IWbemClassObject* pErrorObj = NULL;
  4175. // Asynchronous
  4176. if (m_lSync & ASYNC)
  4177. {
  4178. MessageBox(IDS_ASYNC_NOT_SUPPORTED, IDS_ERROR, MB_OK);
  4179. return;
  4180. }
  4181. // Semisynchronous
  4182. else if (m_lSync & SEMISYNC)
  4183. {
  4184. IWbemCallResultEx* pCallRes = NULL;
  4185. CHourGlass hg;
  4186. res = g_pServicesEx->RenameObject(bsOld.GetString(), bsNew.GetString(),
  4187. m_lGenFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  4188. g_Context, &pCallRes);
  4189. if (SUCCEEDED(res))
  4190. {
  4191. LONG lStatus;
  4192. LONG lTimeout = Timeout();
  4193. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  4194. while ((res = pCallRes->GetCallStatus(lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  4195. {
  4196. // wait
  4197. }
  4198. if (res == WBEM_S_NO_ERROR)
  4199. {
  4200. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::DeleteIntance call
  4201. }
  4202. pCallRes->Release();
  4203. }
  4204. }
  4205. // Synchronous
  4206. else
  4207. {
  4208. CHourGlass hg;
  4209. res = g_pServicesEx->RenameObject(bsOld.GetString(), bsNew.GetString(),
  4210. m_lGenFlags,
  4211. g_Context, NULL);
  4212. }
  4213. if (FAILED(res))
  4214. {
  4215. FormatError(res, m_hDlg, pErrorObj);
  4216. }
  4217. }