Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1766 lines
37 KiB

  1. // WbemClassObject.cpp: implementation of the CWbemClassObject class.
  2. //
  3. // Copyright (c) 2000 Microsoft Corporation
  4. //
  5. // 03/26/00 v-marfin 60751 : In function CreateEnumerator(), if LogicalDisk, assume that error 0x80041001 is due to the
  6. // fact the user does not have logicaldisk perfmon turned on.
  7. // Advise them to run "DiskPerf -YV" and reboot.
  8. // 03/30/00 v-marfin 62531 : Allow an empty array as a means of removing a property of type array.
  9. //
  10. #include "stdafx.h"
  11. #include "SnapIn.h"
  12. #include "WbemClassObject.h"
  13. #include "WbemEventListener.h"
  14. #include <objbase.h>
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char THIS_FILE[]=__FILE__;
  18. #define new DEBUG_NEW
  19. #endif
  20. IMPLEMENT_DYNCREATE(CWbemClassObject,CObject)
  21. //////////////////////////////////////////////////////////////////////
  22. // Construction/Destruction
  23. //////////////////////////////////////////////////////////////////////
  24. CWbemClassObject::CWbemClassObject()
  25. {
  26. m_pIEnumerator = NULL;
  27. m_pIWbemClassObject = NULL;
  28. }
  29. CWbemClassObject::~CWbemClassObject()
  30. {
  31. Destroy();
  32. }
  33. //////////////////////////////////////////////////////////////////////
  34. // Create/Destroy
  35. //////////////////////////////////////////////////////////////////////
  36. HRESULT CWbemClassObject::Create(const CString& sMachineName)
  37. {
  38. TRACEX(_T("CWbemClassObject::Create\n"));
  39. TRACEARGs(sMachineName);
  40. m_sMachineName = sMachineName;
  41. return S_OK;
  42. }
  43. HRESULT CWbemClassObject::Create(IWbemClassObject* pObject)
  44. {
  45. TRACEX(_T("CWbemClassObject::Create\n"));
  46. TRACEARGn(pObject);
  47. ASSERT(pObject);
  48. if( pObject )
  49. {
  50. m_pIWbemClassObject = pObject;
  51. return S_OK;
  52. }
  53. else
  54. {
  55. return E_FAIL;
  56. }
  57. }
  58. void CWbemClassObject::Destroy()
  59. {
  60. TRACEX(_T("CWbemClassObject::Destroy\n"));
  61. if( m_pIEnumerator )
  62. {
  63. m_pIEnumerator->Release();
  64. m_pIEnumerator = NULL;
  65. }
  66. if( m_pIWbemClassObject )
  67. {
  68. m_pIWbemClassObject->Release();
  69. m_pIWbemClassObject = NULL;
  70. }
  71. m_sMachineName.Empty();
  72. m_sNamespace.Empty();
  73. }
  74. //////////////////////////////////////////////////////////////////////
  75. // Property Operations
  76. //////////////////////////////////////////////////////////////////////
  77. // v-marfin
  78. //***********************************************************************************
  79. // GetRawProperty
  80. //
  81. // This function retreives the raw property of the object. No conversions take
  82. // place on it, no formatting, nothing. The user is responsible for determining
  83. // the format and performing any conversions etc.
  84. //***********************************************************************************
  85. HRESULT CWbemClassObject::GetRawProperty(const CString& sProperty, VARIANT& vPropValue)
  86. {
  87. TRACEX(_T("CWbemClassObject::GetRawProperty\n"));
  88. TRACEARGs(sProperty);
  89. ASSERT(m_pIWbemClassObject);
  90. if( m_pIWbemClassObject == NULL )
  91. {
  92. return E_FAIL;
  93. }
  94. BSTR bsProperty = sProperty.AllocSysString();
  95. ASSERT(bsProperty);
  96. if( bsProperty == NULL )
  97. {
  98. return E_FAIL;
  99. }
  100. HRESULT hr = m_pIWbemClassObject->Get(bsProperty, 0L, &vPropValue, NULL, NULL);
  101. if( !CHECKHRESULT(hr) )
  102. {
  103. TRACE(_T("FAILED : IWbemClassObject::GetRawProperty failed.\n"));
  104. ::SysFreeString(bsProperty);
  105. return hr;
  106. }
  107. ::SysFreeString(bsProperty);
  108. return hr;
  109. }
  110. //***********************************************************************************
  111. // SetRawProperty
  112. //
  113. // This function sets the raw property of the object. No conversions take
  114. // place on it, no formatting, nothing. The user is responsible for ensuring
  115. // that the data is in its proper format etc.
  116. //***********************************************************************************
  117. HRESULT CWbemClassObject::SetRawProperty(const CString& sProperty, VARIANT& vPropValue)
  118. {
  119. TRACEX(_T("CWbemClassObject::SetRawProperty\n"));
  120. TRACEARGs(sProperty);
  121. ASSERT(m_pIWbemClassObject);
  122. if( m_pIWbemClassObject == NULL )
  123. {
  124. return E_FAIL;
  125. }
  126. BSTR bsProperty = sProperty.AllocSysString();
  127. ASSERT(bsProperty);
  128. if( bsProperty == NULL )
  129. {
  130. return E_FAIL;
  131. }
  132. HRESULT hr = m_pIWbemClassObject->Put(bsProperty, 0L, &vPropValue, NULL);
  133. if( !CHECKHRESULT(hr) )
  134. {
  135. TRACE(_T("FAILED : IWbemClassObject::SetRawProperty failed.\n"));
  136. TRACEARGn(hr);
  137. ::SysFreeString(bsProperty);
  138. return hr;
  139. }
  140. VariantClear(&vPropValue);
  141. ::SysFreeString(bsProperty);
  142. return S_OK;
  143. }
  144. HRESULT CWbemClassObject::GetPropertyNames(CStringArray& saNames)
  145. {
  146. TRACEX(_T("CWbemClassObject::GetPropertyNames\n"));
  147. ASSERT(m_pIWbemClassObject);
  148. if( m_pIWbemClassObject == NULL )
  149. {
  150. TRACE(_T("FAILED : m_pIWbemClassObject is NULL\n"));
  151. return E_FAIL;
  152. }
  153. HRESULT hr = S_OK;
  154. SAFEARRAY* psa = NULL;
  155. if( ! CHECKHRESULT(hr = m_pIWbemClassObject->GetNames(NULL,WBEM_FLAG_ALWAYS|WBEM_FLAG_NONSYSTEM_ONLY,NULL,&psa)) )
  156. {
  157. return hr;
  158. }
  159. COleSafeArray osa(*psa,VT_BSTR);
  160. long lLower = 0L;
  161. long lUpper = -1L;
  162. osa.GetLBound(1L,&lLower);
  163. osa.GetUBound(1L,&lUpper);
  164. for( long i = lLower; i <= lUpper; i++ )
  165. {
  166. BSTR bsPropertyName;
  167. osa.GetElement(&i,&bsPropertyName);
  168. saNames.Add(CString(bsPropertyName));
  169. }
  170. return hr;
  171. }
  172. HRESULT CWbemClassObject::GetPropertyType(const CString& sPropertyName, CString& sType)
  173. {
  174. TRACEX(_T("CWbemClassObject::GetPropertyType\n"));
  175. ASSERT(m_pIWbemClassObject);
  176. if( m_pIWbemClassObject == NULL )
  177. {
  178. TRACE(_T("FAILED : m_pIWbemClassObject is NULL\n"));
  179. return E_FAIL;
  180. }
  181. HRESULT hr = S_OK;
  182. CIMTYPE type;
  183. if( ! CHECKHRESULT( hr = GetPropertyType(sPropertyName,type) ) )
  184. {
  185. return hr;
  186. }
  187. sType.Empty();
  188. if( type & CIM_FLAG_ARRAY )
  189. {
  190. sType.LoadString(IDS_STRING_ARRAY_OF);
  191. type &= ~CIM_FLAG_ARRAY;
  192. }
  193. CString sResString;
  194. switch( type )
  195. {
  196. case CIM_ILLEGAL:
  197. {
  198. sResString.LoadString(IDS_STRING_CIM_ILLEGAL);
  199. sType += sResString;
  200. }
  201. break;
  202. case CIM_EMPTY:
  203. {
  204. sResString.LoadString(IDS_STRING_CIM_EMPTY);
  205. sType += sResString;
  206. }
  207. break;
  208. case CIM_SINT8:
  209. case CIM_SINT16:
  210. case CIM_SINT32:
  211. case CIM_SINT64:
  212. {
  213. sResString.LoadString(IDS_STRING_CIM_SINT);
  214. sType += sResString;
  215. }
  216. break;
  217. case CIM_UINT8:
  218. case CIM_UINT16:
  219. case CIM_UINT32:
  220. case CIM_UINT64:
  221. {
  222. sResString.LoadString(IDS_STRING_CIM_UINT);
  223. sType += sResString;
  224. }
  225. break;
  226. case CIM_REAL32:
  227. case CIM_REAL64:
  228. {
  229. sResString.LoadString(IDS_STRING_CIM_REAL);
  230. sType += sResString;
  231. }
  232. break;
  233. case CIM_BOOLEAN:
  234. {
  235. sResString.LoadString(IDS_STRING_CIM_BOOLEAN);
  236. sType += sResString;
  237. }
  238. break;
  239. case CIM_STRING:
  240. {
  241. sResString.LoadString(IDS_STRING_CIM_STRING);
  242. sType += sResString;
  243. }
  244. break;
  245. case CIM_DATETIME:
  246. {
  247. sResString.LoadString(IDS_STRING_CIM_DATETIME);
  248. sType += sResString;
  249. }
  250. break;
  251. case CIM_REFERENCE:
  252. {
  253. sResString.LoadString(IDS_STRING_CIM_REFERENCE);
  254. sType += sResString;
  255. }
  256. break;
  257. case CIM_CHAR16:
  258. {
  259. sResString.LoadString(IDS_STRING_CIM_CHAR16);
  260. sType += sResString;
  261. }
  262. break;
  263. case CIM_OBJECT:
  264. {
  265. sResString.LoadString(IDS_STRING_CIM_OBJECT);
  266. sType += sResString;
  267. }
  268. break;
  269. default:
  270. {
  271. hr = E_FAIL;
  272. ASSERT(FALSE);
  273. sType.Empty();
  274. }
  275. }
  276. return hr;
  277. }
  278. HRESULT CWbemClassObject::GetPropertyType(const CString& sPropertyName, CIMTYPE& Type)
  279. {
  280. TRACEX(_T("CWbemClassObject::GetPropertyType\n"));
  281. ASSERT(m_pIWbemClassObject);
  282. if( m_pIWbemClassObject == NULL )
  283. {
  284. TRACE(_T("FAILED : m_pIWbemClassObject is NULL\n"));
  285. return E_FAIL;
  286. }
  287. HRESULT hr = S_OK;
  288. BSTR bsProperty = sPropertyName.AllocSysString();
  289. ASSERT(bsProperty);
  290. if( bsProperty == NULL )
  291. {
  292. return E_FAIL;
  293. }
  294. hr = m_pIWbemClassObject->Get(bsProperty, 0L, NULL, &Type, NULL);
  295. if( !CHECKHRESULT(hr) )
  296. {
  297. TRACE(_T("FAILED : IWbemClassObject::Get( TYPE ) failed.\n"));
  298. ::SysFreeString(bsProperty);
  299. return hr;
  300. }
  301. ::SysFreeString(bsProperty);
  302. return hr;
  303. }
  304. HRESULT CWbemClassObject::SaveAllProperties()
  305. {
  306. TRACEX(_T("CWbemClassObject::SaveAllProperties\n"));
  307. ASSERT(!m_sMachineName.IsEmpty());
  308. IWbemServices* pServices = NULL;
  309. HRESULT hr = S_OK;
  310. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  311. {
  312. return hr;
  313. }
  314. ASSERT(pServices);
  315. ASSERT(m_pIWbemClassObject);
  316. if( m_pIWbemClassObject == NULL )
  317. {
  318. TRACE(_T("FAILED : m_pIWbemClassObject is NULL\n"));
  319. return E_FAIL;
  320. }
  321. // update this instance
  322. hr = pServices->PutInstance(m_pIWbemClassObject,WBEM_FLAG_CREATE_OR_UPDATE,NULL,NULL);
  323. pServices->Release();
  324. if( !CHECKHRESULT(hr) )
  325. {
  326. TRACE(_T("FAILED : IWbemServices::PutInstance failed\n"));
  327. return hr;
  328. }
  329. return hr;
  330. }
  331. bool CWbemClassObject::GetPropertyValueFromString(const CString& sWMIString, const CString& sPropName, CString& sProperty)
  332. {
  333. TRACEX(_T("CWbemClassObject::GetPropertyValueFromString\n"));
  334. TRACEARGs(sWMIString);
  335. TRACEARGs(sPropName);
  336. int iStart = -1;
  337. if( (iStart = sWMIString.Find(sPropName)) != -1 )
  338. {
  339. sProperty = sWMIString.Right(sWMIString.GetLength() - iStart);
  340. int iEnd = sProperty.Find(_T(","));
  341. if( iEnd == -1 )
  342. {
  343. iEnd = sProperty.Find(_T(" AND "));
  344. if( iEnd == -1 )
  345. {
  346. iEnd = sProperty.GetLength();
  347. }
  348. }
  349. sProperty = sProperty.Left(iEnd);
  350. iStart = sProperty.Find(_T("=")) + 1;
  351. sProperty = sProperty.Right(sProperty.GetLength()-iStart);
  352. sProperty.TrimLeft(_T("\""));
  353. sProperty.TrimRight(_T("\""));
  354. return true;
  355. }
  356. sProperty.Empty();
  357. return false; // did not find the property name in the path
  358. }
  359. //////////////////////////////////////////////////////////////////////
  360. // WBEM Operations
  361. //////////////////////////////////////////////////////////////////////
  362. HRESULT CWbemClassObject::GetObject(const CString& sObjectPath)
  363. {
  364. TRACEX(_T("CWbemClassObject::GetObject\n"));
  365. TRACEARGs(sObjectPath);
  366. // do not call me if you have not called Destroy first
  367. ASSERT(m_pIWbemClassObject == NULL);
  368. ASSERT(sObjectPath.GetLength());
  369. if( sObjectPath.IsEmpty() )
  370. {
  371. TRACE(_T("FAILED : sObjectPath is NULL. Failed.\n"));
  372. return E_FAIL;
  373. }
  374. IWbemServices* pServices = NULL;
  375. HRESULT hr = S_OK;
  376. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  377. {
  378. return hr;
  379. }
  380. VERIFY(pServices);
  381. // get the object's signature
  382. BSTR bsPath = sObjectPath.AllocSysString();
  383. hr = pServices->GetObject(bsPath, 0, NULL, &m_pIWbemClassObject, NULL);
  384. pServices->Release();
  385. if( !CHECKHRESULT(hr) )
  386. {
  387. TRACE(_T("FAILED : IWbemServices::GetObject failed.\n"));
  388. ::SysFreeString(bsPath);
  389. return hr;
  390. }
  391. ::SysFreeString(bsPath);
  392. return hr;
  393. }
  394. HRESULT CWbemClassObject::GetObjectText(CString& sText)
  395. {
  396. TRACEX(_T("CWbemClassObject::GetObjectText\n"));
  397. ASSERT(m_pIWbemClassObject);
  398. if( m_pIWbemClassObject == NULL )
  399. {
  400. return E_FAIL;
  401. }
  402. HRESULT hr = S_OK;
  403. BSTR bsText = NULL;
  404. if( ! CHECKHRESULT(hr = m_pIWbemClassObject->GetObjectText(0L,&bsText)) )
  405. {
  406. return hr;
  407. }
  408. sText = bsText;
  409. ::SysFreeString(bsText);
  410. return hr;
  411. }
  412. HRESULT CWbemClassObject::ExecQuery(BSTR bsQueryString)
  413. {
  414. TRACEX(_T("CWbemClassObject::ExecQuery\n"));
  415. TRACEARGs(bsQueryString);
  416. ASSERT(bsQueryString);
  417. if( bsQueryString == NULL )
  418. {
  419. TRACE(_T("FAILED : bsQueryString is NULL. CWbemClassObject::ExecQuery Failed.\n"));
  420. return E_FAIL;
  421. }
  422. IWbemServices* pServices = NULL;
  423. HRESULT hr = S_OK;
  424. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  425. {
  426. return hr;
  427. }
  428. ASSERT(pServices);
  429. BSTR bsLanguage = SysAllocString(_T("WQL"));
  430. m_pIEnumerator = NULL;
  431. // Issue Query
  432. hr = pServices->ExecQuery(bsLanguage,bsQueryString,WBEM_FLAG_BIDIRECTIONAL,0,&m_pIEnumerator);
  433. SysFreeString(bsLanguage);
  434. pServices->Release();
  435. if( !CHECKHRESULT(hr) )
  436. {
  437. TRACE(_T("FAILED : IWbemServices::ExecQuery failed.\n"));
  438. return hr;
  439. }
  440. ASSERT(m_pIEnumerator);
  441. SetBlanket(m_pIEnumerator);
  442. return hr;
  443. }
  444. HRESULT CWbemClassObject::ExecQueryAsync(BSTR bsQueryString, CWbemEventListener* pListener)
  445. {
  446. TRACEX(_T("CWbemClassObject::ExecQueryAsync\n"));
  447. TRACEARGs(bsQueryString);
  448. ASSERT(bsQueryString);
  449. if( bsQueryString == NULL )
  450. {
  451. TRACE(_T("FAILED : bsQueryString is NULL. CWbemClassObject::ExecQuery Failed.\n"));
  452. return E_FAIL;
  453. }
  454. IWbemServices* pServices = NULL;
  455. HRESULT hr = S_OK;
  456. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  457. {
  458. return hr;
  459. }
  460. ASSERT(pServices);
  461. BSTR bsLanguage = SysAllocString(_T("WQL"));
  462. m_pIEnumerator = NULL;
  463. // Issue Query
  464. hr = pServices->ExecQueryAsync(bsLanguage,bsQueryString,WBEM_FLAG_BIDIRECTIONAL,0,pListener->GetSink());
  465. SysFreeString(bsLanguage);
  466. pServices->Release();
  467. if( !CHECKHRESULT(hr) )
  468. {
  469. TRACE(_T("FAILED : IWbemServices::ExecQuery failed.\n"));
  470. return hr;
  471. }
  472. return hr;
  473. }
  474. HRESULT CWbemClassObject::CreateEnumerator(BSTR bsClassName)
  475. {
  476. TRACEX(_T("CWbemClassObject::CreateEnumerator\n"));
  477. ASSERT(bsClassName);
  478. IWbemServices* pServices = NULL;
  479. HRESULT hr = S_OK;
  480. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  481. {
  482. DisplayErrorMsgBox(hr);
  483. return hr;
  484. }
  485. ASSERT(pServices);
  486. hr = pServices->CreateInstanceEnum(bsClassName,WBEM_FLAG_SHALLOW|
  487. WBEM_FLAG_BIDIRECTIONAL, NULL, &m_pIEnumerator);
  488. pServices->Release();
  489. if( !CHECKHRESULT(hr) )
  490. {
  491. // v-marfin 60751 : If LogicalDisk, assume that error 0x80041001 is due to the
  492. // fact the user does not have logicaldisk perfmon turned on.
  493. // Advise them to run "DiskPerf -YV" and reboot.
  494. if ((hr == 0x80041001) && (CString(bsClassName).CompareNoCase(_T("LogicalDisk")) == 0))
  495. {
  496. AfxMessageBox(IDS_WARNING_DISKPERF);
  497. return hr;
  498. }
  499. DisplayErrorMsgBox(hr);
  500. return hr;
  501. }
  502. SetBlanket(m_pIEnumerator);
  503. return hr;
  504. }
  505. HRESULT CWbemClassObject::CreateClassEnumerator(BSTR bsClassName)
  506. {
  507. TRACEX(_T("CWbemClassObject::CreateClassEnumerator\n"));
  508. TRACEARGs(bsClassName);
  509. IWbemServices* pServices = NULL;
  510. HRESULT hr = S_OK;
  511. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  512. {
  513. DisplayErrorMsgBox(hr);
  514. return hr;
  515. }
  516. ASSERT(pServices);
  517. hr = pServices->CreateClassEnum(bsClassName,WBEM_FLAG_DEEP|
  518. WBEM_FLAG_RETURN_IMMEDIATELY|
  519. WBEM_FLAG_FORWARD_ONLY, NULL, &m_pIEnumerator);
  520. pServices->Release();
  521. if( !CHECKHRESULT(hr) )
  522. {
  523. DisplayErrorMsgBox(hr);
  524. return hr;
  525. }
  526. SetBlanket(m_pIEnumerator);
  527. return hr;
  528. }
  529. HRESULT CWbemClassObject::CreateAsyncEnumerator(BSTR bsClassName, CWbemEventListener* pListener)
  530. {
  531. TRACEX(_T("CWbemClassObject::CreateEnumerator\n"));
  532. ASSERT(bsClassName);
  533. IWbemServices* pServices = NULL;
  534. HRESULT hr = S_OK;
  535. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  536. {
  537. return hr;
  538. }
  539. ASSERT(pServices);
  540. hr = pServices->CreateInstanceEnumAsync(bsClassName,WBEM_FLAG_SHALLOW|
  541. WBEM_FLAG_RETURN_IMMEDIATELY|
  542. WBEM_FLAG_FORWARD_ONLY, NULL, pListener->GetSink() );
  543. pServices->Release();
  544. if( !CHECKHRESULT(hr) )
  545. {
  546. return hr;
  547. }
  548. return hr;
  549. }
  550. HRESULT CWbemClassObject::GetNextObject(ULONG& uReturned)
  551. {
  552. ASSERT(m_pIEnumerator);
  553. if( m_pIEnumerator == NULL )
  554. {
  555. return E_FAIL;
  556. }
  557. if( m_pIWbemClassObject )
  558. {
  559. m_pIWbemClassObject->Release();
  560. m_pIWbemClassObject = NULL;
  561. }
  562. HRESULT hr = m_pIEnumerator->Next(WBEM_INFINITE,1,&m_pIWbemClassObject,&uReturned);
  563. if( FAILED(hr) )
  564. {
  565. TRACEARGn(hr);
  566. TRACE(_T("WARNING : IEnumWbemClassObject::Next failed to find another instance\n"));
  567. return hr;
  568. }
  569. return hr;
  570. }
  571. HRESULT CWbemClassObject::Reset()
  572. {
  573. TRACEX(_T("CWbemClassObject::Reset\n"));
  574. ASSERT(m_pIEnumerator);
  575. if( m_pIEnumerator == NULL )
  576. {
  577. return E_FAIL;
  578. }
  579. HRESULT hr = m_pIEnumerator->Reset();
  580. if( !CHECKHRESULT(hr) )
  581. {
  582. TRACE(_T("FAILED : IEnumWbemClassObject::Reset failed.\n"));
  583. return hr;
  584. }
  585. return hr;
  586. }
  587. HRESULT CWbemClassObject::CreateInstance(BSTR bsClassName)
  588. {
  589. TRACEX(_T("CWbemClassObject::CreateInstance\n"));
  590. TRACEARGs(bsClassName);
  591. // do not call me if you have not called Destroy first
  592. ASSERT(m_pIWbemClassObject == NULL);
  593. ASSERT(bsClassName);
  594. if( bsClassName == NULL )
  595. {
  596. TRACE(_T("FAILED : bsClassName is NULL. Failed.\n"));
  597. return E_FAIL;
  598. }
  599. IWbemServices* pServices = NULL;
  600. HRESULT hr = S_OK;
  601. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  602. {
  603. return hr;
  604. }
  605. ASSERT(pServices);
  606. // get the object's signature
  607. IWbemClassObject* pClassObject = NULL;
  608. hr = pServices->GetObject(bsClassName, 0, NULL, &pClassObject, NULL);
  609. pServices->Release();
  610. if( !CHECKHRESULT(hr) )
  611. {
  612. TRACE(_T("FAILED : IWbemServices::GetObject failed.\n"));
  613. return hr;
  614. }
  615. // create an instance of the class based on the signature
  616. ASSERT(pClassObject);
  617. hr = pClassObject->SpawnInstance(0,&m_pIWbemClassObject);
  618. if( !CHECKHRESULT(hr) )
  619. {
  620. TRACE(_T("FAILED : IWbemClassObject::SpawnInstance failed.\n"));
  621. pClassObject->Release();
  622. return hr;
  623. }
  624. ASSERT(m_pIWbemClassObject);
  625. if( m_pIWbemClassObject == NULL )
  626. {
  627. TRACE(_T("FAILED : An unexpected error occurred with IWbemClassObject::SpawnInstance. Failed\n"));
  628. return E_FAIL;
  629. }
  630. pClassObject->Release();
  631. return S_OK;
  632. }
  633. HRESULT CWbemClassObject::DeleteInstance(const CString& sClassObjectPath)
  634. {
  635. TRACEX(_T("CWbemClassObject::DeleteInstance"));
  636. TRACEARGs(sClassObjectPath);
  637. if( sClassObjectPath.IsEmpty() )
  638. {
  639. TRACE(_T("FAILED : bsClassInstanceName is NULL. Failed.\n"));
  640. return E_FAIL;
  641. }
  642. IWbemServices* pServices = NULL;
  643. HRESULT hr = S_OK;
  644. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  645. {
  646. return hr;
  647. }
  648. ASSERT(pServices);
  649. BSTR bsClassInstanceName = sClassObjectPath.AllocSysString();
  650. hr = pServices->DeleteInstance(bsClassInstanceName,0L,NULL,NULL);
  651. ::SysFreeString(bsClassInstanceName);
  652. pServices->Release();
  653. if( !CHECKHRESULT(hr) )
  654. {
  655. TRACE(_T("FAILED : IWbemServices::DeleteInstance failed.\n"));
  656. return hr;
  657. }
  658. return hr;
  659. }
  660. HRESULT CWbemClassObject::GetMethod(const CString& sMethodName, CWbemClassObject& MethodInput)
  661. {
  662. TRACEX(_T("CWbemClassObject::GetMethod\n"));
  663. TRACEARGs(sMethodName);
  664. if( m_pIWbemClassObject == NULL )
  665. {
  666. ASSERT(FALSE);
  667. return E_FAIL;
  668. }
  669. IWbemClassObject* pInClass = NULL;
  670. IWbemClassObject* pOutClass = NULL;
  671. BSTR bsMethodName = sMethodName.AllocSysString();
  672. HRESULT hr = m_pIWbemClassObject->GetMethod(bsMethodName, 0, &pInClass, &pOutClass);
  673. if( ! CHECKHRESULT(hr) || pInClass == NULL )
  674. {
  675. ::SysFreeString(bsMethodName);
  676. return hr;
  677. }
  678. ASSERT(pInClass);
  679. IWbemClassObject* pInInst = NULL;
  680. if( pInClass )
  681. {
  682. hr = pInClass->SpawnInstance(0, &pInInst);
  683. pInClass->Release();
  684. if( ! CHECKHRESULT(hr) || pInInst == NULL )
  685. {
  686. ::SysFreeString(bsMethodName);
  687. return hr;
  688. }
  689. }
  690. if( pOutClass )
  691. {
  692. pOutClass->Release();
  693. }
  694. ASSERT(pInInst);
  695. MethodInput.Create(pInInst);
  696. ::SysFreeString(bsMethodName);
  697. return S_OK;
  698. }
  699. HRESULT CWbemClassObject::ExecuteMethod(const CString& sMethodName, const CString& sArgumentName, const CString& sArgumentValue, int& iReturnValue)
  700. {
  701. TRACEX(_T("CWbemClassObject::ExecuteMethod\n"));
  702. TRACEARGs(sMethodName);
  703. TRACEARGs(sArgumentName);
  704. TRACEARGs(sArgumentValue);
  705. if( sMethodName.IsEmpty() )
  706. {
  707. return E_FAIL;
  708. }
  709. if( sArgumentName.IsEmpty() )
  710. {
  711. return E_FAIL;
  712. }
  713. if( sArgumentValue.IsEmpty() )
  714. {
  715. return E_FAIL;
  716. }
  717. if( m_pIWbemClassObject == NULL )
  718. {
  719. ASSERT(FALSE);
  720. return E_FAIL;
  721. }
  722. CWbemClassObject InInstance;
  723. CWbemClassObject OutInstance;
  724. HRESULT hr = GetMethod(sMethodName,InInstance);
  725. if( ! CHECKHRESULT(hr) )
  726. {
  727. return hr;
  728. }
  729. InInstance.SetProperty(sArgumentName,sArgumentValue);
  730. hr = ExecuteMethod(sMethodName,InInstance,OutInstance);
  731. return hr;
  732. }
  733. HRESULT CWbemClassObject::ExecuteMethod(const CString& sMethodName, CWbemClassObject& InInstance, CWbemClassObject& OutInstance)
  734. {
  735. TRACEX(_T("CWbemClassObject::ExecuteMethod\n"));
  736. TRACEARGs(sMethodName);
  737. IWbemServices* pServices = NULL;
  738. HRESULT hr = S_OK;
  739. if( ! CHECKHRESULT(hr = Connect(pServices)) )
  740. {
  741. return hr;
  742. }
  743. ASSERT(pServices);
  744. // Call the method
  745. IWbemClassObject* pOutInst = NULL;
  746. IWbemClassObject* pInInst = InInstance.GetClassObject();
  747. CString sPath;
  748. GetProperty(_T("__PATH"),sPath);
  749. BSTR bsPath = sPath.AllocSysString();
  750. BSTR bsMethodName = sMethodName.AllocSysString();
  751. hr = pServices->ExecMethod(bsPath, bsMethodName, 0, NULL, pInInst, &pOutInst, NULL);
  752. if( pInInst )
  753. {
  754. pInInst->Release();
  755. }
  756. if( ! CHECKHRESULT(hr) )
  757. {
  758. ::SysFreeString(bsMethodName);
  759. ::SysFreeString(bsPath);
  760. return hr;
  761. }
  762. if( pOutInst )
  763. {
  764. OutInstance.Create(pOutInst);
  765. }
  766. ::SysFreeString(bsMethodName);
  767. ::SysFreeString(bsPath);
  768. return S_OK;
  769. }
  770. HRESULT CWbemClassObject::GetLocaleStringProperty(const CString& sProperty, CString& sPropertyValue)
  771. {
  772. TRACEX(_T("CWbemClassObject::GetProperty\n"));
  773. TRACEARGs(sProperty);
  774. ASSERT(m_pIWbemClassObject);
  775. if( m_pIWbemClassObject == NULL )
  776. {
  777. return E_FAIL;
  778. }
  779. HRESULT hr = GetProperty(sProperty,sPropertyValue);
  780. if( ! CHECKHRESULT(hr) )
  781. {
  782. return hr;
  783. }
  784. return hr;
  785. }
  786. HRESULT CWbemClassObject::GetProperty(const CString& sProperty, CString& sPropertyValue)
  787. {
  788. TRACEX(_T("CWbemClassObject::GetProperty\n"));
  789. TRACEARGs(sProperty);
  790. ASSERT(m_pIWbemClassObject);
  791. if( m_pIWbemClassObject == NULL )
  792. {
  793. return E_FAIL;
  794. }
  795. BSTR bsProperty = sProperty.AllocSysString();
  796. ASSERT(bsProperty);
  797. if( bsProperty == NULL )
  798. {
  799. return E_FAIL;
  800. }
  801. VARIANT vPropValue;
  802. VariantInit(&vPropValue);
  803. HRESULT hr = m_pIWbemClassObject->Get(bsProperty, 0L, &vPropValue, NULL, NULL);
  804. if( !CHECKHRESULT(hr) )
  805. {
  806. TRACE(_T("FAILED : IWbemClassObject::Get( BSTR ) failed.\n"));
  807. ::SysFreeString(bsProperty);
  808. sPropertyValue.Empty();
  809. return hr;
  810. }
  811. if( V_VT(&vPropValue) != VT_NULL )
  812. {
  813. sPropertyValue = V_BSTR(&vPropValue);
  814. }
  815. else
  816. {
  817. sPropertyValue.Empty();
  818. }
  819. VariantClear(&vPropValue);
  820. ::SysFreeString(bsProperty);
  821. return hr;
  822. }
  823. HRESULT CWbemClassObject::GetProperty(const CString& sProperty, int& iPropertyValue)
  824. {
  825. TRACEX(_T("CWbemClassObject::GetProperty\n"));
  826. TRACEARGs(sProperty);
  827. ASSERT(m_pIWbemClassObject);
  828. if( m_pIWbemClassObject == NULL )
  829. {
  830. return E_FAIL;
  831. }
  832. BSTR bsProperty = sProperty.AllocSysString();
  833. ASSERT(bsProperty);
  834. if( bsProperty == NULL )
  835. {
  836. return E_FAIL;
  837. }
  838. VARIANT vPropValue;
  839. VariantInit(&vPropValue);
  840. HRESULT hr = m_pIWbemClassObject->Get(bsProperty, 0L, &vPropValue, NULL, NULL);
  841. if( !CHECKHRESULT(hr) )
  842. {
  843. TRACE(_T("FAILED : IWbemClassObject::Get( int ) failed.\n"));
  844. iPropertyValue = 0;
  845. ::SysFreeString(bsProperty);
  846. return hr;
  847. }
  848. iPropertyValue = V_I4(&vPropValue);
  849. VariantClear(&vPropValue);
  850. ::SysFreeString(bsProperty);
  851. return hr;
  852. }
  853. HRESULT CWbemClassObject::GetProperty(const CString& sProperty, bool& bPropertyValue)
  854. {
  855. TRACEX(_T("CWbemClassObject::GetProperty\n"));
  856. TRACEARGs(sProperty);
  857. ASSERT(m_pIWbemClassObject);
  858. if( m_pIWbemClassObject == NULL )
  859. {
  860. return E_FAIL;
  861. }
  862. BSTR bsProperty = sProperty.AllocSysString();
  863. ASSERT(bsProperty);
  864. if( bsProperty == NULL )
  865. {
  866. return E_FAIL;
  867. }
  868. VARIANT vPropValue;
  869. VariantInit(&vPropValue);
  870. HRESULT hr = m_pIWbemClassObject->Get(bsProperty, 0L, &vPropValue, NULL, NULL);
  871. if( !CHECKHRESULT(hr) )
  872. {
  873. TRACE(_T("FAILED : IWbemClassObject::Get( int ) failed.\n"));
  874. bPropertyValue = false;
  875. ::SysFreeString(bsProperty);
  876. return hr;
  877. }
  878. bPropertyValue = ((V_BOOL(&vPropValue)==VARIANT_TRUE) ? true : false);
  879. VariantClear(&vPropValue);
  880. ::SysFreeString(bsProperty);
  881. return hr;
  882. }
  883. HRESULT CWbemClassObject::GetProperty(const CString& sProperty, float& fPropertyValue)
  884. {
  885. TRACEX(_T("CWbemClassObject::GetProperty\n"));
  886. TRACEARGs(sProperty);
  887. ASSERT(m_pIWbemClassObject);
  888. if( m_pIWbemClassObject == NULL )
  889. {
  890. return E_FAIL;
  891. }
  892. BSTR bsProperty = sProperty.AllocSysString();
  893. ASSERT(bsProperty);
  894. if( bsProperty == NULL )
  895. {
  896. return E_FAIL;
  897. }
  898. VARIANT vPropValue;
  899. VariantInit(&vPropValue);
  900. HRESULT hr = m_pIWbemClassObject->Get(bsProperty, 0L, &vPropValue, NULL, NULL);
  901. if( !CHECKHRESULT(hr) )
  902. {
  903. TRACE(_T("FAILED : IWbemClassObject::Get( float ) failed.\n"));
  904. fPropertyValue = 0;
  905. ::SysFreeString(bsProperty);
  906. return hr;
  907. }
  908. fPropertyValue = V_R4(&vPropValue);
  909. VariantClear(&vPropValue);
  910. ::SysFreeString(bsProperty);
  911. return hr;
  912. }
  913. HRESULT CWbemClassObject::GetProperty(const CString& sProperty, COleSafeArray& ArrayPropertyValue)
  914. {
  915. TRACEX(_T("CWbemClassObject::GetProperty\n"));
  916. TRACEARGs(sProperty);
  917. ASSERT(m_pIWbemClassObject);
  918. if( m_pIWbemClassObject == NULL )
  919. {
  920. return E_FAIL;
  921. }
  922. BSTR bsProperty = sProperty.AllocSysString();
  923. ASSERT(bsProperty);
  924. if( bsProperty == NULL )
  925. {
  926. return E_FAIL;
  927. }
  928. VARIANT vPropValue;
  929. VariantInit(&vPropValue);
  930. HRESULT hr = m_pIWbemClassObject->Get(bsProperty, 0L, &vPropValue, NULL, NULL);
  931. if( !CHECKHRESULT(hr) )
  932. {
  933. TRACE(_T("FAILED : IWbemClassObject::Get( SAFEARRAY ) failed.\n"));
  934. ::SysFreeString(bsProperty);
  935. return hr;
  936. }
  937. if( V_VT(&vPropValue) != VT_NULL )
  938. {
  939. ArrayPropertyValue.Attach(vPropValue);
  940. }
  941. else
  942. {
  943. hr = S_FALSE;
  944. }
  945. VariantClear(&vPropValue);
  946. ::SysFreeString(bsProperty);
  947. return hr;
  948. }
  949. HRESULT CWbemClassObject::GetProperty(const CString& sProperty, CTime& time, bool bConvertToLocalTime /*= true*/)
  950. {
  951. TRACEX(_T("CWbemClassObject::GetProperty\n"));
  952. TRACEARGs(sProperty);
  953. ASSERT(m_pIWbemClassObject);
  954. if( m_pIWbemClassObject == NULL )
  955. {
  956. return E_FAIL;
  957. }
  958. CString sPropertyValue;
  959. BSTR bsProperty = sProperty.AllocSysString();
  960. ASSERT(bsProperty);
  961. if( bsProperty == NULL )
  962. {
  963. return E_FAIL;
  964. }
  965. VARIANT vPropValue;
  966. VariantInit(&vPropValue);
  967. HRESULT hr = m_pIWbemClassObject->Get(bsProperty, 0L, &vPropValue, NULL, NULL);
  968. if( !CHECKHRESULT(hr) )
  969. {
  970. TRACE(_T("FAILED : IWbemClassObject::Get( BSTR ) failed.\n"));
  971. ::SysFreeString(bsProperty);
  972. sPropertyValue.Empty();
  973. return hr;
  974. }
  975. if( V_VT(&vPropValue) != VT_NULL )
  976. {
  977. sPropertyValue = V_BSTR(&vPropValue);
  978. }
  979. else
  980. {
  981. time = CTime();
  982. VariantClear(&vPropValue);
  983. ::SysFreeString(bsProperty);
  984. return S_FALSE;
  985. }
  986. VariantClear(&vPropValue);
  987. ::SysFreeString(bsProperty);
  988. bool bIncomplete = false;
  989. if( sPropertyValue.Find(_T("*")) != -1 )
  990. {
  991. sPropertyValue.Replace(_T('*'),_T('0'));
  992. bIncomplete = true;
  993. }
  994. // parse the DTime format string
  995. SYSTEMTIME st;
  996. int iBias = -1;
  997. int iYear;
  998. int iMonth;
  999. int iDay;
  1000. int iHour;
  1001. int iMinute;
  1002. int iSecond;
  1003. int iMSeconds;
  1004. _stscanf(sPropertyValue,IDS_STRING_DATETIME_FORMAT,&iYear,
  1005. &iMonth,
  1006. &iDay,
  1007. &iHour,
  1008. &iMinute,
  1009. &iSecond,
  1010. &iMSeconds,
  1011. &iBias);
  1012. st.wYear = (WORD)iYear;
  1013. st.wMonth = (WORD)iMonth;
  1014. st.wDay = (WORD)iDay;
  1015. st.wHour = (WORD)iHour;
  1016. st.wMinute = (WORD)iMinute;
  1017. st.wSecond = (WORD)iSecond;
  1018. st.wMilliseconds = (WORD)iMSeconds;
  1019. if( bConvertToLocalTime )
  1020. {
  1021. if( iBias != 0 )
  1022. {
  1023. CTime time = st;
  1024. CTime utc;
  1025. if( iBias < 0 )
  1026. {
  1027. iBias = -iBias;
  1028. }
  1029. CTimeSpan ts(0,0,iBias,0);
  1030. utc = time + ts;
  1031. utc.GetAsSystemTime(st);
  1032. st.wDayOfWeek = 0;
  1033. }
  1034. // adjust to the local time zone
  1035. TIME_ZONE_INFORMATION tzi;
  1036. SYSTEMTIME stLocal;
  1037. GetTimeZoneInformation(&tzi);
  1038. SystemTimeToTzSpecificLocalTime(&tzi,&st,&stLocal);
  1039. time = stLocal;
  1040. }
  1041. else
  1042. {
  1043. if( bIncomplete )
  1044. {
  1045. CTime current = CTime::GetCurrentTime();
  1046. st.wYear = (WORD)current.GetYear();
  1047. st.wMonth = (WORD)current.GetMonth();
  1048. st.wDay = (WORD)current.GetDay();
  1049. }
  1050. time = st;
  1051. }
  1052. sPropertyValue.Empty();
  1053. return hr;
  1054. }
  1055. HRESULT CWbemClassObject::GetProperty(const CString& sProperty, CStringArray& saPropertyValues)
  1056. {
  1057. TRACEX(_T("CWbemClassObject::GetProperty\n"));
  1058. TRACEARGs(sProperty);
  1059. saPropertyValues.RemoveAll();
  1060. COleSafeArray StringArray;
  1061. HRESULT hr = GetProperty(sProperty,StringArray);
  1062. if( hr == S_FALSE )
  1063. {
  1064. return hr;
  1065. }
  1066. // process the strings in the SAFEARRAY
  1067. long lLower = 0L;
  1068. long lUpper = -1L;
  1069. CString sPropertyValue;
  1070. StringArray.GetLBound(1L,&lLower);
  1071. StringArray.GetUBound(1L,&lUpper);
  1072. for( long i = lLower; i <= lUpper; i++ )
  1073. {
  1074. BSTR bsPropertyValue = NULL;
  1075. StringArray.GetElement(&i,&bsPropertyValue);
  1076. sPropertyValue = bsPropertyValue;
  1077. saPropertyValues.Add(sPropertyValue);
  1078. }
  1079. return hr;
  1080. }
  1081. HRESULT CWbemClassObject::SetProperty(const CString& sProperty, CString sPropertyValue)
  1082. {
  1083. TRACEX(_T("CWbemClassObject::SetProperty\n"));
  1084. TRACEARGs(sProperty);
  1085. ASSERT(m_pIWbemClassObject);
  1086. if( m_pIWbemClassObject == NULL )
  1087. {
  1088. return E_FAIL;
  1089. }
  1090. BSTR bsProperty = sProperty.AllocSysString();
  1091. ASSERT(bsProperty);
  1092. if( bsProperty == NULL )
  1093. {
  1094. return E_FAIL;
  1095. }
  1096. VARIANT vPropValue;
  1097. VariantInit(&vPropValue);
  1098. V_VT(&vPropValue) = VT_BSTR;
  1099. V_BSTR(&vPropValue) = sPropertyValue.AllocSysString();
  1100. HRESULT hr = m_pIWbemClassObject->Put(bsProperty, 0L, &vPropValue, NULL);
  1101. if( !CHECKHRESULT(hr) )
  1102. {
  1103. TRACE(_T("FAILED : IWbemClassObject::Put( string ) failed.\n"));
  1104. ::SysFreeString(bsProperty);
  1105. return hr;
  1106. }
  1107. VariantClear(&vPropValue);
  1108. ::SysFreeString(bsProperty);
  1109. return S_OK;
  1110. }
  1111. HRESULT CWbemClassObject::SetProperty(const CString& sProperty, int iPropertyValue)
  1112. {
  1113. TRACEX(_T("CWbemClassObject::SetProperty\n"));
  1114. TRACEARGs(sProperty);
  1115. TRACEARGn(iPropertyValue);
  1116. ASSERT(m_pIWbemClassObject);
  1117. if( m_pIWbemClassObject == NULL )
  1118. {
  1119. return E_FAIL;
  1120. }
  1121. BSTR bsProperty = sProperty.AllocSysString();
  1122. ASSERT(bsProperty);
  1123. if( bsProperty == NULL )
  1124. {
  1125. return E_FAIL;
  1126. }
  1127. VARIANT vPropValue;
  1128. VariantInit(&vPropValue);
  1129. V_VT(&vPropValue) = VT_I4;
  1130. V_I4(&vPropValue) = iPropertyValue;
  1131. HRESULT hr = m_pIWbemClassObject->Put(bsProperty, 0L, &vPropValue, NULL);
  1132. if( !CHECKHRESULT(hr) )
  1133. {
  1134. TRACE(_T("FAILED : IWbemClassObject::Put( int ) failed.\n"));
  1135. TRACEARGn(hr);
  1136. ::SysFreeString(bsProperty);
  1137. return hr;
  1138. }
  1139. VariantClear(&vPropValue);
  1140. ::SysFreeString(bsProperty);
  1141. return S_OK;
  1142. }
  1143. HRESULT CWbemClassObject::SetProperty(const CString& sProperty, bool bPropertyValue)
  1144. {
  1145. TRACEX(_T("CWbemClassObject::SetProperty\n"));
  1146. TRACEARGs(sProperty);
  1147. TRACEARGn(bPropertyValue);
  1148. ASSERT(m_pIWbemClassObject);
  1149. if( m_pIWbemClassObject == NULL )
  1150. {
  1151. return E_FAIL;
  1152. }
  1153. BSTR bsProperty = sProperty.AllocSysString();
  1154. ASSERT(bsProperty);
  1155. if( bsProperty == NULL )
  1156. {
  1157. return E_FAIL;
  1158. }
  1159. VARIANT vPropValue;
  1160. VariantInit(&vPropValue);
  1161. V_VT(&vPropValue) = VT_BOOL;
  1162. V_BOOL(&vPropValue) = bPropertyValue ? VARIANT_TRUE : VARIANT_FALSE;
  1163. HRESULT hr = m_pIWbemClassObject->Put(bsProperty, 0L, &vPropValue, NULL);
  1164. if( !CHECKHRESULT(hr) )
  1165. {
  1166. TRACE(_T("FAILED : IWbemClassObject::Put( int ) failed.\n"));
  1167. TRACEARGn(hr);
  1168. ::SysFreeString(bsProperty);
  1169. return hr;
  1170. }
  1171. VariantClear(&vPropValue);
  1172. ::SysFreeString(bsProperty);
  1173. return S_OK;
  1174. }
  1175. HRESULT CWbemClassObject::SetProperty(const CString& sProperty, float fPropertyValue)
  1176. {
  1177. TRACEX(_T("CWbemClassObject::SetProperty\n"));
  1178. TRACEARGs(sProperty);
  1179. TRACEARGn(fPropertyValue);
  1180. ASSERT(m_pIWbemClassObject);
  1181. if( m_pIWbemClassObject == NULL )
  1182. {
  1183. return E_FAIL;
  1184. }
  1185. BSTR bsProperty = sProperty.AllocSysString();
  1186. ASSERT(bsProperty);
  1187. if( bsProperty == NULL )
  1188. {
  1189. return E_FAIL;
  1190. }
  1191. VARIANT vPropValue;
  1192. VariantInit(&vPropValue);
  1193. V_VT(&vPropValue) = VT_R4;
  1194. V_R4(&vPropValue) = fPropertyValue;
  1195. HRESULT hr = m_pIWbemClassObject->Put(bsProperty, 0L, &vPropValue, NULL);
  1196. if( !CHECKHRESULT(hr) )
  1197. {
  1198. TRACE(_T("FAILED : IWbemClassObject::Put( float ) failed."));
  1199. ::SysFreeString(bsProperty);
  1200. return hr;
  1201. }
  1202. VariantClear(&vPropValue);
  1203. ::SysFreeString(bsProperty);
  1204. return S_OK;
  1205. }
  1206. HRESULT CWbemClassObject::SetProperty(const CString& sProperty, CTime time, bool bConvertToGMTTime /*= true*/)
  1207. {
  1208. TRACEX(_T("CWbemClassObject::SetProperty\n"));
  1209. TRACEARGs(sProperty);
  1210. ASSERT(m_pIWbemClassObject);
  1211. if( m_pIWbemClassObject == NULL )
  1212. {
  1213. return E_FAIL;
  1214. }
  1215. BSTR bsProperty = sProperty.AllocSysString();
  1216. ASSERT(bsProperty);
  1217. if( bsProperty == NULL )
  1218. {
  1219. return E_FAIL;
  1220. }
  1221. CString sPropertyValue;
  1222. if( bConvertToGMTTime )
  1223. {
  1224. // format the time to comply with WMI DTime string type
  1225. tm tmGmt = *(time.GetGmtTm());
  1226. sPropertyValue.Format(IDS_STRING_DATETIME_FORMAT2,tmGmt.tm_year,
  1227. tmGmt.tm_mon,
  1228. tmGmt.tm_mday,
  1229. tmGmt.tm_hour,
  1230. tmGmt.tm_min,
  1231. tmGmt.tm_sec,
  1232. 0,
  1233. _T("+000"));
  1234. }
  1235. else
  1236. {
  1237. sPropertyValue.Format(IDS_STRING_DATETIME_FORMAT2,time.GetYear(),
  1238. time.GetMonth(),
  1239. time.GetDay(),
  1240. time.GetHour(),
  1241. time.GetMinute(),
  1242. time.GetSecond(),
  1243. 0,
  1244. _T("+000"));
  1245. }
  1246. VARIANT vPropValue;
  1247. VariantInit(&vPropValue);
  1248. V_VT(&vPropValue) = VT_BSTR;
  1249. V_BSTR(&vPropValue) = sPropertyValue.AllocSysString();
  1250. HRESULT hr = m_pIWbemClassObject->Put(bsProperty, 0L, &vPropValue, NULL);
  1251. if( !CHECKHRESULT(hr) )
  1252. {
  1253. TRACE(_T("FAILED : IWbemClassObject::Put( string ) failed.\n"));
  1254. ::SysFreeString(bsProperty);
  1255. return hr;
  1256. }
  1257. VariantClear(&vPropValue);
  1258. ::SysFreeString(bsProperty);
  1259. return S_OK;
  1260. }
  1261. HRESULT CWbemClassObject::SetProperty(const CString& sProperty, COleSafeArray& ArrayPropertyValue)
  1262. {
  1263. TRACEX(_T("CWbemClassObject::SetProperty\n"));
  1264. TRACEARGs(sProperty);
  1265. ASSERT(m_pIWbemClassObject);
  1266. if( m_pIWbemClassObject == NULL )
  1267. {
  1268. return E_FAIL;
  1269. }
  1270. BSTR bsProperty = sProperty.AllocSysString();
  1271. ASSERT(bsProperty);
  1272. if( bsProperty == NULL )
  1273. {
  1274. return E_FAIL;
  1275. }
  1276. LPVARIANT lpvPropValue = LPVARIANT(ArrayPropertyValue);
  1277. HRESULT hr = m_pIWbemClassObject->Put(bsProperty, 0L, lpvPropValue, NULL);
  1278. if( !CHECKHRESULT(hr) )
  1279. {
  1280. TRACE(_T("FAILED : IWbemClassObject::Put( SAFEARRAY ) failed.\n"));
  1281. TRACEARGn(hr);
  1282. ::SysFreeString(bsProperty);
  1283. return hr;
  1284. }
  1285. ::SysFreeString(bsProperty);
  1286. return S_OK;
  1287. }
  1288. HRESULT CWbemClassObject::SetProperty(const CString& sProperty, const CStringArray& saPropertyValues)
  1289. {
  1290. TRACEX(_T("CWbemClassObject::SetProperty\n"));
  1291. TRACEARGs(sProperty);
  1292. //----------------------------------------------------------------------------------------
  1293. // v-marfin 62531b : Allow an empty array as a means of removing a property of type array
  1294. if( saPropertyValues.GetSize() == 0 )
  1295. {
  1296. HRESULT hr=0;
  1297. VARIANT vNULL;
  1298. VariantInit(&vNULL);
  1299. hr = VariantChangeType(&vNULL,&vNULL,0,VT_NULL);
  1300. hr = SetRawProperty(sProperty,vNULL);
  1301. return hr;
  1302. }
  1303. //----------------------------------------------------------------------------------------
  1304. COleSafeArray StringArray;
  1305. StringArray.CreateOneDim(VT_BSTR,(int)saPropertyValues.GetSize());
  1306. // process the strings in the SAFEARRAY
  1307. CString sPropertyValue;
  1308. for( long i = 0; i < saPropertyValues.GetSize(); i++ )
  1309. {
  1310. BSTR bsPropertyValue = saPropertyValues[i].AllocSysString();
  1311. StringArray.PutElement(&i,bsPropertyValue);
  1312. ::SysFreeString(bsPropertyValue);
  1313. }
  1314. HRESULT hr = SetProperty(sProperty,StringArray);
  1315. return hr;
  1316. }
  1317. inline HRESULT CWbemClassObject::Connect(IWbemServices*& pServices)
  1318. {
  1319. TRACEX(_T("CWbemClassObject::Connect\n"));
  1320. TRACEARGn(pServices);
  1321. HRESULT hr = S_OK;
  1322. if( m_sNamespace.IsEmpty() ) // connect to a system
  1323. {
  1324. BOOL bAvail;
  1325. if( (hr = CnxGetConnection(m_sMachineName,pServices,bAvail)) != S_OK )
  1326. {
  1327. TRACE(_T("FAILED : Could not retrieve a connection for the machine. Failed.\n"));
  1328. return hr;
  1329. }
  1330. if( ! bAvail )
  1331. return E_FAIL;
  1332. }
  1333. else // connect to a specific namespace
  1334. {
  1335. if( (hr = CnxConnectToNamespace(m_sNamespace,pServices)) != S_OK )
  1336. {
  1337. TRACE(_T("FAILED : Could not retrieve a connection for the machine. Failed.\n"));
  1338. return hr;
  1339. }
  1340. }
  1341. SetBlanket(pServices);
  1342. return hr;
  1343. }
  1344. inline HRESULT CWbemClassObject::SetBlanket(LPUNKNOWN pIUnk)
  1345. {
  1346. return CoSetProxyBlanket( pIUnk,
  1347. RPC_C_AUTHN_WINNT, // NTLM authentication service
  1348. RPC_C_AUTHZ_NONE, // default authorization service...
  1349. NULL, // no mutual authentication
  1350. RPC_C_AUTHN_LEVEL_CONNECT, // authentication level
  1351. RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
  1352. NULL, // use current token
  1353. EOAC_NONE ); // no special capabilities
  1354. }
  1355. inline void CWbemClassObject::DisplayErrorMsgBox(HRESULT hr)
  1356. {
  1357. // construct the path and load wbemcomn.dll for error messages
  1358. TCHAR szWinDir[_MAX_PATH];
  1359. GetWindowsDirectory(szWinDir,_MAX_PATH);
  1360. CString sModulePath;
  1361. sModulePath.Format(_T("%s\\SYSTEM32\\WBEM\\WBEMCOMN.DLL"),szWinDir);
  1362. HMODULE hModule = LoadLibrary(sModulePath);
  1363. TCHAR szMsg[_MAX_PATH*4];
  1364. DWORD dwCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_IGNORE_INSERTS,
  1365. hModule,
  1366. hr,
  1367. 0,
  1368. szMsg,
  1369. _MAX_PATH*4*sizeof(TCHAR),
  1370. NULL
  1371. );
  1372. CString sMsg;
  1373. CString sNamespace = GetNamespace();
  1374. if( sNamespace.IsEmpty() )
  1375. {
  1376. sNamespace.Format(IDS_STRING_HEALTHMON_ROOT,GetMachineName());
  1377. }
  1378. if( dwCount )
  1379. {
  1380. sMsg.Format(IDS_STRING_WMI_ERROR,sNamespace,hr,szMsg);
  1381. }
  1382. else
  1383. {
  1384. CString sUnknown;
  1385. sUnknown.LoadString(IDS_STRING_UNKNOWN);
  1386. sMsg.Format(IDS_STRING_WMI_ERROR,sNamespace,hr,sUnknown);
  1387. }
  1388. AfxMessageBox(sMsg);
  1389. FreeLibrary(hModule);
  1390. }