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.

2567 lines
68 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. OBJEDIT.CPP
  5. Abstract:
  6. WBEMTEST object editor classes.
  7. History:
  8. a-raymcc 12-Jun-96 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include <stdio.h>
  12. #include "wbemqual.h"
  13. #include "resource.h"
  14. #include "resrc1.h"
  15. #include "objedit.h"
  16. #include "wbemtest.h"
  17. //#include <wbemutil.h>
  18. #include "textconv.h"
  19. #include <cominit.h>
  20. #include "bstring.h"
  21. #include "WT_wstring.h"
  22. #include "method.h"
  23. #include <autoptr.h>
  24. // following were changed due to very large SNMP properties
  25. #define LARGE_BUF 2096
  26. #define IDC_CLASS IDC_SUPERCLASS
  27. #define IDC_REFERENCES IDC_DERIVED
  28. #define IDC_ASSOCIATIONS IDC_INSTANCES
  29. extern DWORD gdwAuthLevel;
  30. extern DWORD gdwImpLevel;
  31. extern BSTR gpPrincipal;
  32. extern COAUTHIDENTITY* gpAuthIdentity;
  33. char *ValidQualifierTypes[] =
  34. {
  35. "CIM_SINT32",
  36. "CIM_STRING",
  37. "CIM_BOOLEAN",
  38. "CIM_REAL64"
  39. };
  40. void CopyQualifierSet(IWbemQualifierSet* pDest, IWbemQualifierSet* pSrc, HWND hDlg)
  41. {
  42. pSrc->BeginEnumeration(0);
  43. BSTR strName = NULL;
  44. VARIANT vVal;
  45. VariantInit(&vVal);
  46. long lFlavor;
  47. while(pSrc->Next(0, &strName, &vVal, &lFlavor) == S_OK)
  48. {
  49. if(!wbem_wcsicmp(strName, L"cimtype") &&
  50. !wbem_wcsicmp(V_BSTR(&vVal), L"sint32")) continue;
  51. SCODE hres = pDest->Put(strName, &vVal, lFlavor);
  52. if(FAILED(hres))
  53. {
  54. FormatError(hres, hDlg);
  55. }
  56. }
  57. }
  58. const int nNumValidQualifierTypes = sizeof(ValidQualifierTypes) / sizeof(char *);
  59. LPSTR TypeToString(CVar *p)
  60. {
  61. return TypeToString(p->GetOleType());
  62. }
  63. LPSTR LPWSTRToLPSTR(LPWSTR pWStr)
  64. {
  65. static char buf[TEMP_BUF];
  66. wcstombs(buf, pWStr, TEMP_BUF);
  67. buf[TEMP_BUF-1] = '\0';
  68. return buf;
  69. }
  70. LPSTR CTestQualifierToString(CTestQualifier *pQualifier)
  71. {
  72. int requiredLength = 0;
  73. char * typeString = 0;
  74. char * valueString = 0;
  75. char * returnString;
  76. int nameLength = 0;
  77. if (pQualifier->m_pName)
  78. {
  79. nameLength = wcslen(pQualifier->m_pName)+1;
  80. requiredLength += 2*nameLength;
  81. }
  82. if (pQualifier->m_pValue)
  83. {
  84. typeString = TypeToString(pQualifier->m_pValue);
  85. valueString = ValueToNewString(pQualifier->m_pValue);
  86. requiredLength += strlen(typeString)+strlen(valueString)+2; //2 tabs
  87. // null is nameLength
  88. }
  89. returnString = new char[requiredLength];
  90. if (returnString == 0)
  91. {
  92. delete[] valueString;
  93. return 0;
  94. }
  95. if (pQualifier->m_pName)
  96. {
  97. wcstombs(returnString, pQualifier->m_pName, nameLength*2);
  98. }
  99. if (pQualifier->m_pValue)
  100. {
  101. strcat(returnString, "\t");
  102. strcat(returnString, typeString);
  103. strcat(returnString, "\t");
  104. strcat(returnString, valueString);
  105. delete[] valueString;
  106. }
  107. return returnString;
  108. }
  109. BOOL CTestQualifierEditor::Verify()
  110. {
  111. // Get the Qualifier name.
  112. // =======================
  113. char NameBuf[TEMP_BUF];
  114. char buf[TEMP_BUF];
  115. if (GetWindowText(m_hQualifierName, NameBuf, TEMP_BUF) == 0)
  116. {
  117. MessageBox(IDS_INVALID_QUALIFIER_NAME, IDS_ERROR, MB_OK);
  118. return FALSE;
  119. }
  120. StripTrailingWs(NameBuf);
  121. delete m_pTarget->m_pName;
  122. WString Tmp(NameBuf);
  123. m_pTarget->m_pName = new wchar_t[wcslen(Tmp) + 1];
  124. wcscpy(m_pTarget->m_pName, Tmp);
  125. // Get the Qualifier flavor.
  126. // =========================
  127. m_pTarget->m_lType = 0;
  128. if (SendMessage(m_hRadioPropClass, BM_GETCHECK, 0, 0) == BST_CHECKED)
  129. m_pTarget->m_lType |= WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS;
  130. if (SendMessage(m_hRadioPropInst, BM_GETCHECK, 0, 0) == BST_CHECKED)
  131. m_pTarget->m_lType |= WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE;
  132. if (SendMessage(m_hRadioOverride, BM_GETCHECK, 0, 0) == BST_UNCHECKED)
  133. m_pTarget->m_lType |= WBEM_FLAVOR_NOT_OVERRIDABLE;
  134. if (SendMessage(m_hRadioAmended, BM_GETCHECK, 0, 0) == BST_CHECKED)
  135. m_pTarget->m_lType |= WBEM_FLAVOR_AMENDED;
  136. // NOTE: do not retrieve origin!
  137. // Get the type string.
  138. // ====================
  139. LRESULT nIndex = SendMessage(m_hQualifierType, CB_GETCURSEL, 0, 0);
  140. if (SendMessage(m_hQualifierType, CB_GETLBTEXT, nIndex, LPARAM(buf)) == CB_ERR)
  141. {
  142. MessageBox(IDS_INVALID_QUALIFIER_TYPE, IDS_ERROR, MB_OK);
  143. return FALSE;
  144. }
  145. // Convert type string to a type.
  146. // ==============================
  147. int nType = StringToType(buf);
  148. if (nType == 0)
  149. {
  150. MessageBox(IDS_INVALID_QUALIFIER_TYPE, IDS_ERROR, MB_OK);
  151. return FALSE;
  152. }
  153. if(GetCheck(IDC_ARRAY) == BST_CHECKED)
  154. nType |= VT_ARRAY;
  155. // Get the value.
  156. // ==============
  157. if (m_pTarget->m_pValue)
  158. {
  159. m_pTarget->m_pValue->Empty();
  160. m_pTarget->m_pValue->SetAsNull();
  161. }
  162. CVar *pTemp = 0;
  163. if (GetWindowText(m_hQualifierVal, buf, TEMP_BUF))
  164. {
  165. StripTrailingWs(buf);
  166. pTemp = StringToValue(buf, nType);
  167. }
  168. else // Value is NULL
  169. {
  170. pTemp = new CVar;
  171. }
  172. if (pTemp)
  173. {
  174. if (m_pTarget->m_pValue)
  175. {
  176. *m_pTarget->m_pValue = *pTemp;
  177. delete pTemp;
  178. }
  179. else
  180. m_pTarget->m_pValue = pTemp;
  181. }
  182. else
  183. {
  184. MessageBox(IDS_INVALID_VALUE, IDS_ERROR, MB_OK);
  185. return FALSE;
  186. }
  187. return TRUE;
  188. }
  189. BOOL CTestQualifierEditor::OnInitDialog()
  190. {
  191. CenterOnParent();
  192. m_hQualifierName = GetDlgItem(IDC_ATTRIB_NAME);
  193. m_hQualifierVal = GetDlgItem(IDC_ATTRIB_VALUE);
  194. m_hQualifierType = GetDlgItem(IDC_ATTRIB_TYPE);
  195. m_hRadioPropInst = GetDlgItem(IDC_PROP_INST);
  196. m_hRadioPropClass = GetDlgItem(IDC_PROP_CLASS);
  197. m_hRadioOverride = GetDlgItem(IDC_OVERRIDE);
  198. m_hRadioPropagated = GetDlgItem(IDC_PROPAGATED);
  199. m_hRadioAmended = GetDlgItem(IDC_AMENDED);
  200. SendMessage(m_hRadioPropInst, BM_SETCHECK,
  201. (m_pTarget->m_lType & WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE)?BST_CHECKED:BST_UNCHECKED,
  202. 0);
  203. SendMessage(m_hRadioPropClass, BM_SETCHECK,
  204. (m_pTarget->m_lType & WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS)?BST_CHECKED:BST_UNCHECKED,
  205. 0);
  206. SendMessage(m_hRadioOverride, BM_SETCHECK,
  207. (m_pTarget->m_lType & WBEM_FLAVOR_NOT_OVERRIDABLE)?BST_UNCHECKED:BST_CHECKED,
  208. 0);
  209. SendMessage(m_hRadioPropagated, BM_SETCHECK,
  210. (m_pTarget->m_lType & WBEM_FLAVOR_ORIGIN_PROPAGATED)?BST_CHECKED:BST_UNCHECKED,
  211. 0);
  212. SendMessage(m_hRadioAmended, BM_SETCHECK,
  213. (m_pTarget->m_lType & WBEM_FLAVOR_AMENDED)?BST_CHECKED:BST_UNCHECKED,
  214. 0);
  215. EnableWindow(m_hRadioPropagated, FALSE);
  216. // Default property name.
  217. // ======================
  218. if (m_pTarget->m_pName)
  219. SetWindowText(m_hQualifierName, LPWSTRToLPSTR(m_pTarget->m_pName));
  220. // Populate the combo box with the valid Qualifier types
  221. // ======================================================
  222. for (int i = 0; i < nNumValidQualifierTypes; i++)
  223. SendMessage(m_hQualifierType, CB_ADDSTRING, 0, LPARAM(ValidQualifierTypes[i]));
  224. // If a type is assigned, select it.
  225. // =================================
  226. if (m_pTarget->m_pValue)
  227. {
  228. long lType = m_pTarget->m_pValue->GetOleType();
  229. LPSTR pTypeStr = TypeToString(lType & ~VT_ARRAY);
  230. SendMessage(m_hQualifierType, CB_SELECTSTRING, WPARAM(-1),
  231. LPARAM(pTypeStr));
  232. // Set the array bit
  233. // =================
  234. SetCheck(IDC_ARRAY,
  235. ((lType & VT_ARRAY) ? BST_CHECKED : BST_UNCHECKED));
  236. }
  237. // Else select VT_BSTR by default.
  238. // ===============================
  239. else
  240. {
  241. SendMessage(m_hQualifierType, CB_SELECTSTRING, WPARAM(-1), LPARAM("CIM_STRING"));
  242. }
  243. // If a value is assigned, initialize it.
  244. // ======================================
  245. if (m_pTarget->m_pValue)
  246. {
  247. LPSTR pVal = ValueToNewString(m_pTarget->m_pValue);
  248. SetWindowText(m_hQualifierVal, pVal);
  249. delete[] pVal;
  250. }
  251. // If editing, don't allow the user to change
  252. // the Qualifier name or type.
  253. // ==========================================
  254. if (m_bEditing)
  255. {
  256. EnableWindow(m_hQualifierName, FALSE);
  257. }
  258. return TRUE;
  259. }
  260. CTestQualifierEditor::CTestQualifierEditor(
  261. HWND hParent,
  262. CTestQualifier *pTarget,
  263. BOOL bEditing
  264. ) : CWbemDialog(IDD_ATTRIB_EDITOR, hParent)
  265. {
  266. m_pTarget = pTarget;
  267. m_bEditing = bEditing;
  268. }
  269. INT_PTR CTestQualifierEditor::Edit()
  270. {
  271. return Run();
  272. }
  273. CEmbeddedObjectListEditor::CEmbeddedObjectListEditor(HWND hParent, LONG lGenFlags, LONG lQryFlags,
  274. LPCWSTR wszPropName, CVarVector* pVarVector)
  275. : CQueryResultDlg(hParent, lGenFlags, lQryFlags), m_wsPropName((LPWSTR)wszPropName)
  276. {
  277. m_pVarVector = pVarVector;
  278. for(int i = 0; i < pVarVector->Size(); i++)
  279. {
  280. CVar vTemp;
  281. pVarVector->FillCVarAt( i, vTemp );
  282. IWbemClassObject* pObj = (IWbemClassObject*) vTemp.GetEmbeddedObject();
  283. m_InternalArray.Add(pObj);
  284. // Verify the object pointer
  285. if ( NULL != pObj )
  286. {
  287. pObj->Release();
  288. }
  289. }
  290. }
  291. CEmbeddedObjectListEditor::~CEmbeddedObjectListEditor()
  292. {
  293. // Prevent object release --- we don't own them.
  294. // =============================================
  295. }
  296. BOOL CEmbeddedObjectListEditor::OnInitDialog()
  297. {
  298. char szBuffer[1000];
  299. char szFormat[104];
  300. if(LoadString(GetModuleHandle(NULL), IDS_EMBEDDED_ARRAY, szFormat, 104))
  301. {
  302. sprintf(szBuffer, szFormat, (LPWSTR)m_wsPropName);
  303. SetTitle(szBuffer);
  304. }
  305. else
  306. SetTitle("Embedded array");
  307. BOOL bRet = CQueryResultDlg::OnInitDialog();
  308. SetComplete(WBEM_S_NO_ERROR, NULL, NULL);
  309. return bRet;
  310. }
  311. BOOL CEmbeddedObjectListEditor::Verify()
  312. {
  313. // First AddRef all elements - this is so we don't release objects
  314. // out from underneath ourselves.
  315. for(int i = 0; i < m_InternalArray.Size(); i++)
  316. {
  317. IWbemClassObject* pObj = (IWbemClassObject*)m_InternalArray[i];
  318. if ( NULL != pObj )
  319. {
  320. pObj->AddRef();
  321. }
  322. }
  323. // Now axe the elements in the Vector
  324. while(m_pVarVector->Size()) m_pVarVector->RemoveAt(0);
  325. // First AddRef all elements - this is so we don't release objects
  326. // out from underneath ourselves.
  327. for(int i = 0; i < m_InternalArray.Size(); i++)
  328. {
  329. IWbemClassObject* pObj = (IWbemClassObject*)m_InternalArray[i];
  330. CVar v;
  331. v.SetEmbeddedObject((I_EMBEDDED_OBJECT*)pObj);
  332. m_pVarVector->Add( v );
  333. }
  334. return TRUE;
  335. }
  336. IWbemClassObject* CEmbeddedObjectListEditor::AddNewElement()
  337. {
  338. return _CreateInstance(m_hDlg, m_lGenFlags, m_lSync, m_lTimeout);
  339. }
  340. BOOL CEmbeddedObjectListEditor::DeleteListElement(int nSel)
  341. {
  342. return TRUE;
  343. }
  344. //***************************************************************************
  345. //
  346. // class CTestPropertyEditor
  347. //
  348. //***************************************************************************
  349. class CTestPropertyEditor : public CWbemDialog
  350. {
  351. CTestProperty *m_pTarget;
  352. BOOL m_bEditOnly;
  353. BOOL m_bInstance;
  354. LONG m_lGenFlags; // generic WBEM_FLAG_ .. flags
  355. LONG m_lSync; // sync, async, semisync
  356. LONG m_lTimeout; // used for semisync
  357. // Control handles.
  358. // ================
  359. HWND m_hPropName;
  360. HWND m_hPropType;
  361. HWND m_hValue;
  362. HWND m_hQualifierList;
  363. public:
  364. CTestPropertyEditor(HWND hParent, LONG lGenFlags, BOOL bEditOnly, LONG lSync,
  365. CTestProperty *pProp, BOOL bInstance, LONG lTimeout);
  366. INT_PTR Edit();
  367. BOOL OnInitDialog();
  368. BOOL Verify();
  369. BOOL OnCommand(WORD wCode, WORD wID);
  370. BOOL OnDoubleClick(int nID);
  371. BOOL OnSelChange(int nID);
  372. void Refresh();
  373. void OnAddQualifier();
  374. void OnDelQualifier();
  375. void OnEditQualifier();
  376. void OnKey();
  377. void OnIndexed();
  378. void OnNotNull();
  379. void ViewEmbedding();
  380. void OnValueNull();
  381. void OnValueNotNull();
  382. void OnArray();
  383. int RemoveSysQuals();
  384. void SetSystemCheck(int nID);
  385. };
  386. void CTestPropertyEditor::OnAddQualifier()
  387. {
  388. CTestQualifier att;
  389. att.m_lType =
  390. WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE |
  391. WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS;
  392. CTestQualifierEditor ed(m_hDlg, &att, FALSE);
  393. INT_PTR nRes = ed.Edit();
  394. if ((nRes == IDCANCEL) || (nRes == 0))
  395. return;
  396. // If here, the Qualifier is being added.
  397. // ======================================
  398. IWbemQualifierSet* pQualifierSet = m_pTarget->m_pQualifiers;
  399. VARIANT *p = att.m_pValue->GetNewVariant();
  400. HRESULT hres = pQualifierSet->Put(att.m_pName, p, att.m_lType);
  401. if(FAILED(hres))
  402. {
  403. FormatError(hres, m_hDlg);
  404. }
  405. VariantClear(p);
  406. Refresh();
  407. }
  408. void CTestPropertyEditor::SetSystemCheck(int nID)
  409. {
  410. SetCheck(IDC_KEY, BST_UNCHECKED);
  411. SetCheck(IDC_INDEXED, BST_UNCHECKED);
  412. SetCheck(IDC_NOT_NULL, BST_UNCHECKED);
  413. SetCheck(IDC_NORMAL, BST_UNCHECKED);
  414. SetCheck(nID, BST_CHECKED);
  415. }
  416. int CTestPropertyEditor::RemoveSysQuals()
  417. {
  418. IWbemQualifierSet* pSet = m_pTarget->m_pQualifiers;
  419. HRESULT hres;
  420. hres = pSet->Delete(L"key");
  421. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  422. {
  423. SetSystemCheck(IDC_KEY);
  424. return IDC_KEY;
  425. }
  426. hres = pSet->Delete(L"indexed");
  427. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  428. {
  429. SetSystemCheck(IDC_INDEXED);
  430. return IDC_INDEXED;
  431. }
  432. hres = pSet->Delete(L"not_null");
  433. if(FAILED(hres) && hres != WBEM_E_NOT_FOUND)
  434. {
  435. SetSystemCheck(IDC_NOT_NULL);
  436. return IDC_NOT_NULL;
  437. }
  438. return 0;
  439. }
  440. void CTestPropertyEditor::OnIndexed()
  441. {
  442. IWbemQualifierSet* pQualifierSet = m_pTarget->m_pQualifiers;
  443. int nRes = RemoveSysQuals();
  444. if(nRes != 0)
  445. {
  446. MessageBox(IDS_CANNOT_CHANGE_SYSTEM_QUALS, IDS_ERROR, MB_OK);
  447. return;
  448. }
  449. VARIANT v;
  450. V_VT(&v) = VT_BOOL;
  451. V_BOOL(&v) = VARIANT_TRUE;
  452. HRESULT hres = pQualifierSet->Put(L"indexed",
  453. &v,
  454. WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE |
  455. WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS);
  456. if(FAILED(hres))
  457. {
  458. MessageBox(IDS_MAY_NOT_SPECIFY_INDEXED, IDS_ERROR,
  459. MB_OK | MB_ICONSTOP);
  460. SetSystemCheck(IDC_NORMAL);
  461. }
  462. }
  463. void CTestPropertyEditor::OnKey()
  464. {
  465. IWbemQualifierSet* pQualifierSet = m_pTarget->m_pQualifiers;
  466. int nRes = RemoveSysQuals();
  467. if(nRes != 0)
  468. {
  469. MessageBox(IDS_CANNOT_CHANGE_SYSTEM_QUALS, IDS_ERROR, MB_OK);
  470. return;
  471. }
  472. VARIANT v;
  473. V_VT(&v) = VT_BOOL;
  474. V_BOOL(&v) = VARIANT_TRUE;
  475. HRESULT hres = pQualifierSet->Put(L"key",
  476. &v,
  477. WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE |
  478. WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS);
  479. if(FAILED(hres) && hres == WBEM_E_CANNOT_BE_KEY)
  480. {
  481. MessageBox(IDS_MAY_NOT_SPECIFY_KEY, IDS_ERROR,
  482. MB_OK | MB_ICONSTOP);
  483. SetSystemCheck(IDC_NORMAL);
  484. }
  485. }
  486. void CTestPropertyEditor::OnNotNull()
  487. {
  488. IWbemQualifierSet* pQualifierSet = m_pTarget->m_pQualifiers;
  489. int nRes = RemoveSysQuals();
  490. if(nRes != 0)
  491. {
  492. MessageBox(IDS_CANNOT_CHANGE_SYSTEM_QUALS, IDS_ERROR, MB_OK);
  493. return;
  494. }
  495. VARIANT v;
  496. V_VT(&v) = VT_BOOL;
  497. V_BOOL(&v) = VARIANT_TRUE;
  498. HRESULT hres = pQualifierSet->Put(L"not_null",
  499. &v,
  500. WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE |
  501. WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS);
  502. if(FAILED(hres))
  503. {
  504. MessageBox(IDS_MAY_NOT_SPECIFY_NOT_NULL, IDS_ERROR,
  505. MB_OK | MB_ICONSTOP);
  506. SetSystemCheck(IDC_NORMAL);
  507. }
  508. }
  509. void CTestPropertyEditor::OnEditQualifier()
  510. {
  511. // See if anything is selected.
  512. // ============================
  513. LRESULT nSel = SendMessage(m_hQualifierList, LB_GETCURSEL, 0, 0);
  514. if (nSel == LB_ERR)
  515. return;
  516. char buf[TEMP_BUF];
  517. *buf = 0;
  518. SendMessage(m_hQualifierList, LB_GETTEXT, nSel, LPARAM(buf));
  519. if (*buf == 0)
  520. return;
  521. // At this point, the text of the selected Qualifier is in <buf>.
  522. // ==============================================================
  523. char name[TEMP_BUF];
  524. *name = 0;
  525. if (sscanf(buf, "%[^\t\0]", name) == EOF)
  526. return;
  527. if (*name == 0)
  528. return;
  529. WString WName = name;
  530. // Find the Qualifier in question.
  531. // ===============================
  532. VARIANT v;
  533. VariantInit(&v);
  534. LONG lType = 0;
  535. IWbemQualifierSet* pQualifierSet = m_pTarget->m_pQualifiers;
  536. CBString bsWName(WName);
  537. SCODE res = pQualifierSet->Get(bsWName.GetString(), 0, &v, &lType);
  538. if (res != 0)
  539. {
  540. MessageBox(IDS_QUALIFIER_NOT_FOUND, IDS_CRITICAL_ERROR, MB_OK);
  541. return;
  542. }
  543. // If here, convert temporarily to a CTestQualifier object for the duration of
  544. // the edit.
  545. // ====================================================================
  546. CVar *pNewVal = new CVar;
  547. pNewVal->SetVariant(&v);
  548. VariantClear(&v);
  549. CTestQualifier att;
  550. att.m_pValue = pNewVal;
  551. att.m_pName = new wchar_t[wcslen(WName) + 1];
  552. wcscpy(att.m_pName, WName);
  553. att.m_lType = lType;
  554. CTestQualifierEditor ed(m_hDlg, &att, TRUE);
  555. INT_PTR nRes = ed.Edit();
  556. if ((nRes == IDCANCEL) || (nRes == 0))
  557. {
  558. return;
  559. }
  560. // If here, the Qualifier is being added.
  561. // ======================================
  562. VARIANT *p = att.m_pValue->GetNewVariant();
  563. CBString bsName(att.m_pName);
  564. res = pQualifierSet->Put(bsName.GetString(), p, att.m_lType);
  565. if(FAILED(res))
  566. {
  567. FormatError(res, m_hDlg);
  568. }
  569. VariantClear(p);
  570. Refresh();
  571. }
  572. //ok
  573. void CTestPropertyEditor::OnDelQualifier()
  574. {
  575. // See if anything is selected.
  576. // ============================
  577. LRESULT nSel = SendMessage(m_hQualifierList, LB_GETCURSEL, 0, 0);
  578. if (nSel == LB_ERR)
  579. return;
  580. char buf[TEMP_BUF];
  581. *buf = 0;
  582. SendMessage(m_hQualifierList, LB_GETTEXT, nSel, LPARAM(buf));
  583. if (*buf == 0)
  584. return;
  585. // At this point, the text of the selected Qualifier is in <buf>.
  586. // ==============================================================
  587. char name[TEMP_BUF];
  588. *name = 0;
  589. if (sscanf(buf, "%[^\t\0]", name) == EOF)
  590. return;
  591. if (*name == 0)
  592. return;
  593. WString WName = name;
  594. // Remove the Qualifier.
  595. // =====================
  596. IWbemQualifierSet *pQualifierSet = m_pTarget->m_pQualifiers;
  597. CBString bsName(WName);
  598. HRESULT hres = pQualifierSet->Delete(bsName.GetString());
  599. if(FAILED(hres) || hres != 0)
  600. {
  601. FormatError(hres, m_hDlg);
  602. }
  603. Refresh();
  604. }
  605. void CTestPropertyEditor::Refresh()
  606. {
  607. // Zap the current contents.
  608. // =========================
  609. SendMessage(m_hQualifierList, LB_RESETCONTENT, 0, 0);
  610. SetCheck(IDC_KEY, BST_UNCHECKED);
  611. SetCheck(IDC_INDEXED, BST_UNCHECKED);
  612. SetCheck(IDC_NOT_NULL, BST_UNCHECKED);
  613. SetCheck(IDC_NORMAL, BST_CHECKED);
  614. // Fill in the Qualifier list.
  615. // ===========================
  616. IWbemQualifierSet *pQualifiers = m_pTarget->m_pQualifiers;
  617. if(pQualifiers == NULL)
  618. {
  619. EnableWindow(m_hQualifierList, FALSE);
  620. EnableWindow(GetDlgItem(IDC_KEY), FALSE);
  621. EnableWindow(GetDlgItem(IDC_INDEXED), FALSE);
  622. EnableWindow(GetDlgItem(IDC_NOT_NULL), FALSE);
  623. EnableWindow(GetDlgItem(IDC_NORMAL), FALSE);
  624. EnableWindow(GetDlgItem(IDC_ADD_ATTRIB), FALSE);
  625. EnableWindow(GetDlgItem(IDC_EDIT_ATTRIB), FALSE);
  626. EnableWindow(GetDlgItem(IDC_DELETE_ATTRIB), FALSE);
  627. EnableWindow(GetDlgItem(IDC_STATIC_QUAL), FALSE);
  628. }
  629. if (pQualifiers)
  630. {
  631. pQualifiers->BeginEnumeration(0);
  632. BSTR strName = NULL;
  633. long lFlavor;
  634. VARIANT vVal;
  635. VariantInit(&vVal);
  636. while(pQualifiers->Next(0, &strName, &vVal, &lFlavor) == S_OK)
  637. {
  638. if(!wbem_wcsicmp(strName, L"key"))
  639. {
  640. SetSystemCheck(IDC_KEY);
  641. SysFreeString(strName);
  642. strName = NULL;
  643. continue;
  644. }
  645. else if(!wbem_wcsicmp(strName, L"indexed"))
  646. {
  647. if(GetCheck(IDC_KEY) == BST_UNCHECKED)
  648. {
  649. SetSystemCheck(IDC_INDEXED);
  650. }
  651. SysFreeString(strName);
  652. strName = NULL;
  653. continue;
  654. }
  655. else if(!wbem_wcsicmp(strName, L"not_null"))
  656. {
  657. if(GetCheck(IDC_KEY) == BST_UNCHECKED &&
  658. GetCheck(IDC_INDEXED) == BST_UNCHECKED)
  659. {
  660. SetSystemCheck(IDC_NOT_NULL);
  661. }
  662. SysFreeString(strName);
  663. strName = NULL;
  664. continue;
  665. }
  666. CTestQualifier A;
  667. A.m_pName = new wchar_t[wcslen(strName) + 1];
  668. wcscpy(A.m_pName, strName);
  669. A.m_pValue = new CVar(&vVal);
  670. A.m_lType = lFlavor;
  671. // Build list box string.
  672. // ======================
  673. const char * stringValue = CTestQualifierToString(&A);
  674. SendMessage(m_hQualifierList, LB_ADDSTRING, 0,LPARAM(stringValue));
  675. delete[] stringValue;
  676. VariantClear(&vVal);
  677. SysFreeString(strName);
  678. strName = NULL;
  679. }
  680. pQualifiers->EndEnumeration();
  681. VariantClear(&vVal);
  682. }
  683. }
  684. BOOL CTestPropertyEditor::OnDoubleClick(int nID)
  685. {
  686. if(nID == IDC_ATTRIB_LIST)
  687. {
  688. OnEditQualifier();
  689. return TRUE;
  690. }
  691. return FALSE;
  692. }
  693. BOOL CTestPropertyEditor::OnSelChange(int nID)
  694. {
  695. if(nID == IDC_TYPE_LIST)
  696. {
  697. char* pszType = GetCBCurSelString(IDC_TYPE_LIST);
  698. BOOL bArray = (GetCheck(IDC_ARRAY) == BST_CHECKED);
  699. m_pTarget->m_lType = StringToType(pszType);
  700. if(bArray)
  701. m_pTarget->m_lType |= VT_ARRAY;
  702. if((m_pTarget->m_lType & ~VT_ARRAY) == VT_EMBEDDED_OBJECT)
  703. {
  704. ShowWindow(GetDlgItem(IDC_EMBEDDING), SW_SHOW);
  705. }
  706. else
  707. {
  708. ShowWindow(GetDlgItem(IDC_EMBEDDING), SW_HIDE);
  709. }
  710. delete [] pszType;
  711. }
  712. return TRUE;
  713. }
  714. BOOL CTestPropertyEditor::OnCommand(WORD wCode, WORD wID)
  715. {
  716. switch(wID)
  717. {
  718. case IDC_EDIT_ATTRIB: OnEditQualifier(); return TRUE;
  719. case IDC_ADD_ATTRIB: OnAddQualifier(); return TRUE;
  720. case IDC_DELETE_ATTRIB: OnDelQualifier(); return TRUE;
  721. case IDC_KEY:
  722. if(wCode == BN_CLICKED)
  723. OnKey();
  724. return TRUE;
  725. case IDC_INDEXED:
  726. if(wCode == BN_CLICKED)
  727. OnIndexed();
  728. return TRUE;
  729. case IDC_NOT_NULL:
  730. if(wCode == BN_CLICKED)
  731. OnNotNull();
  732. return TRUE;
  733. case IDC_NORMAL:
  734. if(wCode == BN_CLICKED)
  735. RemoveSysQuals();
  736. return TRUE;
  737. case IDC_VALUE_NULL:
  738. OnValueNull();
  739. return TRUE;
  740. case IDC_VALUE_NOT_NULL:
  741. OnValueNotNull();
  742. return TRUE;
  743. case IDC_EMBEDDING:
  744. ViewEmbedding();
  745. return TRUE;
  746. case IDC_ARRAY:
  747. OnArray();
  748. return TRUE;
  749. }
  750. return TRUE;
  751. }
  752. void CTestPropertyEditor::OnArray()
  753. {
  754. if(GetCheck(IDC_ARRAY) == BST_CHECKED)
  755. m_pTarget->m_lType |= VT_ARRAY;
  756. else
  757. m_pTarget->m_lType &= ~VT_ARRAY;
  758. }
  759. void CTestPropertyEditor::OnValueNull()
  760. {
  761. if((m_pTarget->m_lType & ~VT_ARRAY) == VT_EMBEDDED_OBJECT)
  762. {
  763. if(MessageBox(IDS_SAVE_EMBEDDING, IDS_WARNING,
  764. MB_ICONQUESTION | MB_YESNO) == IDYES)
  765. {
  766. SetCheck(IDC_VALUE_NULL, BST_UNCHECKED);
  767. SetCheck(IDC_VALUE_NOT_NULL, BST_CHECKED);
  768. return;
  769. }
  770. else
  771. {
  772. delete m_pTarget->m_pValue;
  773. m_pTarget->m_pValue = NULL;
  774. SetDlgItemText(IDC_VALUE, "");
  775. }
  776. }
  777. EnableWindow(m_hValue, FALSE);
  778. EnableWindow(GetDlgItem(IDC_EMBEDDING), FALSE);
  779. }
  780. void CTestPropertyEditor::OnValueNotNull()
  781. {
  782. if((m_pTarget->m_lType & ~VT_ARRAY) != VT_EMBEDDED_OBJECT)
  783. {
  784. EnableWindow(m_hValue, TRUE);
  785. }
  786. EnableWindow(GetDlgItem(IDC_EMBEDDING), TRUE);
  787. }
  788. BOOL CTestPropertyEditor::OnInitDialog()
  789. {
  790. ShowWindow(GetDlgItem(IDC_EMBEDDING), SW_HIDE);
  791. CenterOnParent();
  792. m_hPropName = GetDlgItem(IDC_PROPNAME);
  793. m_hPropType = GetDlgItem(IDC_TYPE_LIST);
  794. m_hValue = GetDlgItem(IDC_VALUE);
  795. m_hQualifierList = GetDlgItem(IDC_ATTRIB_LIST);
  796. LONG Tabs[] = { 80, 140, 170 };
  797. int TabCount = 3;
  798. SendMessage(m_hQualifierList, LB_SETTABSTOPS,
  799. (WPARAM) TabCount, (LPARAM) Tabs);
  800. // Populate the combo box with the valid prop types
  801. // ================================================
  802. for (int i = 0; i < g_nNumValidPropTypes; i++)
  803. SendMessage(m_hPropType, CB_ADDSTRING, 0, LPARAM(g_aValidPropTypes[i]));
  804. SendMessage(m_hPropType, CB_SELECTSTRING, WPARAM(-1), LPARAM("CIM_STRING"));
  805. // Now initialize the controls with the contents of the
  806. // current object, if any.
  807. // ====================================================
  808. if (m_pTarget->m_pName)
  809. {
  810. SetWindowText(m_hPropName, LPWSTRToLPSTR(m_pTarget->m_pName));
  811. }
  812. if(m_pTarget->m_pClass)
  813. {
  814. SetDlgItemText(IDC_ORIGIN, LPWSTRToLPSTR(m_pTarget->m_pClass));
  815. }
  816. // If the value is available, set the text and select.
  817. // ===================================================
  818. if (m_pTarget->m_pValue)
  819. {
  820. LPSTR pTypeStr = TypeToString(m_pTarget->m_lType & ~VT_ARRAY);
  821. SendMessage(m_hPropType, CB_SELECTSTRING, WPARAM(-1), LPARAM(pTypeStr));
  822. if(m_pTarget->m_lType & VT_ARRAY)
  823. SetCheck(IDC_ARRAY, BST_CHECKED);
  824. else
  825. SetCheck(IDC_ARRAY, BST_UNCHECKED);
  826. if((m_pTarget->m_lType & ~VT_ARRAY) == VT_EMBEDDED_OBJECT)
  827. {
  828. ShowWindow(GetDlgItem(IDC_EMBEDDING), SW_SHOW);
  829. }
  830. if(m_pTarget->m_pValue->IsNull())
  831. {
  832. SetCheck(IDC_VALUE_NULL, BST_CHECKED);
  833. EnableWindow(m_hValue, FALSE);
  834. EnableWindow(GetDlgItem(IDC_EMBEDDING), FALSE);
  835. }
  836. else
  837. {
  838. SetCheck(IDC_VALUE_NOT_NULL, BST_CHECKED);
  839. LPSTR pValStr = ValueToNewString(m_pTarget->m_pValue,
  840. m_pTarget->m_lType);
  841. SetWindowText(m_hValue, pValStr);
  842. delete [] pValStr;
  843. EnableWindow(m_hValue, strstr(pTypeStr, "VT_EMBEDDED_OBJECT")
  844. == NULL);
  845. }
  846. }
  847. else
  848. {
  849. SetCheck(IDC_VALUE_NULL, BST_CHECKED);
  850. SendMessage(m_hPropType, CB_SELECTSTRING, WPARAM(-1), LPARAM("VT_BSTR"));
  851. EnableWindow(m_hValue, FALSE);
  852. EnableWindow(GetDlgItem(IDC_EMBEDDING), FALSE);
  853. }
  854. // Refresh the Qualifiers.
  855. // =======================
  856. Refresh();
  857. // If editing, don't allow type/name change.
  858. // =========================================
  859. if (m_bEditOnly)
  860. {
  861. EnableWindow(m_hPropName, FALSE);
  862. EnableWindow(m_hPropType, FALSE);
  863. }
  864. if(m_bInstance)
  865. {
  866. EnableWindow(GetDlgItem(IDC_KEY), FALSE);
  867. EnableWindow(GetDlgItem(IDC_INDEXED), FALSE);
  868. EnableWindow(GetDlgItem(IDC_NOT_NULL), FALSE);
  869. EnableWindow(GetDlgItem(IDC_NORMAL), FALSE);
  870. }
  871. return TRUE;
  872. }
  873. void CTestPropertyEditor::ViewEmbedding()
  874. {
  875. if(m_pTarget->m_lType == VT_EMBEDDED_OBJECT)
  876. {
  877. if(m_pTarget->m_pValue != NULL &&
  878. m_pTarget->m_pValue->GetType() != VT_EMBEDDED_OBJECT)
  879. {
  880. delete m_pTarget->m_pValue;
  881. m_pTarget->m_pValue = NULL;
  882. }
  883. if(m_pTarget->m_pValue == NULL)
  884. {
  885. m_pTarget->m_pValue = new CVar;
  886. m_pTarget->m_pValue->SetEmbeddedObject(NULL);
  887. }
  888. IWbemClassObject* pCurrentEmbed =
  889. (IWbemClassObject*)m_pTarget->m_pValue->GetEmbeddedObject();
  890. IWbemClassObject* pEmbed;
  891. if(pCurrentEmbed == NULL)
  892. {
  893. pEmbed = PreCreateInstance(m_hDlg, m_lGenFlags, m_lSync, m_lTimeout);
  894. if(pEmbed == NULL) return;
  895. }
  896. else
  897. {
  898. pCurrentEmbed->Clone(&pEmbed);
  899. pCurrentEmbed->Release();
  900. }
  901. CObjectEditor ed(m_hDlg, m_lGenFlags, CObjectEditor::readwrite, m_lSync,
  902. pEmbed, m_lTimeout);
  903. if(ed.Edit() == IDOK)
  904. {
  905. m_pTarget->m_pValue->SetEmbeddedObject(pEmbed);
  906. SetDlgItemText(IDC_VALUE, "<embedded object>");
  907. }
  908. pEmbed->Release();
  909. }
  910. else if(m_pTarget->m_lType == (VT_EMBEDDED_OBJECT | VT_ARRAY))
  911. {
  912. if(m_pTarget->m_pValue != NULL &&
  913. m_pTarget->m_pValue->GetType() != VT_EX_CVARVECTOR)
  914. {
  915. delete m_pTarget->m_pValue;
  916. m_pTarget->m_pValue = NULL;
  917. }
  918. if(m_pTarget->m_pValue == NULL)
  919. {
  920. m_pTarget->m_pValue = new CVar;
  921. CVarVector* pvv = new CVarVector(VT_EMBEDDED_OBJECT);
  922. m_pTarget->m_pValue->SetVarVector(pvv, TRUE);
  923. }
  924. CVarVector* pCurrentEmbed = m_pTarget->m_pValue->GetVarVector();
  925. CEmbeddedObjectListEditor ed(m_hDlg, m_lGenFlags, 0, m_pTarget->m_pName,
  926. pCurrentEmbed);
  927. // Pass on invocation method (sync, async..) related settings for use
  928. // by any further operations (editing/deleting/etc. of an instance).
  929. ed.SetCallMethod(m_lSync);
  930. ed.SetTimeout(m_lTimeout);
  931. ed.Run();
  932. SetDlgItemText(IDC_VALUE, "<array of embedded objects>");
  933. }
  934. else
  935. {
  936. ShowWindow(GetDlgItem(IDC_EMBEDDING), SW_HIDE);
  937. }
  938. }
  939. BOOL CTestPropertyEditor::Verify()
  940. {
  941. // Buffer is used for property name, value string (which can be long if an array), and type string).
  942. // Find the largest of the three for buffer length (TEMP_BUF size is used for type).
  943. int buflen = max(max(GetWindowTextLength(m_hPropName), GetWindowTextLength(m_hValue)) + 1, TEMP_BUF);
  944. char* buf = new char[buflen];
  945. // Verify that name is present.
  946. // ============================
  947. if (GetWindowText(m_hPropName, buf, buflen) == 0)
  948. {
  949. MessageBox(IDS_NO_PROPERTY_NAME, IDS_ERROR, MB_OK);
  950. delete [] buf;
  951. return FALSE;
  952. }
  953. StripTrailingWs(buf);
  954. WString Name = buf;
  955. if (m_pTarget->m_pName)
  956. delete m_pTarget->m_pName;
  957. m_pTarget->m_pName = new wchar_t[wcslen(Name) + 1];
  958. wcscpy(m_pTarget->m_pName, Name);
  959. // Get the type.
  960. // =============
  961. LRESULT nIndex = SendMessage(m_hPropType, CB_GETCURSEL, 0, 0);
  962. if (SendMessage(m_hPropType, CB_GETLBTEXT, nIndex, LPARAM(buf)) == CB_ERR)
  963. {
  964. MessageBox(IDS_INVALID_PROPERTY_TYPE, IDS_ERROR, MB_OK);
  965. delete [] buf;
  966. return FALSE;
  967. }
  968. // Convert type string to a type.
  969. // ==============================
  970. int nType = StringToType(buf);
  971. if(GetCheck(IDC_ARRAY) == BST_CHECKED)
  972. nType |= VT_ARRAY;
  973. if (nType == 0)
  974. {
  975. MessageBox(IDS_INVALID_PROPERTY_TYPE, IDS_ERROR, MB_OK);
  976. delete [] buf;
  977. return FALSE;
  978. }
  979. m_pTarget->m_lType = nType;
  980. // Verify that Value is present.
  981. // =============================
  982. CVar *p;
  983. if(GetCheck(IDC_VALUE_NULL) == BST_CHECKED)
  984. {
  985. p = new CVar;
  986. p->SetAsNull();
  987. }
  988. else
  989. {
  990. *buf = 0;
  991. GetWindowText(m_hValue, buf, buflen);
  992. StripTrailingWs(buf);
  993. // Convert value string to the correct value.
  994. // ==========================================
  995. if((nType & ~VT_ARRAY) != VT_EMBEDDED_OBJECT)
  996. {
  997. p = StringToValue(buf, nType);
  998. }
  999. else
  1000. {
  1001. // otherwise already there
  1002. p = m_pTarget->m_pValue;
  1003. }
  1004. }
  1005. if (!p)
  1006. {
  1007. MessageBox(IDS_INVALID_PROPERTY_VALUE, IDS_ERROR, MB_OK);
  1008. delete [] buf;
  1009. return FALSE;
  1010. }
  1011. if(m_pTarget->m_pValue != p)
  1012. {
  1013. if (m_pTarget->m_pValue)
  1014. delete m_pTarget->m_pValue;
  1015. m_pTarget->m_pValue = p;
  1016. }
  1017. delete [] buf;
  1018. return TRUE;
  1019. }
  1020. CTestPropertyEditor::CTestPropertyEditor(HWND hParent, LONG lGenFlags, BOOL bEditOnly, LONG lSync,
  1021. CTestProperty* pProp, BOOL bInstance, LONG lTimeout)
  1022. : CWbemDialog(IDD_PROPERTY_EDITOR, hParent)
  1023. {
  1024. m_pTarget = pProp;
  1025. m_lGenFlags = lGenFlags;
  1026. m_bEditOnly = bEditOnly;
  1027. m_lSync = lSync;
  1028. m_bInstance = bInstance;
  1029. m_lTimeout = lTimeout;
  1030. }
  1031. INT_PTR CTestPropertyEditor::Edit()
  1032. {
  1033. return Run();
  1034. }
  1035. void CObjectEditor::OnAddQualifier()
  1036. {
  1037. CTestQualifier att;
  1038. att.m_lType = WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE |
  1039. WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS;
  1040. CTestQualifierEditor ed(m_hDlg, &att, FALSE);
  1041. INT_PTR nRes = ed.Edit();
  1042. if ((nRes == IDCANCEL) || (nRes == 0))
  1043. return;
  1044. // If here, the Qualifier is being added.
  1045. // ======================================
  1046. IWbemQualifierSet* pQualifierSet = 0;
  1047. m_pObj->GetQualifierSet(&pQualifierSet);
  1048. VARIANT *p = att.m_pValue->GetNewVariant();
  1049. CBString bsName(att.m_pName);
  1050. /*
  1051. DWORD dwFlavor =
  1052. WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE |
  1053. WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS;
  1054. */
  1055. HRESULT hres = pQualifierSet->Put(bsName.GetString(), p, att.m_lType);
  1056. if(FAILED(hres))
  1057. {
  1058. FormatError(hres, m_hDlg);
  1059. }
  1060. VariantClear(p);
  1061. pQualifierSet->Release();
  1062. Refresh();
  1063. }
  1064. void CObjectEditor::OnEditQualifier()
  1065. {
  1066. // See if anything is selected.
  1067. // ============================
  1068. LRESULT nSel = SendMessage(m_hQualifierList, LB_GETCURSEL, 0, 0);
  1069. if (nSel == LB_ERR)
  1070. return;
  1071. int sizeRequired = SendMessage(m_hQualifierList, LB_GETTEXTLEN, nSel, 0);
  1072. wmilib::auto_buffer<char> buf(new char[sizeRequired+1]);
  1073. if (buf.get() == 0)
  1074. return;
  1075. *buf = 0;
  1076. SendMessage(m_hQualifierList, LB_GETTEXT, nSel, LPARAM(buf.get()));
  1077. if (*buf == 0)
  1078. return;
  1079. // At this point, the text of the selected Qualifier is in <buf>.
  1080. // ==============================================================
  1081. char name[TEMP_BUF];
  1082. *name = 0;
  1083. if (sscanf(buf.get(), "%[^\t\0]", name) == EOF)
  1084. return;
  1085. if (*name == 0)
  1086. return;
  1087. WString WName = name;
  1088. // Find the Qualifier in question.
  1089. // ===============================
  1090. IWbemQualifierSet* pQualifierSet = 0;
  1091. VARIANT v;
  1092. VariantInit(&v);
  1093. LONG lType = 0;
  1094. m_pObj->GetQualifierSet(&pQualifierSet);
  1095. SCODE res = pQualifierSet->Get(WName, 0, &v, &lType);
  1096. if (res != 0)
  1097. {
  1098. MessageBox(IDS_QUALIFIER_NOT_FOUND, IDS_CRITICAL_ERROR, MB_OK);
  1099. pQualifierSet->Release();
  1100. return;
  1101. }
  1102. // If here, convert temporarily to a CTestQualifier object for the duration of
  1103. // the edit.
  1104. // ====================================================================
  1105. CVar *pNewVal = new CVar;
  1106. pNewVal->SetVariant(&v);
  1107. VariantClear(&v);
  1108. CTestQualifier att;
  1109. att.m_pValue = pNewVal;
  1110. att.m_pName = new wchar_t[wcslen(WName) + 1];
  1111. wcscpy(att.m_pName, WName);
  1112. att.m_lType = lType;
  1113. CTestQualifierEditor ed(m_hDlg, &att, TRUE);
  1114. INT_PTR nRes = ed.Edit();
  1115. if ((nRes == IDCANCEL) || (nRes == 0))
  1116. {
  1117. pQualifierSet->Release();
  1118. return;
  1119. }
  1120. // If here, the Qualifier is being added.
  1121. // ======================================
  1122. m_pObj->GetQualifierSet(&pQualifierSet);
  1123. VARIANT *p = att.m_pValue->GetNewVariant();
  1124. CBString bsName(att.m_pName);
  1125. res = pQualifierSet->Put(bsName.GetString(), p, att.m_lType);
  1126. if(FAILED(res))
  1127. {
  1128. FormatError(res, m_hDlg);
  1129. }
  1130. VariantClear(p);
  1131. pQualifierSet->Release();
  1132. Refresh();
  1133. }
  1134. //
  1135. //
  1136. // Called when deleting an Qualifier on the object itself.
  1137. //
  1138. void CObjectEditor::OnDelQualifier()
  1139. {
  1140. // See if anything is selected.
  1141. // ============================
  1142. LRESULT nSel = SendMessage(m_hQualifierList, LB_GETCURSEL, 0, 0);
  1143. if (nSel == LB_ERR)
  1144. return;
  1145. char buf[TEMP_BUF];
  1146. *buf = 0;
  1147. SendMessage(m_hQualifierList, LB_GETTEXT, nSel, LPARAM(buf));
  1148. if (*buf == 0)
  1149. return;
  1150. // At this point, the text of the selected Qualifier is in <buf>.
  1151. // ==============================================================
  1152. char name[TEMP_BUF];
  1153. *name = 0;
  1154. if (sscanf(buf, "%[^\t\0]", name) == EOF)
  1155. return;
  1156. if (*name == 0)
  1157. return;
  1158. WString WName = name;
  1159. // Remove the Qualifier.
  1160. // =====================
  1161. IWbemQualifierSet *pQualifierSet;
  1162. m_pObj->GetQualifierSet(&pQualifierSet);
  1163. HRESULT hres = pQualifierSet->Delete(WName);
  1164. if(FAILED(hres) || hres != 0)
  1165. {
  1166. FormatError(hres, m_hDlg);
  1167. }
  1168. pQualifierSet->Release();
  1169. Refresh();
  1170. }
  1171. void CObjectEditor::OnAddProp()
  1172. {
  1173. HRESULT hres;
  1174. // Add a dummy property for now
  1175. // ============================
  1176. VARIANT v;
  1177. VariantInit(&v);
  1178. V_VT(&v) = VT_I4;
  1179. V_I4(&v) = 1;
  1180. CBString bsTemp(L"DUMMY_PROPERTY__D");
  1181. if(FAILED(m_pObj->Put(bsTemp.GetString(), 0, &v, 0)))
  1182. {
  1183. MessageBox(NULL, IDS_CANNOT_ADD_PROPERTIES, IDS_ERROR, MB_OK|MB_ICONSTOP);
  1184. return;
  1185. }
  1186. IWbemQualifierSet* pQualifierSet;
  1187. m_pObj->GetPropertyQualifierSet(bsTemp.GetString(), &pQualifierSet);
  1188. // Create CTestProperty with the dummy attr set for now
  1189. // ================================================
  1190. CTestProperty prop(pQualifierSet);
  1191. VariantClear(&v);
  1192. hres = m_pObj->Get(L"__CLASS", 0, &v, NULL, NULL);
  1193. if (hres != S_OK || V_VT(&v) != VT_BSTR )
  1194. {
  1195. prop.m_pClass = new wchar_t[wcslen(L"Unknown") + 1];
  1196. wcscpy(prop.m_pClass, L"Unknown");
  1197. }
  1198. else
  1199. {
  1200. prop.m_pClass = new wchar_t[wcslen(V_BSTR(&v)) + 1];
  1201. wcscpy(prop.m_pClass, V_BSTR(&v));
  1202. VariantClear(&v);
  1203. }
  1204. CTestPropertyEditor ed(m_hDlg, m_lGenFlags, FALSE, m_lSync, &prop, FALSE,
  1205. m_lTimeout);
  1206. INT_PTR nRes = ed.Edit();
  1207. if ((nRes == IDCANCEL) || (nRes == 0))
  1208. {
  1209. pQualifierSet->Release();
  1210. m_pObj->Delete(bsTemp.GetString());
  1211. return;
  1212. }
  1213. // Set the property
  1214. // ================
  1215. VARIANT* pVariant = prop.m_pValue->GetNewVariant();
  1216. bsTemp = prop.m_pName;
  1217. VARTYPE vtType;
  1218. if(m_bClass)
  1219. {
  1220. vtType = (VARTYPE)prop.m_lType;
  1221. }
  1222. else
  1223. {
  1224. vtType = 0;
  1225. }
  1226. hres = m_pObj->Put(bsTemp.GetString(), 0, pVariant, vtType);
  1227. VariantClear(pVariant);
  1228. if(FAILED(hres))
  1229. {
  1230. FormatError(hres, m_hDlg);
  1231. return;
  1232. }
  1233. // Copy the Qualifiers
  1234. // ===================
  1235. IWbemQualifierSet* pRealQualifierSet;
  1236. bsTemp = prop.m_pName;
  1237. m_pObj->GetPropertyQualifierSet(bsTemp.GetString(), &pRealQualifierSet);
  1238. CopyQualifierSet(pRealQualifierSet, pQualifierSet, m_hDlg);
  1239. pQualifierSet->EndEnumeration();
  1240. pQualifierSet->Release();
  1241. pRealQualifierSet->Release();
  1242. bsTemp = L"DUMMY_PROPERTY__D";
  1243. m_pObj->Delete(bsTemp.GetString());
  1244. Refresh();
  1245. }
  1246. void CObjectEditor::OnEditProp()
  1247. {
  1248. // Find out which property is selected.
  1249. // ====================================
  1250. LRESULT nIndex = SendMessage(m_hPropList, LB_GETCURSEL, 0, 0);
  1251. if (nIndex == LB_ERR)
  1252. return;
  1253. char buf[TEMP_BUF];
  1254. *buf = 0;
  1255. SendMessage(m_hPropList, LB_GETTEXT, nIndex, LPARAM(buf));
  1256. if (*buf == 0)
  1257. return;
  1258. // Scan out the property name.
  1259. // ===========================
  1260. char name[TEMP_BUF];
  1261. *name = 0;
  1262. if (sscanf(buf, "%[^\t\0]", name) == EOF)
  1263. return;
  1264. if (*name == 0)
  1265. return;
  1266. WString wsName = name;
  1267. // Get the property value from the object
  1268. // ======================================
  1269. VARIANT vVal;
  1270. CIMTYPE ctType;
  1271. m_pObj->Get((LPWSTR)wsName, 0, &vVal, &ctType, NULL);
  1272. // Create a CTestProperty from it
  1273. // ==========================
  1274. IWbemQualifierSet* pQualifierSet = 0;
  1275. SCODE sc = m_pObj->GetPropertyQualifierSet((LPWSTR)wsName, &pQualifierSet);
  1276. CTestProperty Copy(pQualifierSet);
  1277. if (pQualifierSet)
  1278. pQualifierSet->Release();
  1279. Copy.m_pName = new wchar_t[wcslen(wsName) + 1];
  1280. wcscpy(Copy.m_pName, wsName);
  1281. Copy.m_pValue = new CVar(&vVal);
  1282. Copy.m_lType = ctType;
  1283. // Note that GetPropertyOrigin can fail for objects returned by Projection
  1284. // Queries, so we need to be careful with strClass.
  1285. BSTR strClass = NULL;
  1286. m_pObj->GetPropertyOrigin((LPWSTR)wsName, &strClass);
  1287. if ( NULL != strClass )
  1288. {
  1289. Copy.m_pClass = new wchar_t[wcslen(strClass)+1];
  1290. wcscpy(Copy.m_pClass, strClass);
  1291. SysFreeString(strClass);
  1292. }
  1293. // Edit it.
  1294. // =========
  1295. CTestPropertyEditor ed(m_hDlg, m_lGenFlags, TRUE, m_lSync, &Copy, !m_bClass,
  1296. m_lTimeout);
  1297. INT_PTR nRes = ed.Edit();
  1298. if ((nRes == IDCANCEL) || (nRes == 0))
  1299. return;
  1300. // If here, we must replace the info for the property.
  1301. // ===================================================
  1302. VARIANT* pVariant = Copy.m_pValue->GetNewVariant();
  1303. if(m_bClass && V_VT(pVariant) == VT_NULL)
  1304. ctType = (VARTYPE)Copy.m_lType;
  1305. else
  1306. ctType = 0;
  1307. sc = m_pObj->Put(Copy.m_pName, 0, pVariant, ctType);
  1308. VariantClear(pVariant);
  1309. if(FAILED(sc))
  1310. {
  1311. FormatError(sc, m_hDlg);
  1312. }
  1313. Refresh();
  1314. }
  1315. void CObjectEditor::OnDelProp()
  1316. {
  1317. // Find out which property is selected.
  1318. // ====================================
  1319. LRESULT nIndex = SendMessage(m_hPropList, LB_GETCURSEL, 0, 0);
  1320. if (nIndex == LB_ERR)
  1321. return;
  1322. char buf[TEMP_BUF];
  1323. *buf = 0;
  1324. SendMessage(m_hPropList, LB_GETTEXT, nIndex, LPARAM(buf));
  1325. if (*buf == 0)
  1326. return;
  1327. // Scan out the property name.
  1328. // ===========================
  1329. char name[TEMP_BUF];
  1330. *name = 0;
  1331. if (sscanf(buf, "%[^\t\0]", name) == EOF)
  1332. return;
  1333. if (*name == 0)
  1334. return;
  1335. WString WName = name;
  1336. if(FAILED(m_pObj->Delete(LPWSTR(WName))))
  1337. {
  1338. MessageBox(NULL, IDS_CANNOT_EDIT_PROPERTY, IDS_ERROR,
  1339. MB_OK|MB_ICONSTOP);
  1340. return;
  1341. }
  1342. Refresh();
  1343. }
  1344. class CMofViewer : public CWbemDialog
  1345. {
  1346. BSTR m_strText;
  1347. public:
  1348. CMofViewer(HWND hParent, BSTR strText)
  1349. : CWbemDialog(IDD_MOF, hParent), m_strText(strText)
  1350. {}
  1351. BOOL OnInitDialog();
  1352. };
  1353. BOOL CMofViewer::OnInitDialog()
  1354. {
  1355. int iSize = wcslen(m_strText)*2+1;
  1356. char* szText = new char[iSize];
  1357. wcstombs(szText, m_strText, iSize);
  1358. char* szText1 = new char[strlen(szText)*2+1];
  1359. char* pc = szText;
  1360. char* pc1 = szText1;
  1361. while(*pc)
  1362. {
  1363. if(*pc == '\n')
  1364. {
  1365. *(pc1++) = '\r';
  1366. }
  1367. *(pc1++) = *(pc++);
  1368. }
  1369. *pc1 = 0;
  1370. SetDlgItemText(IDC_MOF, szText1);
  1371. delete [] szText;
  1372. return TRUE;
  1373. }
  1374. void CObjectEditor::OnShowMof()
  1375. {
  1376. BSTR strText;
  1377. HRESULT hres = m_pObj->GetObjectText(0, &strText);
  1378. if(FAILED(hres))
  1379. {
  1380. MessageBox(IDS_MOF_FAILED, IDS_ERROR, MB_OK);
  1381. }
  1382. else
  1383. {
  1384. CMofViewer mv(m_hDlg, strText);
  1385. mv.Run();
  1386. }
  1387. }
  1388. void CObjectEditor::OnRefreshObject()
  1389. {
  1390. #if 0
  1391. BSTR strText;
  1392. HRESULT res;
  1393. // Asynchronous
  1394. if (m_lSync & ASYNC)
  1395. {
  1396. MessageBox(IDS_ASYNC_NOT_SUPPORTED, IDS_ERROR, MB_OK);
  1397. return;
  1398. }
  1399. else if (m_lSync & SEMISYNC)
  1400. {
  1401. IWbemCallResultEx* pCallRes = NULL;
  1402. CHourGlass hg;
  1403. res = g_pServicesEx->RefreshObject(&m_pObj,
  1404. m_lGenFlags | WBEM_FLAG_RETURN_IMMEDIATELY,
  1405. g_Context, &pCallRes);
  1406. if (SUCCEEDED(res))
  1407. {
  1408. LONG lStatus;
  1409. SetInterfaceSecurityEx(pCallRes, gpAuthIdentity, gpPrincipal, gdwAuthLevel, gdwImpLevel);
  1410. while ((res = pCallRes->GetCallStatus(m_lTimeout, &lStatus)) == WBEM_S_TIMEDOUT)
  1411. {
  1412. // wait
  1413. }
  1414. if (res == WBEM_S_NO_ERROR)
  1415. {
  1416. res = (HRESULT)lStatus; // lStatus is the final result of the above IWbemServices::PutClass call
  1417. }
  1418. pCallRes->Release();
  1419. }
  1420. }
  1421. // Synchronous
  1422. else
  1423. {
  1424. CHourGlass hg;
  1425. res = g_pServicesEx->RefreshObject(&m_pObj,
  1426. m_lGenFlags ,
  1427. g_Context, NULL);
  1428. }
  1429. HRESULT hres = g_pServicesEx->RefreshObject(&m_pObj, 0, NULL, NULL );
  1430. if(FAILED(hres))
  1431. {
  1432. FormatError(hres, m_hDlg, NULL);
  1433. }
  1434. else
  1435. Refresh();
  1436. #endif
  1437. HRESULT res;
  1438. if (m_pObj)
  1439. {
  1440. VARIANT Var;
  1441. VariantInit(&Var);
  1442. res = m_pObj->Get(L"__RELPATH",0,&Var,NULL,NULL);
  1443. if (SUCCEEDED(res))
  1444. {
  1445. if (VT_BSTR == V_VT(&Var))
  1446. {
  1447. IWbemClassObject * pObj = NULL;
  1448. res = g_pNamespace->GetObject(V_BSTR(&Var),
  1449. m_lGenFlags ,
  1450. g_Context,
  1451. &pObj,
  1452. NULL);
  1453. if (SUCCEEDED(res))
  1454. {
  1455. m_pObj->Release();
  1456. m_pObj = pObj;
  1457. }
  1458. else
  1459. {
  1460. FormatError(res, m_hDlg, NULL);
  1461. }
  1462. }
  1463. else
  1464. {
  1465. FormatError(WBEM_E_INVALID_OBJECT, m_hDlg, NULL);
  1466. }
  1467. }
  1468. else
  1469. {
  1470. FormatError(res, m_hDlg, NULL);
  1471. }
  1472. VariantClear(&Var);
  1473. }
  1474. }
  1475. BOOL CObjectEditor::OnInitDialog()
  1476. {
  1477. CenterOnParent();
  1478. m_hPropList = GetDlgItem(IDC_PROP_LIST);
  1479. m_hQualifierList = GetDlgItem(IDC_ATTRIB_LIST);
  1480. m_hMethodList = GetDlgItem(IDC_METHOD_LIST);
  1481. // Set tabs in the property list box.
  1482. // ==================================
  1483. LONG Tabs[] = { 80, 140, 170 };
  1484. int TabCount = 3;
  1485. SendMessage(m_hPropList, LB_SETTABSTOPS,
  1486. (WPARAM) TabCount, (LPARAM) Tabs);
  1487. SendMessage(m_hPropList, LB_SETHORIZONTALEXTENT, 1000, 0);
  1488. SendMessage(m_hQualifierList, LB_SETTABSTOPS,
  1489. (WPARAM) TabCount, (LPARAM) Tabs);
  1490. SendMessage(m_hQualifierList, LB_SETHORIZONTALEXTENT, 1000, 0);
  1491. SendMessage(m_hMethodList, LB_SETTABSTOPS,
  1492. (WPARAM) TabCount, (LPARAM) Tabs);
  1493. SendMessage(m_hMethodList, LB_SETHORIZONTALEXTENT, 1000, 0);
  1494. if (m_dwEditMode == readonly)
  1495. {
  1496. EnableWindow(GetDlgItem(IDOK), FALSE);
  1497. }
  1498. if (m_bClass)
  1499. {
  1500. SetCheck(IDC_UPDATE_NORMAL, TRUE);
  1501. SetCheck(IDC_UPDATE_COMPATIBLE, TRUE);
  1502. }
  1503. else
  1504. {
  1505. SetCheck(IDC_UPDATE_NORMAL, TRUE);
  1506. EnableWindow(GetDlgItem(IDC_UPDATE_COMPATIBLE), FALSE);
  1507. EnableWindow(GetDlgItem(IDC_UPDATE_SAFE), FALSE);
  1508. EnableWindow(GetDlgItem(IDC_UPDATE_FORCE), FALSE);
  1509. }
  1510. Refresh();
  1511. return TRUE;
  1512. }
  1513. BOOL CObjectEditor::OnDoubleClick(int nID)
  1514. {
  1515. if(nID == IDC_ATTRIB_LIST)
  1516. {
  1517. OnEditQualifier();
  1518. return TRUE;
  1519. }
  1520. else if(nID == IDC_PROP_LIST)
  1521. {
  1522. OnEditProp();
  1523. return TRUE;
  1524. }
  1525. else if(nID == IDC_METHOD_LIST)
  1526. {
  1527. OnEditMethod();
  1528. return TRUE;
  1529. }
  1530. else return FALSE;
  1531. }
  1532. BOOL CObjectEditor::OnCommand(WORD wCode, WORD wID)
  1533. {
  1534. switch(wID)
  1535. {
  1536. case IDC_EDIT_ATTRIB: OnEditQualifier(); return TRUE;
  1537. case IDC_ADD_ATTRIB: OnAddQualifier(); return TRUE;
  1538. case IDC_DELETE_ATTRIB: OnDelQualifier(); return TRUE;
  1539. case IDC_SHOW_MOF: OnShowMof(); return TRUE;
  1540. case IDC_ADD_PROP: OnAddProp(); return TRUE;
  1541. case IDC_EDIT_PROP: OnEditProp(); return TRUE;
  1542. case IDC_DELETE_PROP: OnDelProp(); return TRUE;
  1543. case IDC_ADD_METHOD: OnAddMethod(); return TRUE;
  1544. case IDC_EDIT_METHOD: OnEditMethod(); return TRUE;
  1545. case IDC_DELETE_METHOD: OnDelMethod(); return TRUE;
  1546. case IDC_REFRESH_OBJECT: OnRefreshObject(); return TRUE;
  1547. case IDC_SUPERCLASS:
  1548. if(m_bClass) OnSuperclass();
  1549. else OnClass();
  1550. return TRUE;
  1551. case IDC_DERIVED:
  1552. if(m_bClass) OnDerived();
  1553. else OnRefs();
  1554. return TRUE;
  1555. case IDC_INSTANCES:
  1556. if(m_bClass) OnInstances();
  1557. else OnAssocs();
  1558. return TRUE;
  1559. case IDC_HIDE_SYSTEM:
  1560. OnHideSystem();
  1561. return TRUE;
  1562. case IDC_HIDE_DERIVED:
  1563. OnHideDerived();
  1564. return TRUE;
  1565. }
  1566. return FALSE;
  1567. }
  1568. void CObjectEditor::OnSuperclass()
  1569. {
  1570. // Get the superclass
  1571. // ==================
  1572. VARIANT v;
  1573. VariantInit(&v);
  1574. if(FAILED(m_pObj->Get(L"__SUPERCLASS", 0, &v, NULL, NULL)) ||
  1575. V_VT(&v) != VT_BSTR)
  1576. {
  1577. MessageBox(IDS_NO_SUPERCLASS, IDS_ERROR, MB_OK);
  1578. return;
  1579. }
  1580. ShowClass(m_hDlg, m_lGenFlags, V_BSTR(&v), m_lSync, m_pOwner, m_lTimeout);
  1581. }
  1582. void CObjectEditor::OnClass()
  1583. {
  1584. // Get the class
  1585. // =============
  1586. VARIANT v;
  1587. VariantInit(&v);
  1588. if(FAILED(m_pObj->Get(L"__CLASS", 0, &v, NULL, NULL)))
  1589. {
  1590. MessageBox(IDS_CRITICAL_ERROR, IDS_ERROR, MB_OK);
  1591. return;
  1592. }
  1593. ShowClass(m_hDlg, m_lGenFlags, V_BSTR(&v), m_lSync, m_pOwner, m_lTimeout);
  1594. }
  1595. void CObjectEditor::OnDerived()
  1596. {
  1597. // Get the children
  1598. // ================
  1599. VARIANT v;
  1600. VariantInit(&v);
  1601. if(FAILED(m_pObj->Get(L"__CLASS", 0, &v, NULL, NULL)) ||
  1602. SysStringLen(V_BSTR(&v)) == 0)
  1603. {
  1604. MessageBox(IDS_INCOMPLETE_CLASS, IDS_ERROR, MB_OK);
  1605. return;
  1606. }
  1607. ShowClasses(m_hDlg, m_lGenFlags, WBEM_FLAG_SHALLOW, V_BSTR(&v), m_lSync, m_pOwner,
  1608. m_lTimeout, m_nBatch);
  1609. }
  1610. void CObjectEditor::OnInstances()
  1611. {
  1612. // Get the instances
  1613. // ================
  1614. VARIANT v;
  1615. VariantInit(&v);
  1616. if(FAILED(m_pObj->Get(L"__CLASS", 0, &v, NULL, NULL)) ||
  1617. SysStringLen(V_BSTR(&v)) == 0)
  1618. {
  1619. MessageBox(IDS_INCOMPLETE_CLASS, IDS_ERROR, MB_OK);
  1620. return;
  1621. }
  1622. ShowInstances(m_hDlg, m_lGenFlags, WBEM_FLAG_SHALLOW, V_BSTR(&v), m_lSync, m_pOwner,
  1623. m_lTimeout, m_nBatch);
  1624. }
  1625. void CObjectEditor::OnRefs()
  1626. {
  1627. CQueryResultDlg* pResDlg = new CQueryResultDlg(m_hDlg, m_lGenFlags, WBEM_FLAG_DEEP);
  1628. // Pass on invocation method (sync, async..) and related settings for this
  1629. // query and by any further operations (editing/deleting/etc. of an instance).
  1630. pResDlg->SetCallMethod(m_lSync);
  1631. pResDlg->SetTimeout(m_lTimeout);
  1632. pResDlg->SetBatchCount(m_nBatch);
  1633. VARIANT v;
  1634. VariantInit(&v);
  1635. if(FAILED(m_pObj->Get(L"__RELPATH", 0, &v, NULL, NULL)) ||
  1636. V_VT(&v) != VT_BSTR)
  1637. {
  1638. MessageBox(IDS_UNREFERENCABLE_OBJECT, IDS_ERROR, MB_OK);
  1639. delete pResDlg;
  1640. return;
  1641. }
  1642. WCHAR* wszQuery = new WCHAR[wcslen(V_BSTR(&v))+100];
  1643. swprintf(wszQuery, L"references of {%s}", V_BSTR(&v)); // Actual query, dont internationalize
  1644. char szTitle[1000];
  1645. char szFormat[104];
  1646. char* pTitle = NULL;
  1647. if(LoadString(GetModuleHandle(NULL), IDS_REFERENCES_OF, szFormat, 104))
  1648. {
  1649. sprintf(szTitle, szFormat, V_BSTR(&v));
  1650. pTitle = szTitle;
  1651. }
  1652. if(_ExecQuery(m_hDlg, m_lGenFlags, WBEM_FLAG_DEEP, wszQuery, L"WQL", m_lSync,
  1653. pResDlg, pTitle, m_lTimeout, m_nBatch))
  1654. {
  1655. pResDlg->RunDetached(m_pOwner);
  1656. }
  1657. else
  1658. {
  1659. delete pResDlg;
  1660. }
  1661. delete [] wszQuery;
  1662. }
  1663. void CObjectEditor::OnAssocs()
  1664. {
  1665. CQueryResultDlg* pResDlg = new CQueryResultDlg(m_hDlg, m_lGenFlags, WBEM_FLAG_DEEP);
  1666. // Pass on invocation method (sync, async..) and related settings for this
  1667. // query and by any further operations (editing/deleting/etc. of an instance).
  1668. pResDlg->SetCallMethod(m_lSync);
  1669. pResDlg->SetTimeout(m_lTimeout);
  1670. pResDlg->SetBatchCount(m_nBatch);
  1671. VARIANT v;
  1672. VariantInit(&v);
  1673. if(FAILED(m_pObj->Get(L"__RELPATH", 0, &v, NULL, NULL)) ||
  1674. V_VT(&v) != VT_BSTR)
  1675. {
  1676. MessageBox(IDS_UNREFERENCABLE_OBJECT, IDS_ERROR, MB_OK);
  1677. delete pResDlg;
  1678. return;
  1679. }
  1680. WCHAR* wszQuery = new WCHAR[wcslen(V_BSTR(&v))+100];
  1681. swprintf(wszQuery, L"associators of {%s}", V_BSTR(&v)); // Actual query, dont internationalize
  1682. char szTitle[1000];
  1683. char szFormat[104];
  1684. char* pTitle = NULL;
  1685. if(LoadString(GetModuleHandle(NULL), IDS_ASSOCIATORS_OF, szFormat, 104))
  1686. {
  1687. sprintf(szTitle, szFormat, V_BSTR(&v));
  1688. pTitle = szTitle;
  1689. }
  1690. if(_ExecQuery(m_hDlg, m_lGenFlags, WBEM_FLAG_DEEP, wszQuery, L"WQL", m_lSync,
  1691. pResDlg, pTitle, m_lTimeout, m_nBatch))
  1692. {
  1693. pResDlg->RunDetached(m_pOwner);
  1694. }
  1695. else
  1696. {
  1697. delete pResDlg;
  1698. }
  1699. delete [] wszQuery;
  1700. }
  1701. BOOL CObjectEditor::mstatic_bHideSystemDefault = FALSE;
  1702. void CObjectEditor::OnHideSystem()
  1703. {
  1704. m_bHideSystem = (GetCheck(IDC_HIDE_SYSTEM) == BST_CHECKED);
  1705. mstatic_bHideSystemDefault = m_bHideSystem;
  1706. Refresh();
  1707. }
  1708. void CObjectEditor::OnHideDerived()
  1709. {
  1710. m_bHideDerived = (GetCheck(IDC_HIDE_DERIVED) == BST_CHECKED);
  1711. Refresh();
  1712. }
  1713. void CObjectEditor::Refresh()
  1714. {
  1715. // Zap the current contents.
  1716. // =========================
  1717. SendMessage(m_hPropList, LB_RESETCONTENT, 0, 0);
  1718. SendMessage(m_hQualifierList, LB_RESETCONTENT, 0, 0);
  1719. SendMessage(m_hMethodList, LB_RESETCONTENT, 0, 0);
  1720. // Set the title to relpath
  1721. // ========================
  1722. char buf[TEMP_BUF];
  1723. char szFormat[1024];
  1724. VARIANT v;
  1725. VariantInit(&v);
  1726. HRESULT hres = m_pObj->Get(L"__RELPATH", 0, &v, NULL, NULL);
  1727. if(FAILED(hres) || V_VT(&v) == VT_NULL)
  1728. {
  1729. hres = m_pObj->Get(L"__CLASS", 0, &v, NULL, NULL);
  1730. if(FAILED(hres) || V_VT(&v) == VT_NULL)
  1731. {
  1732. hres = m_pObj->Get(L"__SUPERCLASS", 0, &v, NULL, NULL);
  1733. if(FAILED(hres) || V_VT(&v) != VT_BSTR)
  1734. {
  1735. LoadString(GetModuleHandle(NULL), IDS_NEW_TOP_LEVEL_CLASS, buf, TEMP_BUF);
  1736. }
  1737. else
  1738. {
  1739. LoadString(GetModuleHandle(NULL), IDS_NEW_CHILD_OF, szFormat, 1024);
  1740. _snprintf(buf, sizeof(buf)/sizeof(buf[0]), szFormat, V_BSTR(&v));
  1741. }
  1742. }
  1743. else
  1744. {
  1745. LoadString(GetModuleHandle(NULL), IDS_INSTANCE_OF, szFormat, 1024);
  1746. _snprintf(buf, sizeof(buf)/sizeof(buf[0]), szFormat, V_BSTR(&v));
  1747. }
  1748. }
  1749. else
  1750. {
  1751. LoadString(GetModuleHandle(NULL), IDS_OBJECT_EDITOR_FOR, szFormat, 1024);
  1752. _snprintf(buf, sizeof(buf)/sizeof(buf[0]), szFormat, V_BSTR(&v));
  1753. }
  1754. buf[sizeof(buf)-1] = '\0';
  1755. VariantClear(&v);
  1756. SetWindowText(m_hDlg, buf);
  1757. // Fill in the property list.
  1758. // ==========================
  1759. LONG lConFlags = 0; // condition flags (i.e., WBEM_CONDITION_FLAG_TYPE)
  1760. if(m_bHideDerived)
  1761. lConFlags = WBEM_FLAG_LOCAL_ONLY;
  1762. else if(m_bHideSystem)
  1763. lConFlags = WBEM_FLAG_NONSYSTEM_ONLY;
  1764. m_pObj->BeginEnumeration(lConFlags);
  1765. BSTR Name = NULL;
  1766. CIMTYPE ctType;
  1767. while (m_pObj->Next(0, &Name, &v, &ctType, NULL) == WBEM_S_NO_ERROR)
  1768. {
  1769. char buf2[TEMP_BUF];
  1770. CVar value(&v);
  1771. LPSTR TypeString = TypeToString(ctType);
  1772. LPSTR ValueString = ValueToNewString(&value, ctType);
  1773. if(strlen(ValueString) > 100)
  1774. {
  1775. ValueString[100] = 0;
  1776. }
  1777. sprintf(buf2, "%S\t%s\t%s", Name, TypeString, ValueString);
  1778. delete [] ValueString;
  1779. SendMessage(m_hPropList, LB_ADDSTRING, 0, LPARAM(buf2));
  1780. VariantClear(&v);
  1781. }
  1782. m_pObj->EndEnumeration();
  1783. // Fill in the Qualifier list.
  1784. // ===========================
  1785. IWbemQualifierSet *pQualifiers = NULL;
  1786. hres = m_pObj->GetQualifierSet(&pQualifiers);
  1787. if(SUCCEEDED(hres))
  1788. {
  1789. pQualifiers->BeginEnumeration(0);
  1790. BSTR strName = NULL;
  1791. VARIANT vVal;
  1792. VariantInit(&vVal);
  1793. long lFlavor;
  1794. while(pQualifiers->Next(0, &strName, &vVal, &lFlavor) == S_OK)
  1795. {
  1796. CTestQualifier A;
  1797. A.m_pName = new wchar_t[wcslen(strName)+1];
  1798. wcscpy(A.m_pName, strName);
  1799. A.m_pValue = new CVar(&vVal);
  1800. A.m_lType = lFlavor;
  1801. // Build list box string.
  1802. // ======================
  1803. const char * stringValue = CTestQualifierToString(&A);
  1804. SendMessage(m_hQualifierList, LB_ADDSTRING, 0,
  1805. LPARAM(stringValue));
  1806. delete [] stringValue;
  1807. }
  1808. pQualifiers->EndEnumeration();
  1809. pQualifiers->Release();
  1810. }
  1811. else
  1812. EnableWindow(m_hQualifierList, FALSE);
  1813. // Fill in the methods
  1814. m_pObj->BeginMethodEnumeration(0);
  1815. while (m_pObj->NextMethod(0, &Name, NULL, NULL) == WBEM_S_NO_ERROR)
  1816. {
  1817. char buf3[TEMP_BUF];
  1818. wcstombs(buf3, Name, TEMP_BUF);
  1819. buf3[TEMP_BUF-1] = '\0';
  1820. SendMessage(m_hMethodList, LB_ADDSTRING, 0, LPARAM(buf3));
  1821. VariantClear(&v);
  1822. }
  1823. m_pObj->EndMethodEnumeration();
  1824. // Configure the buttons
  1825. // =====================
  1826. ConfigureButtons();
  1827. }
  1828. void CObjectEditor::ConfigureButtons()
  1829. {
  1830. VARIANT v;
  1831. VariantInit(&v);
  1832. if(m_bClass)
  1833. {
  1834. // Configure for class
  1835. // ===================
  1836. if(FAILED(m_pObj->Get(L"__SUPERCLASS", 0, &v, NULL, NULL)) ||
  1837. V_VT(&v) != VT_BSTR)
  1838. {
  1839. EnableWindow(GetDlgItem(IDC_SUPERCLASS), FALSE);
  1840. }
  1841. else
  1842. {
  1843. VariantClear(&v);
  1844. EnableWindow(GetDlgItem(IDC_SUPERCLASS), TRUE);
  1845. }
  1846. }
  1847. else
  1848. {
  1849. // Configure for instance
  1850. // ======================
  1851. SetDlgItemText(IDC_CLASS, IDS_CLASS);
  1852. SetDlgItemText(IDC_REFERENCES, IDS_REFERENCES);
  1853. SetDlgItemText(IDC_ASSOCIATIONS, IDS_ASSOCIATORS);
  1854. EnableWindow(GetDlgItem(IDC_KEY), FALSE);
  1855. EnableWindow(GetDlgItem(IDC_INDEXED), FALSE);
  1856. }
  1857. if(m_bNoMethods || !m_bClass)
  1858. {
  1859. EnableWindow(GetDlgItem(IDC_ADD_METHOD), FALSE);
  1860. EnableWindow(GetDlgItem(IDC_EDIT_METHOD), FALSE);
  1861. EnableWindow(GetDlgItem(IDC_DELETE_METHOD), FALSE);
  1862. }
  1863. SetCheck(IDC_HIDE_SYSTEM, m_bHideSystem?BST_CHECKED:BST_UNCHECKED);
  1864. SetCheck(IDC_HIDE_DERIVED, m_bHideDerived?BST_CHECKED:BST_UNCHECKED);
  1865. EnableWindow(GetDlgItem(IDC_HIDE_SYSTEM), !m_bHideDerived);
  1866. if(m_dwEditMode == foreign)
  1867. {
  1868. EnableWindow(GetDlgItem(IDC_CLASS), FALSE);
  1869. EnableWindow(GetDlgItem(IDC_REFERENCES), FALSE);
  1870. EnableWindow(GetDlgItem(IDC_ASSOCIATIONS), FALSE);
  1871. EnableWindow(GetDlgItem(IDOK), FALSE);
  1872. }
  1873. }
  1874. CObjectEditor::CObjectEditor(HWND hParent, LONG lGenFlags, DWORD dwEditMode, LONG lSync,
  1875. IWbemClassObject *pObj, LONG lTimeout, ULONG nBatch)
  1876. : CWbemDialog(IDD_OBJECT_EDITOR, hParent)
  1877. {
  1878. m_pObj = pObj;
  1879. pObj->AddRef();
  1880. m_dwEditMode = dwEditMode;
  1881. m_bHideSystem = mstatic_bHideSystemDefault;
  1882. m_bHideDerived = FALSE;
  1883. m_bNoMethods = dwEditMode & nomethods;
  1884. m_lGenFlags = lGenFlags; // generic flags (i.e., WBEM_GENERIC_FLAG_TYPE)
  1885. m_lSync = lSync; // sync, async, semisync
  1886. m_lTimeout = lTimeout; // used in semisync only
  1887. m_nBatch = nBatch; // used in semisync and sync enumerations
  1888. VARIANT v;
  1889. VariantInit(&v);
  1890. m_pObj->Get(L"__GENUS", 0, &v, NULL, NULL);
  1891. m_bClass = (V_I4(&v) == WBEM_GENUS_CLASS);
  1892. m_bResultingObj = NULL;
  1893. }
  1894. CObjectEditor::~CObjectEditor()
  1895. {
  1896. m_pObj->Release();
  1897. }
  1898. INT_PTR CObjectEditor::Edit()
  1899. {
  1900. return Run();
  1901. }
  1902. BOOL CObjectEditor::OnOK()
  1903. {
  1904. if(m_bResultingObj)
  1905. {
  1906. //We need to extract the flag values...
  1907. LONG lChgFlags = 0; // change flags (i.e., WBEM_CHANGE_FLAG_TYPE)
  1908. if (GetCheck(IDC_UPDATE_NORMAL))
  1909. lChgFlags |= WBEM_FLAG_CREATE_OR_UPDATE;
  1910. if (GetCheck(IDC_UPDATE_CREATE))
  1911. lChgFlags |= WBEM_FLAG_CREATE_ONLY;
  1912. if (GetCheck(IDC_UPDATE_UPDATE))
  1913. lChgFlags |= WBEM_FLAG_UPDATE_ONLY;
  1914. if (GetCheck(IDC_UPDATE_COMPATIBLE))
  1915. lChgFlags |= WBEM_FLAG_UPDATE_COMPATIBLE;
  1916. if (GetCheck(IDC_UPDATE_SAFE))
  1917. lChgFlags |= WBEM_FLAG_UPDATE_SAFE_MODE;
  1918. if (GetCheck(IDC_UPDATE_FORCE))
  1919. lChgFlags |= WBEM_FLAG_UPDATE_FORCE_MODE;
  1920. if (!ResultingObject(m_pObj, lChgFlags))
  1921. return TRUE;
  1922. }
  1923. return CBasicWbemDialog::OnOK();
  1924. }
  1925. void CObjectEditor::RunDetached(CRefCountable* pOwner)
  1926. {
  1927. m_bResultingObj = TRUE;
  1928. SetOwner(pOwner);
  1929. SetDeleteOnClose();
  1930. Create(FALSE);
  1931. }
  1932. BOOL CObjectEditor::ResultingObject(IWbemClassObject* pObj, LONG lChgFlags)
  1933. {
  1934. BOOL bRes;
  1935. if(m_bClass)
  1936. bRes = _PutClass(m_hDlg, m_lGenFlags, lChgFlags, m_lSync, pObj, m_lTimeout);
  1937. else
  1938. bRes = _PutInstance(m_hDlg, m_lGenFlags, lChgFlags, m_lSync, pObj, m_lTimeout);
  1939. return bRes;
  1940. }
  1941. void CObjectEditor::OnAddMethod()
  1942. {
  1943. // Add a dummy property for now
  1944. // ============================
  1945. SCODE hres;
  1946. VARIANT v;
  1947. v.vt = VT_BSTR;
  1948. IWbemClassObject * pIn = NULL;
  1949. IWbemClassObject * pOut = NULL;
  1950. SCODE sc;
  1951. CBString bsParm(L"__PARAMETERS");
  1952. sc = g_pNamespace->GetObject(bsParm.GetString(), m_lGenFlags, g_Context, &pIn, NULL);
  1953. sc = g_pNamespace->GetObject(bsParm.GetString(), m_lGenFlags, g_Context, &pOut, NULL);
  1954. CBString bsTemp(L"__CLASS");
  1955. v.bstrVal = SysAllocString(L"InArgs");
  1956. sc = pIn->Put(bsTemp.GetString(), 0, &v, 0);
  1957. SysFreeString(v.bstrVal);
  1958. v.bstrVal = SysAllocString(L"OutArgs");
  1959. sc = pOut->Put(bsTemp.GetString(), 0, &v, 0);
  1960. SysFreeString(v.bstrVal);
  1961. bsTemp = L"DUMMY_METHOD__D";
  1962. if(FAILED(m_pObj->PutMethod(bsTemp.GetString(), 0, NULL, NULL)))
  1963. {
  1964. MessageBox(NULL, IDS_CANNOT_EDIT_METHOD, IDS_ERROR, MB_OK|MB_ICONSTOP);
  1965. return;
  1966. }
  1967. IWbemQualifierSet* pQualifierSet;
  1968. m_pObj->GetMethodQualifierSet(bsTemp.GetString(), &pQualifierSet);
  1969. // Create CTestMethod with the dummy attr set for now
  1970. // =================================================
  1971. CTestMethod method(pQualifierSet, pIn, pOut, FALSE, FALSE);
  1972. m_pObj->Get(L"__CLASS", 0, &v, NULL, NULL);
  1973. method.m_pClass = new wchar_t[wcslen(V_BSTR(&v)) + 1];
  1974. wcscpy(method.m_pClass, V_BSTR(&v));
  1975. VariantClear(&v);
  1976. CMethodEditor ed(m_hDlg, &method, FALSE, FALSE);
  1977. INT_PTR nRes = ed.Edit();
  1978. if ((nRes == IDCANCEL) || (nRes == 0))
  1979. {
  1980. pQualifierSet->Release();
  1981. goto DeleteDummy;
  1982. }
  1983. // Set the method
  1984. // ================
  1985. bsTemp = method.m_pName;
  1986. hres = m_pObj->PutMethod(bsTemp.GetString(), 0,
  1987. (method.m_bEnableInputArgs) ? method.m_pInArgs: NULL,
  1988. (method.m_bEnableOutputArgs) ? method.m_pOutArgs : NULL);
  1989. if(FAILED(hres))
  1990. {
  1991. FormatError(hres, m_hDlg, NULL);
  1992. goto DeleteDummy;
  1993. }
  1994. // Copy the Qualifiers
  1995. // ===================
  1996. IWbemQualifierSet* pRealQualifierSet;
  1997. m_pObj->GetMethodQualifierSet(bsTemp.GetString(), &pRealQualifierSet);
  1998. CopyQualifierSet(pRealQualifierSet, pQualifierSet, m_hDlg);
  1999. pQualifierSet->EndEnumeration();
  2000. pQualifierSet->Release();
  2001. pRealQualifierSet->Release();
  2002. DeleteDummy:
  2003. bsTemp = L"DUMMY_METHOD__D";
  2004. m_pObj->DeleteMethod(bsTemp.GetString());
  2005. Refresh();
  2006. return;
  2007. }
  2008. void CObjectEditor::OnEditMethod()
  2009. {
  2010. SCODE sc;
  2011. IWbemClassObject * pIn = NULL;
  2012. IWbemClassObject * pOut = NULL;
  2013. // Find out which property is selected.
  2014. // ====================================
  2015. LRESULT nIndex = SendMessage(m_hMethodList, LB_GETCURSEL, 0, 0);
  2016. if (nIndex == LB_ERR)
  2017. return;
  2018. char buf[TEMP_BUF];
  2019. *buf = 0;
  2020. SendMessage(m_hMethodList, LB_GETTEXT, nIndex, LPARAM(buf));
  2021. if (*buf == 0)
  2022. return;
  2023. WString wsName = buf;
  2024. CBString bsTemp(L"__CLASS");
  2025. // Get the property value from the object
  2026. // ======================================
  2027. m_pObj->GetMethod((LPWSTR)wsName, 0, &pIn, &pOut);
  2028. // Create a CTestMethod from it
  2029. // ==========================
  2030. IWbemQualifierSet* pQualifierSet = 0;
  2031. sc = m_pObj->GetMethodQualifierSet((LPWSTR)wsName, &pQualifierSet);
  2032. IWbemClassObject * pTempIn = pIn;
  2033. IWbemClassObject * pTempOut = pOut;
  2034. VARIANT v;
  2035. v.vt = VT_BSTR;
  2036. // If the current methods lacks an input or output object, then just create a
  2037. // temporary one in case the user decides to start using the input or output object.
  2038. if(pTempIn == NULL)
  2039. {
  2040. CBString bsParm(L"__PARAMETERS");
  2041. sc = g_pNamespace->GetObject(bsParm.GetString(), m_lGenFlags, g_Context,
  2042. &pTempIn, NULL);
  2043. v.bstrVal = SysAllocString(L"InArgs");
  2044. sc = pTempIn->Put(bsTemp.GetString(), 0, &v, 0);
  2045. SysFreeString(v.bstrVal);
  2046. }
  2047. if(pTempOut == NULL)
  2048. {
  2049. CBString bsParm(L"__PARAMETERS");
  2050. sc = g_pNamespace->GetObject(bsParm.GetString(), m_lGenFlags, g_Context,
  2051. &pTempOut, NULL);
  2052. v.bstrVal = SysAllocString(L"OutArgs");
  2053. sc = pTempOut->Put(bsTemp.GetString(), 0, &v, 0);
  2054. SysFreeString(v.bstrVal);
  2055. }
  2056. CTestMethod Copy(pQualifierSet, pTempIn, pTempOut, pIn != NULL, pOut != NULL);
  2057. if (pQualifierSet)
  2058. pQualifierSet->Release();
  2059. Copy.m_pName = new wchar_t[wcslen(wsName) + 1];
  2060. wcscpy(Copy.m_pName, wsName);
  2061. BSTR strClass;
  2062. m_pObj->GetMethodOrigin((LPWSTR)wsName, &strClass);
  2063. Copy.m_pClass = new wchar_t[wcslen(strClass) + 1];
  2064. wcscpy(Copy.m_pClass, strClass);
  2065. SysFreeString(strClass);
  2066. // Edit it.
  2067. // =========
  2068. CMethodEditor ed(m_hDlg, &Copy, TRUE, !m_bClass);
  2069. INT_PTR nRes = ed.Edit();
  2070. if ((nRes == IDCANCEL) || (nRes == 0))
  2071. return;
  2072. // If here, we must replace the info for the property.
  2073. // ===================================================
  2074. sc = m_pObj->PutMethod(Copy.m_pName, 0,
  2075. (Copy.m_bEnableInputArgs) ? Copy.m_pInArgs : NULL,
  2076. (Copy.m_bEnableOutputArgs) ? Copy.m_pOutArgs : NULL);
  2077. if(FAILED(sc))
  2078. {
  2079. FormatError(sc, m_hDlg);
  2080. }
  2081. Refresh();
  2082. }
  2083. void CObjectEditor::OnDelMethod()
  2084. {
  2085. // Find out which property is selected.
  2086. // ====================================
  2087. LRESULT nIndex = SendMessage(m_hMethodList, LB_GETCURSEL, 0, 0);
  2088. if (nIndex == LB_ERR)
  2089. return;
  2090. char buf[TEMP_BUF];
  2091. *buf = 0;
  2092. SendMessage(m_hMethodList, LB_GETTEXT, nIndex, LPARAM(buf));
  2093. if (*buf == 0)
  2094. return;
  2095. WString WName = buf;
  2096. if(FAILED(m_pObj->DeleteMethod(LPWSTR(WName))))
  2097. {
  2098. MessageBox(NULL, IDS_CANNOT_EDIT_METHOD, IDS_ERROR,
  2099. MB_OK|MB_ICONSTOP);
  2100. return;
  2101. }
  2102. Refresh();
  2103. }