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

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