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.

2489 lines
62 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. // Instance.CPP
  6. //
  7. // Purpose: Implementation of CInstance class
  8. //
  9. //***************************************************************************
  10. #include "precomp.h"
  11. #include <BrodCast.h>
  12. #include <assertbreak.h>
  13. #include <stopwatch.h>
  14. #include "FWStrings.h"
  15. #define DEPRECATED 1
  16. ////////////////////////////////////////////////////////////////////////
  17. //
  18. // Function: CInstance ctor
  19. //
  20. //
  21. //
  22. // Inputs: IWbemClassObject* - the class we want to wrap
  23. // MethodContext* - since the context is shared, this will be addreffed
  24. // Outputs:
  25. //
  26. // Return:
  27. //
  28. // Comments: pointers should not be NULL
  29. //
  30. ////////////////////////////////////////////////////////////////////////
  31. CInstance::CInstance(IWbemClassObject* piClassObject, MethodContext* pMethodContext)
  32. : m_nRefCount( 1 )
  33. {
  34. ASSERT_BREAK(piClassObject != NULL);
  35. ASSERT_BREAK(pMethodContext != NULL);
  36. // Both these values will be released in the destructor, so they both oughta
  37. // be AddRefed. Note that they are
  38. m_piClassObject = piClassObject;
  39. if ( NULL != piClassObject )
  40. { // this, however, is a copy
  41. m_piClassObject->AddRef();
  42. }
  43. m_pMethodContext = pMethodContext;
  44. if (pMethodContext)
  45. { // this, however, is a copy
  46. m_pMethodContext->AddRef();
  47. }
  48. }
  49. ////////////////////////////////////////////////////////////////////////
  50. //
  51. // Function: CInstance Dtor
  52. //
  53. //
  54. //
  55. // Inputs:
  56. //
  57. // Outputs:
  58. //
  59. // Return:
  60. //
  61. // Comments:
  62. //
  63. ////////////////////////////////////////////////////////////////////////
  64. CInstance::~CInstance()
  65. {
  66. if ( NULL != m_piClassObject )
  67. {
  68. m_piClassObject->Release();
  69. }
  70. if ( NULL != m_pMethodContext )
  71. {
  72. m_pMethodContext->Release();
  73. }
  74. }
  75. ////////////////////////////////////////////////////////////////////////
  76. //
  77. // Function: CInstance::AddRef
  78. //
  79. // Increments our reference count.
  80. //
  81. // Inputs: None.
  82. //
  83. // Outputs: None.
  84. //
  85. // Return: New Reference Count.
  86. //
  87. // Comments: We may want to go to an Interlocked Inc/Dec model at
  88. // some point if Thread Safety on these objects becomes
  89. // an issue.
  90. //
  91. ////////////////////////////////////////////////////////////////////////
  92. LONG CInstance::AddRef( void )
  93. {
  94. return InterlockedIncrement(&m_nRefCount);
  95. }
  96. ////////////////////////////////////////////////////////////////////////
  97. //
  98. // Function: CInstance::Release
  99. //
  100. // Decrements our reference count.
  101. //
  102. // Inputs: None.
  103. //
  104. // Outputs: None.
  105. //
  106. // Return: New Reference Count.
  107. //
  108. // Comments: Deletes the object when the ref count hits 0. We may
  109. // want to go to an Interlocked Inc/Dec model at some
  110. // point if Thread Safety on these objects becomes an issue.
  111. //
  112. ////////////////////////////////////////////////////////////////////////
  113. LONG CInstance::Release( void )
  114. {
  115. LONG nRet = InterlockedDecrement(&m_nRefCount);
  116. ASSERT_BREAK(nRet >= 0);
  117. if ( 0 == nRet )
  118. {
  119. delete this;
  120. }
  121. else if (nRet < 0)
  122. {
  123. // Duplicate release. Let's try to dump the stack
  124. DWORD t_stack[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  125. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  126. #ifdef _X86_
  127. DWORD *dwEBP;
  128. _asm {
  129. mov dwEBP, ebp
  130. }
  131. dwEBP += 1;
  132. memcpy(&t_stack, dwEBP, sizeof(t_stack));
  133. #endif
  134. CHString sMsg;
  135. sMsg.Format(L"Duplicate release: %08x %08x %08x %08x %08x %08x %08x %08x "
  136. L"%08x %08x %08x %08x %08x %08x %08x %08x "
  137. L"%08x %08x %08x %08x %08x %08x %08x %08x "
  138. L"%08x %08x %08x %08x %08x %08x %08x %08x ",
  139. t_stack[0], t_stack[1], t_stack[2], t_stack[3],
  140. t_stack[4], t_stack[5], t_stack[6], t_stack[7],
  141. t_stack[8], t_stack[9], t_stack[10], t_stack[11],
  142. t_stack[12], t_stack[13], t_stack[14], t_stack[15],
  143. t_stack[16], t_stack[17], t_stack[18], t_stack[19],
  144. t_stack[20], t_stack[21], t_stack[22], t_stack[23],
  145. t_stack[24], t_stack[25], t_stack[26], t_stack[27],
  146. t_stack[28], t_stack[29], t_stack[30], t_stack[31]
  147. );
  148. LogErrorMessage(sMsg);
  149. }
  150. return nRet;
  151. }
  152. ////////////////////////////////////////////////////////////////////////
  153. //
  154. // Function: Commit
  155. //
  156. // returns this CInstance to CIMOM
  157. // will stuff it into the cache someday
  158. // Inputs:
  159. //
  160. // Outputs:
  161. //
  162. // Return:
  163. //
  164. // Comments:
  165. //
  166. ////////////////////////////////////////////////////////////////////////
  167. HRESULT CInstance::Commit(void)
  168. {
  169. return m_pMethodContext->Commit( this );
  170. }
  171. IWbemClassObject* CInstance::GetClassObjectInterface()
  172. {
  173. m_piClassObject->AddRef();
  174. return m_piClassObject;
  175. }
  176. // reference counting //
  177. // string support //
  178. ////////////////////////////////////////////////////////////////////////
  179. //
  180. // Function: Set
  181. //
  182. //
  183. //
  184. // Inputs: Name of property to set
  185. // string to be set
  186. //
  187. // Outputs:
  188. //
  189. // Return: false if you try to set a property that is not a string type
  190. //
  191. // Comments:
  192. //
  193. ////////////////////////////////////////////////////////////////////////
  194. bool CInstance::SetWCHARSplat( LPCWSTR name, LPCWSTR pStr)
  195. {
  196. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  197. bool bRet = false;
  198. DWORD dwLastErr = 0;
  199. ASSERT_BREAK(name != NULL);
  200. if (m_piClassObject && name)
  201. {
  202. // Variant_t handles the VariantInit/VariantClear
  203. variant_t v(pStr);
  204. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  205. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  206. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  207. ASSERT_BREAK(SUCCEEDED(hr));
  208. bRet = (bool)SUCCEEDED(hr);
  209. if (!bRet)
  210. {
  211. dwLastErr = (hr);
  212. LogError(IDS_FAILED, IDS_SETCHSTRING, name, hr);
  213. }
  214. }
  215. else
  216. {
  217. if (m_piClassObject)
  218. {
  219. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  220. LogError(IDS_InParam, IDS_SETCHSTRING);
  221. }
  222. else
  223. {
  224. dwLastErr = (WBEM_E_FAILED);
  225. LogError(IDS_NOCLASS, IDS_SETCHSTRING);
  226. }
  227. }
  228. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  229. SetLastError(dwLastErr);
  230. return bRet;
  231. }
  232. ////////////////////////////////////////////////////////////////////////
  233. //
  234. // Function: Set
  235. //
  236. //
  237. //
  238. // Inputs: Name of property to set to VT_NULL
  239. //
  240. // Outputs:
  241. //
  242. // Return: false if you try to set a property that is not a string type
  243. //
  244. // Comments:
  245. //
  246. ////////////////////////////////////////////////////////////////////////
  247. bool CInstance::SetNull(LPCWSTR name)
  248. {
  249. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  250. bool bRet = false;
  251. DWORD dwLastErr = 0;
  252. ASSERT_BREAK(name != NULL);
  253. if (m_piClassObject && name)
  254. {
  255. // Variant_t handles the VariantInit/VariantClear
  256. variant_t v;
  257. v.vt = VT_NULL ;
  258. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  259. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  260. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  261. ASSERT_BREAK(SUCCEEDED(hr));
  262. bRet = (bool)SUCCEEDED(hr);
  263. if (!bRet)
  264. {
  265. dwLastErr = (hr);
  266. LogError(IDS_FAILED, IDS_SETCHSTRING, name, hr);
  267. }
  268. }
  269. else
  270. {
  271. if (m_piClassObject)
  272. {
  273. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  274. LogError(IDS_InParam, IDS_SETCHSTRING);
  275. }
  276. else
  277. {
  278. dwLastErr = (WBEM_E_FAILED);
  279. LogError(IDS_NOCLASS, IDS_SETCHSTRING);
  280. }
  281. }
  282. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  283. SetLastError(dwLastErr);
  284. return bRet;
  285. }
  286. ////////////////////////////////////////////////////////////////////////
  287. //
  288. // Function: SetStringArray
  289. //
  290. //
  291. //
  292. // Inputs: Name of property to set
  293. // string to be set
  294. //
  295. // Outputs:
  296. //
  297. // Return: false if you try to set a property that is not a string array type
  298. //
  299. // Comments:
  300. //
  301. ////////////////////////////////////////////////////////////////////////
  302. bool CInstance::SetStringArray(LPCWSTR name, const SAFEARRAY &strArray)
  303. {
  304. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  305. bool bRet = false;
  306. DWORD dwLastErr = 0;
  307. ASSERT_BREAK(name != NULL);
  308. if (m_piClassObject && name)
  309. {
  310. SAFEARRAY *t_SafeArray = NULL;
  311. HRESULT hr = SafeArrayCopy ( ( SAFEARRAY * ) & strArray , &t_SafeArray );
  312. if ( SUCCEEDED ( hr ) )
  313. {
  314. // Variant_t handles the VariantInit/VariantClear
  315. variant_t v;
  316. v.vt = VT_BSTR | VT_ARRAY ;
  317. v.parray = t_SafeArray ;
  318. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  319. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  320. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  321. ASSERT_BREAK(SUCCEEDED(hr));
  322. bRet = (bool)SUCCEEDED(hr);
  323. if (!bRet)
  324. {
  325. dwLastErr = (hr);
  326. LogError(IDS_FAILED, IDS_SetStringArray, name, hr);
  327. }
  328. }
  329. else
  330. {
  331. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  332. }
  333. }
  334. else
  335. {
  336. if (m_piClassObject)
  337. {
  338. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  339. LogError(IDS_InParam, IDS_SetStringArray);
  340. }
  341. else
  342. {
  343. dwLastErr = (WBEM_E_FAILED);
  344. LogError(IDS_NOCLASS, IDS_SetStringArray);
  345. }
  346. }
  347. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  348. SetLastError(dwLastErr);
  349. return bRet;
  350. }
  351. ////////////////////////////////////////////////////////////////////////
  352. //
  353. // Function: Get (CHString)
  354. //
  355. //
  356. //
  357. // Inputs: Name of property to retrieve
  358. // CHString buffer to receive value
  359. // Outputs:
  360. //
  361. // Return: false if you try to get a property that is not a string compatible type
  362. //
  363. // Comments:
  364. //
  365. ////////////////////////////////////////////////////////////////////////
  366. bool CInstance::GetCHString(LPCWSTR name, CHString& str) const
  367. {
  368. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  369. bool bRet = false;
  370. DWORD dwLastErr = 0;
  371. ASSERT_BREAK(name != NULL);
  372. if (m_piClassObject && name)
  373. {
  374. // Variant_t handles the VariantInit/VariantClear
  375. variant_t v;
  376. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  377. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  378. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  379. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  380. // null properties from logging an error.
  381. BOOL bSuccess = SUCCEEDED(hr) && (v.vt == VT_BSTR || v.vt == VT_NULL);
  382. ASSERT_BREAK(bSuccess);
  383. if (SUCCEEDED(hr))
  384. {
  385. if ((v.vt == VT_BSTR) && (v.bstrVal != NULL))
  386. {
  387. str = v.bstrVal;
  388. bRet = true;
  389. }
  390. else
  391. {
  392. dwLastErr = (WBEM_S_NO_MORE_DATA);
  393. }
  394. }
  395. if (!bSuccess)
  396. {
  397. if (SUCCEEDED(hr))
  398. {
  399. hr = WBEM_E_TYPE_MISMATCH;
  400. }
  401. dwLastErr = (hr);
  402. LogError(IDS_FAILED, IDS_GETCHSTRING, name, hr);
  403. }
  404. }
  405. else
  406. {
  407. if (m_piClassObject)
  408. {
  409. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  410. LogError(IDS_InParam, IDS_GETCHSTRING);
  411. }
  412. else
  413. {
  414. dwLastErr = (WBEM_E_FAILED);
  415. LogError(IDS_NOCLASS, IDS_GETCHSTRING);
  416. }
  417. }
  418. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  419. SetLastError(dwLastErr);
  420. return bRet;
  421. }
  422. ////////////////////////////////////////////////////////////////////////
  423. //
  424. // Function: GetStringArray
  425. //
  426. //
  427. //
  428. // Inputs: Name of property to retrieve
  429. // SAFEARRAY *& strArray
  430. // Outputs:
  431. //
  432. // Return: false if you try to get a property that is not a string array compatible type
  433. //
  434. // Comments:
  435. //
  436. ////////////////////////////////////////////////////////////////////////
  437. bool CInstance::GetStringArray(LPCWSTR name, SAFEARRAY *& strArray) const
  438. {
  439. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  440. bool bRet = false;
  441. DWORD dwLastErr = 0;
  442. ASSERT_BREAK(name != NULL);
  443. if (m_piClassObject && name)
  444. {
  445. // Variant_t handles the VariantInit/VariantClear
  446. variant_t v;
  447. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  448. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  449. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  450. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  451. // null properties from logging an error.
  452. BOOL bSuccess = SUCCEEDED(hr) && (v.vt == (VT_BSTR|VT_ARRAY) ||
  453. v.vt == VT_NULL);
  454. ASSERT_BREAK(bSuccess);
  455. if (SUCCEEDED(hr))
  456. {
  457. if ((v.vt == (VT_BSTR|VT_ARRAY)) && (v.parray != NULL ))
  458. {
  459. if ( SUCCEEDED ( SafeArrayCopy ( v.parray , ( SAFEARRAY ** ) &strArray ) ) )
  460. {
  461. bRet = true ;
  462. }
  463. else
  464. {
  465. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  466. }
  467. }
  468. else
  469. {
  470. dwLastErr = (WBEM_S_NO_MORE_DATA);
  471. }
  472. }
  473. if (!bSuccess)
  474. {
  475. if (SUCCEEDED(hr))
  476. {
  477. hr = WBEM_E_TYPE_MISMATCH;
  478. }
  479. dwLastErr = (hr);
  480. LogError(IDS_FAILED, IDS_GetStringArray, name, hr);
  481. }
  482. }
  483. else
  484. {
  485. if (m_piClassObject)
  486. {
  487. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  488. LogError(IDS_InParam, IDS_GetStringArray);
  489. }
  490. else
  491. {
  492. dwLastErr = (WBEM_E_FAILED);
  493. LogError(IDS_NOCLASS, IDS_GetStringArray);
  494. }
  495. }
  496. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  497. SetLastError(dwLastErr);
  498. return bRet;
  499. }
  500. bool CInstance::GetWCHAR(LPCWSTR name, WCHAR **pW) const
  501. {
  502. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  503. bool bRet = false;
  504. DWORD dwLastErr = 0;
  505. if (m_piClassObject && name)
  506. {
  507. // Variant_t handles the VariantInit/VariantClear
  508. variant_t v;
  509. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  510. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  511. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  512. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  513. // null properties from logging an error.
  514. BOOL bSuccess = SUCCEEDED(hr) && (v.vt == VT_BSTR || v.vt == VT_NULL);
  515. ASSERT_BREAK(bSuccess);
  516. if (SUCCEEDED(hr))
  517. {
  518. if ((v.vt == VT_BSTR) && (v.bstrVal != NULL))
  519. {
  520. *pW = (WCHAR *)malloc((wcslen(v.bstrVal) + 1)*sizeof(WCHAR));
  521. if (*pW == NULL)
  522. {
  523. VariantClear(&v);
  524. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  525. }
  526. wcscpy(*pW, v.bstrVal);
  527. bRet = true;
  528. }
  529. else
  530. {
  531. dwLastErr = (WBEM_S_NO_MORE_DATA);
  532. }
  533. }
  534. if (!bSuccess)
  535. {
  536. if (SUCCEEDED(hr))
  537. {
  538. hr = WBEM_E_TYPE_MISMATCH;
  539. }
  540. dwLastErr = (hr);
  541. LogError(IDS_FAILED, IDS_GETCHSTRING, name, hr);
  542. }
  543. }
  544. else
  545. {
  546. if (m_piClassObject)
  547. {
  548. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  549. LogError(IDS_InParam, IDS_GETCHSTRING);
  550. }
  551. else
  552. {
  553. dwLastErr = (WBEM_E_FAILED);
  554. LogError(IDS_NOCLASS, IDS_GETCHSTRING);
  555. }
  556. }
  557. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  558. SetLastError(dwLastErr);
  559. return bRet;
  560. }
  561. // WORD support //
  562. ////////////////////////////////////////////////////////////////////////
  563. //
  564. // Function: Set (WORD)
  565. //
  566. //
  567. //
  568. // Inputs: Name of property to set
  569. // WORD to be set
  570. // Outputs:
  571. //
  572. // Return: false if you try to set a property that is not a WORD compatible type
  573. //
  574. // Comments:
  575. //
  576. ////////////////////////////////////////////////////////////////////////
  577. bool CInstance::SetWORD(LPCWSTR name, WORD w)
  578. {
  579. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  580. bool bRet = false;
  581. DWORD dwLastErr = 0;
  582. ASSERT_BREAK(name);
  583. if (m_piClassObject && name)
  584. {
  585. VARIANT v;
  586. VariantInit(&v);
  587. v.vt = VT_I4;
  588. v.lVal = (long)w;
  589. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  590. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  591. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  592. ASSERT_BREAK(SUCCEEDED(hr));
  593. bRet = (bool)SUCCEEDED(hr);
  594. VariantClear(&v);
  595. if (!bRet)
  596. {
  597. dwLastErr = (hr);
  598. LogError(IDS_FAILED, IDS_SETWORD, name, hr);
  599. }
  600. }
  601. else
  602. {
  603. if (m_piClassObject)
  604. {
  605. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  606. LogError(IDS_InParam, IDS_SETWORD);
  607. }
  608. else
  609. {
  610. dwLastErr = (WBEM_E_FAILED);
  611. LogError(IDS_NOCLASS, IDS_SETWORD);
  612. }
  613. }
  614. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  615. SetLastError(dwLastErr);
  616. return bRet;
  617. }
  618. ////////////////////////////////////////////////////////////////////////
  619. //
  620. // Function: Get (WORD)
  621. //
  622. //
  623. //
  624. // Inputs: Name of property to retrieve
  625. // WORD buffer to receive value
  626. // Outputs:
  627. //
  628. // Return: false if you try to get a property that is not a WORD compatible type
  629. //
  630. // Comments:
  631. //
  632. ////////////////////////////////////////////////////////////////////////
  633. bool CInstance::GetWORD(LPCWSTR name, WORD& w) const
  634. {
  635. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  636. bool bRet = false;
  637. DWORD dwLastErr = 0;
  638. ASSERT_BREAK(name != NULL);
  639. if (m_piClassObject && name)
  640. {
  641. // Variant_t handles the VariantInit/VariantClear
  642. variant_t v;
  643. CIMTYPE vtType;
  644. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  645. HRESULT hr = m_piClassObject->Get(name, 0, &v, &vtType, NULL);
  646. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  647. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  648. // null properties from logging an error.
  649. BOOL bSuccess = SUCCEEDED(hr) && CIM_UINT16 == vtType;
  650. ASSERT_BREAK(bSuccess);
  651. if (SUCCEEDED(hr))
  652. {
  653. // the CIM type is important here
  654. if( (v.vt == VT_I4) && (CIM_UINT16 == vtType) )
  655. {
  656. w = (WORD)v.lVal;
  657. bRet = true;
  658. }
  659. else
  660. {
  661. dwLastErr = (WBEM_S_NO_MORE_DATA);
  662. }
  663. }
  664. if (!bSuccess)
  665. {
  666. if (SUCCEEDED(hr))
  667. {
  668. hr = WBEM_E_TYPE_MISMATCH;
  669. }
  670. dwLastErr = (hr);
  671. LogError(IDS_FAILED, IDS_GETWORD, name, hr);
  672. }
  673. }
  674. else
  675. {
  676. if (m_piClassObject)
  677. {
  678. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  679. LogError(IDS_InParam, IDS_GETWORD);
  680. }
  681. else
  682. {
  683. dwLastErr = (WBEM_E_FAILED);
  684. LogError(IDS_NOCLASS, IDS_GETWORD);
  685. }
  686. }
  687. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  688. SetLastError(dwLastErr);
  689. return bRet;
  690. }
  691. // DWORD support //
  692. ////////////////////////////////////////////////////////////////////////
  693. //
  694. // Function: Set (DWORD)
  695. //
  696. //
  697. //
  698. // Inputs: Name of property to set
  699. // DWORD to be set
  700. // Outputs:
  701. //
  702. // Return: false if you try to set a property that is not a DWORD compatible type
  703. //
  704. // Comments:
  705. //
  706. ////////////////////////////////////////////////////////////////////////
  707. bool CInstance::SetDWORD(LPCWSTR name, DWORD d)
  708. {
  709. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  710. bool bRet = false;
  711. DWORD dwLastErr = 0;
  712. if (m_piClassObject && name)
  713. {
  714. VARIANT v;
  715. VariantInit(&v);
  716. v.vt = VT_I4;
  717. v.lVal = (long)d;
  718. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  719. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  720. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  721. ASSERT_BREAK(SUCCEEDED(hr));
  722. bRet = (bool)SUCCEEDED(hr);
  723. if (!bRet)
  724. {
  725. dwLastErr = (hr);
  726. LogError(IDS_FAILED, IDS_SETDWORD, name, hr);
  727. }
  728. VariantClear(&v);
  729. }
  730. else
  731. {
  732. if (m_piClassObject)
  733. {
  734. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  735. LogError(IDS_InParam, IDS_SETDWORD);
  736. }
  737. else
  738. {
  739. dwLastErr = (WBEM_E_FAILED);
  740. LogError(IDS_NOCLASS, IDS_SETDWORD);
  741. }
  742. }
  743. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  744. SetLastError(dwLastErr);
  745. return bRet;
  746. }
  747. ////////////////////////////////////////////////////////////////////////
  748. //
  749. // Function: Get (DWORD)
  750. //
  751. //
  752. //
  753. // Inputs: Name of property to retrieve
  754. // DWORD buffer to receive value
  755. // Outputs:
  756. //
  757. // Return: false if you try to get a property that is not a DWORD compatible type
  758. //
  759. // Comments:
  760. //
  761. ////////////////////////////////////////////////////////////////////////
  762. bool CInstance::GetDWORD(LPCWSTR name, DWORD& d) const
  763. {
  764. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  765. bool bRet = false;
  766. DWORD dwLastErr = 0;
  767. if (m_piClassObject && name)
  768. {
  769. // Variant_t handles the VariantInit/VariantClear
  770. variant_t v;
  771. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  772. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  773. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  774. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  775. // null properties from logging an error.
  776. BOOL bSuccess = SUCCEEDED(hr) && (v.vt == VT_I4 || v.vt == VT_NULL);
  777. ASSERT_BREAK(bSuccess);
  778. if (SUCCEEDED(hr))
  779. {
  780. if (v.vt == VT_I4)
  781. {
  782. d = (DWORD)v.lVal;
  783. bRet = true;
  784. }
  785. else
  786. {
  787. dwLastErr = (WBEM_S_NO_MORE_DATA);
  788. }
  789. }
  790. if (!bSuccess)
  791. {
  792. if (SUCCEEDED(hr))
  793. {
  794. hr = WBEM_E_TYPE_MISMATCH;
  795. }
  796. dwLastErr = (hr);
  797. LogError(IDS_FAILED, IDS_GETDWORD, name, hr);
  798. }
  799. }
  800. else
  801. {
  802. if (m_piClassObject)
  803. {
  804. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  805. LogError(IDS_InParam, IDS_GETDWORD);
  806. }
  807. else
  808. {
  809. dwLastErr = (WBEM_E_FAILED);
  810. LogError(IDS_NOCLASS, IDS_GETDWORD);
  811. }
  812. }
  813. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  814. SetLastError(dwLastErr);
  815. return bRet;
  816. }
  817. // DOUBLE support //
  818. ////////////////////////////////////////////////////////////////////////
  819. //
  820. // Function: Set (DOUBLE)
  821. //
  822. //
  823. //
  824. // Inputs: Name of property to set
  825. // DOUBLE to be set
  826. // Outputs:
  827. //
  828. // Return: false if you try to set a property that is not a DOUBLE compatible type
  829. //
  830. // Comments:
  831. //
  832. ////////////////////////////////////////////////////////////////////////
  833. bool CInstance::SetDOUBLE(LPCWSTR name, DOUBLE dub)
  834. {
  835. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  836. bool bRet = false;
  837. DWORD dwLastErr = 0;
  838. if (m_piClassObject && name)
  839. {
  840. VARIANT v;
  841. VariantInit(&v);
  842. V_R8(&v) = dub;
  843. V_VT(&v) = VT_R8;
  844. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  845. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  846. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  847. ASSERT_BREAK(SUCCEEDED(hr));
  848. bRet = (bool)SUCCEEDED(hr);
  849. VariantClear(&v);
  850. if (!bRet)
  851. {
  852. dwLastErr = (hr);
  853. LogError(IDS_FAILED, IDS_SETDOUBLE, name, hr);
  854. }
  855. }
  856. else
  857. {
  858. if (m_piClassObject)
  859. {
  860. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  861. LogError(IDS_InParam, IDS_SETDOUBLE);
  862. }
  863. else
  864. {
  865. dwLastErr = (WBEM_E_FAILED);
  866. LogError(IDS_NOCLASS, IDS_SETDOUBLE);
  867. }
  868. }
  869. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  870. SetLastError(dwLastErr);
  871. return bRet;
  872. }
  873. ////////////////////////////////////////////////////////////////////////
  874. //
  875. // Function: Get (DOUBLE)
  876. //
  877. //
  878. //
  879. // Inputs: Name of property to retrieve
  880. // DOUBLE buffer to receive value
  881. // Outputs:
  882. //
  883. // Return: false if you try to get a property that is not a DOUBLE compatible type
  884. //
  885. // Comments:
  886. //
  887. ////////////////////////////////////////////////////////////////////////
  888. bool CInstance::GetDOUBLE(LPCWSTR name, DOUBLE& dub) const
  889. {
  890. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  891. bool bRet = false;
  892. DWORD dwLastErr = 0;
  893. if (m_piClassObject && name)
  894. {
  895. // Variant_t handles the VariantInit/VariantClear
  896. variant_t v;
  897. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  898. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  899. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  900. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  901. // null properties from logging an error.
  902. BOOL bSuccess = SUCCEEDED(hr) && (v.vt == VT_R8 || v.vt == VT_NULL);
  903. ASSERT_BREAK(bSuccess);
  904. if (SUCCEEDED(hr))
  905. {
  906. if (v.vt == VT_R8)
  907. {
  908. dub = V_R8(&v);
  909. bRet = true;
  910. }
  911. else
  912. {
  913. dwLastErr = (WBEM_S_NO_MORE_DATA);
  914. }
  915. }
  916. if (!bSuccess)
  917. {
  918. if (SUCCEEDED(hr))
  919. {
  920. hr = WBEM_E_TYPE_MISMATCH;
  921. }
  922. dwLastErr = (hr);
  923. LogError(IDS_FAILED, IDS_GETDOUBLE, name, hr);
  924. }
  925. }
  926. else
  927. {
  928. if (m_piClassObject)
  929. {
  930. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  931. LogError(IDS_InParam, IDS_GETDOUBLE);
  932. }
  933. else
  934. {
  935. dwLastErr = (WBEM_E_FAILED);
  936. LogError(IDS_NOCLASS, IDS_GETDOUBLE);
  937. }
  938. }
  939. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  940. SetLastError(dwLastErr);
  941. return bRet;
  942. }
  943. ////////////////////////////////////////////////////////////////////////
  944. //
  945. // Function: Set (Byte)
  946. //
  947. //
  948. //
  949. // Inputs: Name of property to set
  950. // BYTE to be set
  951. // Outputs:
  952. //
  953. // Return: false if you try to set a property that is not a BYTE compatible type
  954. //
  955. // Comments:
  956. //
  957. ////////////////////////////////////////////////////////////////////////
  958. bool CInstance::SetByte(LPCWSTR name, BYTE b)
  959. {
  960. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  961. bool bRet = false;
  962. DWORD dwLastErr = 0;
  963. if (m_piClassObject && name)
  964. {
  965. VARIANT v;
  966. VariantInit(&v);
  967. v.vt = VT_UI1;
  968. v.bVal = (long)b ;
  969. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  970. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  971. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  972. ASSERT_BREAK(SUCCEEDED(hr));
  973. bRet = (bool)SUCCEEDED(hr);
  974. if (!bRet)
  975. {
  976. dwLastErr = (hr);
  977. LogError(IDS_FAILED, IDS_SETBYTE, name, hr);
  978. }
  979. VariantClear(&v);
  980. }
  981. else
  982. {
  983. if (m_piClassObject)
  984. {
  985. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  986. LogError(IDS_InParam, IDS_SETBYTE);
  987. }
  988. else
  989. {
  990. dwLastErr = (WBEM_E_FAILED);
  991. LogError(IDS_NOCLASS, IDS_SETBYTE);
  992. }
  993. }
  994. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  995. SetLastError(dwLastErr);
  996. return bRet;
  997. }
  998. ////////////////////////////////////////////////////////////////////////
  999. //
  1000. // Function: SetEmbeddedObject
  1001. //
  1002. //
  1003. //
  1004. // Inputs: Name of property to set
  1005. // CInstance to be set
  1006. // Outputs:
  1007. //
  1008. // Return: false if you try to set a property that is not a IUnknown compatible type
  1009. //
  1010. // Comments: CInstance is not released - responsibility of caller
  1011. //
  1012. ////////////////////////////////////////////////////////////////////////
  1013. bool CInstance::SetEmbeddedObject(LPCWSTR name, CInstance& cInstance )
  1014. {
  1015. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1016. bool bRet = false;
  1017. DWORD dwLastErr = 0;
  1018. if (m_piClassObject && name)
  1019. {
  1020. IWbemClassObject *t_ClassObject = cInstance.GetClassObjectInterface();
  1021. if ( t_ClassObject )
  1022. {
  1023. variant_t v;
  1024. v.vt = VT_UNKNOWN;
  1025. v.punkVal = t_ClassObject;
  1026. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1027. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  1028. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1029. ASSERT_BREAK(SUCCEEDED(hr));
  1030. bRet = (bool)SUCCEEDED(hr);
  1031. if (!bRet)
  1032. {
  1033. dwLastErr = (hr);
  1034. LogError(IDS_FAILED, IDS_SetEmbeddedObject, name, hr);
  1035. }
  1036. }
  1037. else
  1038. {
  1039. dwLastErr = (WBEM_E_FAILED);
  1040. }
  1041. }
  1042. else
  1043. {
  1044. if (m_piClassObject)
  1045. {
  1046. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1047. LogError(IDS_InParam, IDS_SetEmbeddedObject);
  1048. }
  1049. else
  1050. {
  1051. dwLastErr = (WBEM_E_FAILED);
  1052. LogError(IDS_NOCLASS, IDS_SetEmbeddedObject);
  1053. }
  1054. }
  1055. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1056. SetLastError(dwLastErr);
  1057. return bRet;
  1058. }
  1059. ////////////////////////////////////////////////////////////////////////
  1060. //
  1061. // Function: Get (Byte)
  1062. //
  1063. //
  1064. //
  1065. // Inputs: Name of property to retrieve
  1066. // BYTE buffer to receive value
  1067. // Outputs:
  1068. //
  1069. // Return: false if you try to get a property that is not a DWORD compatible type
  1070. //
  1071. // Comments:
  1072. //
  1073. ////////////////////////////////////////////////////////////////////////
  1074. bool CInstance::GetByte(LPCWSTR name, BYTE& b) const
  1075. {
  1076. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1077. bool bRet = false;
  1078. DWORD dwLastErr = 0;
  1079. if (m_piClassObject && name)
  1080. {
  1081. // Variant_t handles the VariantInit/VariantClear
  1082. variant_t v;
  1083. CIMTYPE vtType;
  1084. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1085. HRESULT hr = m_piClassObject->Get(name, 0, &v, &vtType, NULL);
  1086. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1087. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  1088. // null properties from logging an error.
  1089. BOOL bSuccess = (SUCCEEDED(hr)) && ((vtType == CIM_SINT8) || (vtType == CIM_UINT8));
  1090. ASSERT_BREAK(bSuccess);
  1091. if (SUCCEEDED(hr))
  1092. {
  1093. if( (v.vt == VT_UI1) && ( (vtType == CIM_SINT8) || (vtType == CIM_UINT8) ) )
  1094. {
  1095. b = v.bVal;
  1096. bRet = true;
  1097. }
  1098. else
  1099. {
  1100. dwLastErr = (WBEM_S_NO_MORE_DATA);
  1101. }
  1102. }
  1103. if (!bSuccess)
  1104. {
  1105. if (SUCCEEDED(hr))
  1106. {
  1107. hr = WBEM_E_TYPE_MISMATCH;
  1108. }
  1109. dwLastErr = (hr);
  1110. LogError(IDS_FAILED, IDS_GETBYTE, name, hr);
  1111. }
  1112. }
  1113. else
  1114. {
  1115. if (m_piClassObject)
  1116. {
  1117. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1118. LogError(IDS_InParam, IDS_GETBYTE);
  1119. }
  1120. else
  1121. {
  1122. dwLastErr = (WBEM_E_FAILED);
  1123. LogError(IDS_NOCLASS, IDS_GETBYTE);
  1124. }
  1125. }
  1126. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1127. SetLastError(dwLastErr);
  1128. return bRet;
  1129. }
  1130. ////////////////////////////////////////////////////////////////////////
  1131. //
  1132. // Function: GetEmbeddedObject
  1133. //
  1134. //
  1135. //
  1136. // Inputs: Name of property to retrieve
  1137. // reference to buffer hold pointer to new instance
  1138. // Outputs:
  1139. //
  1140. // Return: false if you try to get a property that is not a object compatible type
  1141. //
  1142. // Comments: Creates CInstance, user is responsible for release
  1143. //
  1144. ////////////////////////////////////////////////////////////////////////
  1145. bool CInstance::GetEmbeddedObject (LPCWSTR name, CInstance** pInstance, MethodContext* pMethodContext) const
  1146. {
  1147. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1148. bool bRet = false;
  1149. DWORD dwLastErr = 0;
  1150. ASSERT_BREAK(m_piClassObject && (pInstance != NULL));
  1151. if (m_piClassObject && name && (pInstance != NULL))
  1152. {
  1153. // Variant_t handles the VariantInit/VariantClear
  1154. variant_t v;
  1155. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1156. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  1157. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1158. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  1159. // null properties from logging an error.
  1160. BOOL bSuccess = SUCCEEDED(hr) && (v.vt == VT_UNKNOWN || v.vt == VT_NULL);
  1161. ASSERT_BREAK(bSuccess);
  1162. if (SUCCEEDED(hr))
  1163. {
  1164. if (v.vt == VT_UNKNOWN)
  1165. {
  1166. IUnknown *t_Unknown = v.punkVal ;
  1167. if ( t_Unknown )
  1168. {
  1169. IWbemClassObject *t_Object = NULL;
  1170. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1171. HRESULT t_Result = t_Unknown->QueryInterface ( IID_IWbemClassObject , (void**) &t_Object ) ;
  1172. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1173. if ( SUCCEEDED ( t_Result ) )
  1174. {
  1175. *pInstance = new CInstance(t_Object, pMethodContext);
  1176. if (pInstance == NULL)
  1177. {
  1178. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  1179. }
  1180. bRet = true ;
  1181. }
  1182. else
  1183. {
  1184. dwLastErr = (t_Result);
  1185. }
  1186. }
  1187. else
  1188. {
  1189. dwLastErr = (WBEM_S_NO_MORE_DATA);
  1190. }
  1191. }
  1192. else
  1193. {
  1194. if (bSuccess)
  1195. {
  1196. dwLastErr = (WBEM_S_NO_MORE_DATA);
  1197. }
  1198. }
  1199. }
  1200. if (!bSuccess)
  1201. {
  1202. if (SUCCEEDED(hr))
  1203. {
  1204. hr = WBEM_E_TYPE_MISMATCH;
  1205. }
  1206. dwLastErr = (hr);
  1207. LogError(IDS_FAILED, IDS_GetEmbeddedObject, name, hr);
  1208. }
  1209. }
  1210. else
  1211. {
  1212. if (m_piClassObject)
  1213. {
  1214. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1215. LogError(IDS_InParam, IDS_GetEmbeddedObject);
  1216. }
  1217. else
  1218. {
  1219. dwLastErr = (WBEM_E_FAILED);
  1220. LogError(IDS_NOCLASS, IDS_GetEmbeddedObject);
  1221. }
  1222. }
  1223. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1224. SetLastError(dwLastErr);
  1225. return bRet;
  1226. }
  1227. // bool support //
  1228. ////////////////////////////////////////////////////////////////////////
  1229. //
  1230. // Function: Set (bool)
  1231. //
  1232. //
  1233. //
  1234. // Inputs: Name of property to set
  1235. // bool to be set
  1236. // Outputs:
  1237. //
  1238. // Return: false if you try to set a property that is not a bool compatible type
  1239. //
  1240. // Comments:
  1241. //
  1242. ////////////////////////////////////////////////////////////////////////
  1243. bool CInstance::Setbool(LPCWSTR name, bool b)
  1244. {
  1245. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1246. bool bRet = false;
  1247. DWORD dwLastErr = 0;
  1248. if (m_piClassObject && name)
  1249. {
  1250. VARIANT v;
  1251. VariantInit(&v);
  1252. v.vt = VT_BOOL;
  1253. if (b)
  1254. {
  1255. v.boolVal = VARIANT_TRUE;
  1256. }
  1257. else
  1258. {
  1259. v.boolVal = VARIANT_FALSE;
  1260. }
  1261. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1262. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  1263. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1264. ASSERT_BREAK(SUCCEEDED(hr));
  1265. bRet = (bool)SUCCEEDED(hr);
  1266. if (!bRet)
  1267. {
  1268. dwLastErr = (hr);
  1269. LogError(IDS_FAILED, IDS_SETBOOL, name, hr);
  1270. }
  1271. VariantClear(&v);
  1272. }
  1273. else
  1274. {
  1275. if (m_piClassObject)
  1276. {
  1277. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1278. LogError(IDS_InParam, IDS_SETBOOL);
  1279. }
  1280. else
  1281. {
  1282. dwLastErr = (WBEM_E_FAILED);
  1283. LogError(IDS_NOCLASS, IDS_SETBOOL);
  1284. }
  1285. }
  1286. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1287. SetLastError(dwLastErr);
  1288. return bRet;
  1289. }
  1290. ////////////////////////////////////////////////////////////////////////
  1291. //
  1292. // Function: Get (bool)
  1293. //
  1294. //
  1295. //
  1296. // Inputs: Name of property to retrieve
  1297. // bool buffer to receive value
  1298. // Outputs:
  1299. //
  1300. // Return: false if you try to get a property that is not a bool compatible type
  1301. //
  1302. // Comments:
  1303. //
  1304. ////////////////////////////////////////////////////////////////////////
  1305. bool CInstance::Getbool(LPCWSTR name, bool& b) const
  1306. {
  1307. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1308. bool bRet = false;
  1309. DWORD dwLastErr = 0;
  1310. if (m_piClassObject && name)
  1311. {
  1312. // Variant_t handles the VariantInit/VariantClear
  1313. variant_t v;
  1314. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1315. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  1316. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1317. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  1318. // null properties from logging an error.
  1319. BOOL bSuccess = (SUCCEEDED(hr)) && (v.vt == VT_BOOL || v.vt == VT_NULL);
  1320. ASSERT_BREAK(bSuccess);
  1321. if (SUCCEEDED(hr))
  1322. {
  1323. if (v.vt == VT_BOOL)
  1324. {
  1325. if (v.boolVal)
  1326. {
  1327. b = true;
  1328. }
  1329. else
  1330. {
  1331. b = false;
  1332. }
  1333. bRet = true;
  1334. ASSERT_BREAK((v.boolVal == VARIANT_TRUE) || (v.boolVal == VARIANT_FALSE));
  1335. }
  1336. else
  1337. {
  1338. dwLastErr = (WBEM_S_NO_MORE_DATA);
  1339. }
  1340. }
  1341. if (!bSuccess)
  1342. {
  1343. if (SUCCEEDED(hr))
  1344. {
  1345. hr = WBEM_E_TYPE_MISMATCH;
  1346. }
  1347. dwLastErr = (hr);
  1348. LogError(IDS_FAILED, IDS_GETBOOL, name, hr);
  1349. }
  1350. }
  1351. else
  1352. {
  1353. if (m_piClassObject)
  1354. {
  1355. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1356. LogError(IDS_InParam, IDS_GETBOOL);
  1357. }
  1358. else
  1359. {
  1360. dwLastErr = (WBEM_E_FAILED);
  1361. LogError(IDS_NOCLASS, IDS_GETBOOL);
  1362. }
  1363. }
  1364. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1365. SetLastError(dwLastErr);
  1366. return bRet;
  1367. }
  1368. ////////////////////////////////////////////////////////////////////////
  1369. //
  1370. // Function: SetVariant
  1371. //
  1372. //
  1373. //
  1374. // Inputs: const LPCWSTR name - Name of property to set
  1375. // const VARIANT& variant - Value to assign to Name.
  1376. //
  1377. // Outputs:
  1378. //
  1379. // Return: false if the supplied variant type is not correct
  1380. // for the property we are setting.
  1381. //
  1382. // Comments:
  1383. //
  1384. ////////////////////////////////////////////////////////////////////////
  1385. bool CInstance::SetVariant( LPCWSTR name, const VARIANT& variant )
  1386. {
  1387. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1388. bool bRet = false;
  1389. DWORD dwLastErr = 0;
  1390. if (m_piClassObject && name)
  1391. {
  1392. HRESULT hr;
  1393. // I realize the (VARIANT*) cast is ugly, as it is a const,
  1394. // HOWEVER, somewhere nobody seems to understand why we would
  1395. // possibly want to keep things const. I could copy the VARIANT,
  1396. // but that requires the same cast, so under duress, and to reduce
  1397. // redundant code, I'm casting here. Did I mention EXTREME
  1398. // DURESS?
  1399. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1400. hr = m_piClassObject->Put(name, 0, (VARIANT*) &variant, NULL );
  1401. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1402. ASSERT_BREAK(SUCCEEDED(hr));
  1403. bRet = (bool)SUCCEEDED(hr);
  1404. if (!bRet)
  1405. {
  1406. dwLastErr = (hr);
  1407. LogError(IDS_FAILED, IDS_SETVARIANT, name, hr);
  1408. }
  1409. }
  1410. else
  1411. {
  1412. if (m_piClassObject)
  1413. {
  1414. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1415. LogError(IDS_InParam, IDS_SETVARIANT);
  1416. }
  1417. else
  1418. {
  1419. dwLastErr = (WBEM_E_FAILED);
  1420. LogError(IDS_NOCLASS, IDS_SETVARIANT);
  1421. }
  1422. }
  1423. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1424. SetLastError(dwLastErr);
  1425. return bRet;
  1426. }
  1427. ////////////////////////////////////////////////////////////////////////
  1428. //
  1429. // Function: GetVariant
  1430. //
  1431. //
  1432. //
  1433. // Inputs: const LPCWSTR name - Name of property to set
  1434. // VARIANT& variant - Value to assign to Name.
  1435. //
  1436. // Outputs:
  1437. //
  1438. // Return: false if the supplied variant type is not correct
  1439. // for the property we are setting.
  1440. //
  1441. // Comments:
  1442. //
  1443. ////////////////////////////////////////////////////////////////////////
  1444. bool CInstance::GetVariant( LPCWSTR name, VARIANT& variant ) const
  1445. {
  1446. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1447. bool bRet = false;
  1448. DWORD dwLastErr = 0;
  1449. if (m_piClassObject && name)
  1450. {
  1451. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1452. HRESULT hr = m_piClassObject->Get(name, 0, &variant, NULL, NULL );
  1453. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1454. ASSERT_BREAK(SUCCEEDED(hr));
  1455. bRet = (bool)SUCCEEDED(hr);
  1456. if (!bRet)
  1457. {
  1458. dwLastErr = (hr);
  1459. LogError(IDS_FAILED, IDS_GETVARIANT, name, hr);
  1460. }
  1461. }
  1462. else
  1463. {
  1464. if (m_piClassObject)
  1465. {
  1466. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1467. LogError(IDS_InParam, IDS_GETVARIANT);
  1468. }
  1469. else
  1470. {
  1471. dwLastErr = (WBEM_E_FAILED);
  1472. LogError(IDS_NOCLASS, IDS_GETVARIANT);
  1473. }
  1474. }
  1475. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1476. SetLastError(dwLastErr);
  1477. return bRet;
  1478. }
  1479. ////////////////////////////////////////////////////////////////////////
  1480. //
  1481. // Function: SetDateTime
  1482. //
  1483. //
  1484. //
  1485. // Inputs: const LPCWSTR name - Name of property to set
  1486. // const WBEMTime& wbemtime - Value to assign to Name.
  1487. //
  1488. // Outputs:
  1489. //
  1490. // Return: false if the supplied time type is not correct
  1491. // for the property we are setting.
  1492. //
  1493. // Comments:
  1494. //
  1495. ////////////////////////////////////////////////////////////////////////
  1496. bool CInstance::SetDateTime( LPCWSTR name, const WBEMTime& wbemtime )
  1497. {
  1498. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1499. bool bRet = false;
  1500. DWORD dwLastErr = 0;
  1501. if (m_piClassObject && name && (wbemtime.IsOk()))
  1502. {
  1503. //GetDMTF may throw so get htis before modifying variant_t
  1504. BSTR bstrTmp = wbemtime.GetDMTF(true);
  1505. // Variant_t handles the VariantInit/VariantClear
  1506. variant_t v;
  1507. // Time is stored as a BSTR
  1508. v.vt = VT_BSTR;
  1509. v.bstrVal = bstrTmp;
  1510. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1511. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  1512. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1513. ASSERT_BREAK(SUCCEEDED(hr));
  1514. bRet = (bool)SUCCEEDED(hr);
  1515. if (!bRet)
  1516. {
  1517. dwLastErr = (hr);
  1518. LogError(IDS_FAILED, IDS_SETDATETIME, name, hr);
  1519. }
  1520. }
  1521. else
  1522. {
  1523. if (m_piClassObject)
  1524. {
  1525. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1526. LogError(IDS_InParam, IDS_SETDATETIME);
  1527. }
  1528. else
  1529. {
  1530. dwLastErr = (WBEM_E_FAILED);
  1531. LogError(IDS_NOCLASS, IDS_SETDATETIME);
  1532. }
  1533. }
  1534. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1535. SetLastError(dwLastErr);
  1536. return bRet;
  1537. }
  1538. ////////////////////////////////////////////////////////////////////////
  1539. //
  1540. // Function: GetDateTime
  1541. //
  1542. //
  1543. //
  1544. // Inputs: const LPCWSTR name - Name of property to set
  1545. // WBEMTime& wbemtime - Value to obtain from Name.
  1546. //
  1547. // Outputs:
  1548. //
  1549. // Return: false if the supplied variant type is not correct
  1550. // for the property we are setting.
  1551. //
  1552. // Comments:
  1553. //
  1554. ////////////////////////////////////////////////////////////////////////
  1555. bool CInstance::GetDateTime( LPCWSTR name, WBEMTime& wbemtime ) const
  1556. {
  1557. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1558. bool bRet = false;
  1559. DWORD dwLastErr = 0;
  1560. if (m_piClassObject && name)
  1561. {
  1562. // Variant_t handles the VariantInit/VariantClear
  1563. variant_t v;
  1564. //
  1565. // Get the name as a BSTR and pass it into the
  1566. // wbemtime, which handles the conversion internally
  1567. // like a good little class.
  1568. //
  1569. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1570. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  1571. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1572. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  1573. // null properties from logging an error.
  1574. BOOL bSuccess = (SUCCEEDED(hr)) && (v.vt == VT_BSTR || v.vt == VT_NULL);
  1575. ASSERT_BREAK(bSuccess);
  1576. if (SUCCEEDED(hr))
  1577. {
  1578. if ((v.vt == VT_BSTR) && (v.bstrVal != NULL))
  1579. {
  1580. wbemtime = v.bstrVal;
  1581. bRet = wbemtime.IsOk();
  1582. if (!bRet)
  1583. {
  1584. dwLastErr = (WBEM_E_TYPE_MISMATCH);
  1585. }
  1586. }
  1587. else
  1588. {
  1589. dwLastErr = (WBEM_S_NO_MORE_DATA);
  1590. }
  1591. }
  1592. if (!bSuccess)
  1593. {
  1594. if (SUCCEEDED(hr))
  1595. {
  1596. hr = WBEM_E_TYPE_MISMATCH;
  1597. }
  1598. dwLastErr = (hr);
  1599. LogError(IDS_FAILED, IDS_GETDATETIME, name, hr);
  1600. }
  1601. }
  1602. else
  1603. {
  1604. if (m_piClassObject)
  1605. {
  1606. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1607. LogError(IDS_InParam, IDS_GETDATETIME);
  1608. }
  1609. else
  1610. {
  1611. dwLastErr = (WBEM_E_FAILED);
  1612. LogError(IDS_NOCLASS, IDS_GETDATETIME);
  1613. }
  1614. }
  1615. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1616. SetLastError(dwLastErr);
  1617. return bRet;
  1618. }
  1619. ////////////////////////////////////////////////////////////////////////
  1620. //
  1621. // Function: SetTimeSpan
  1622. //
  1623. //
  1624. //
  1625. // Inputs: const LPCWSTR name - Name of property to set
  1626. // const WBEMTimeSpan& wbemtimespan - Value to assign to Name.
  1627. //
  1628. // Outputs:
  1629. //
  1630. // Return: false if the supplied timespan type is not correct
  1631. // for the property we are setting.
  1632. //
  1633. // Comments:
  1634. //
  1635. ////////////////////////////////////////////////////////////////////////
  1636. bool CInstance::SetTimeSpan( LPCWSTR name, const WBEMTimeSpan& wbemtimespan )
  1637. {
  1638. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1639. bool bRet = false;
  1640. DWORD dwLastErr = 0;
  1641. if (m_piClassObject && name && (wbemtimespan.IsOk()))
  1642. {
  1643. //GetBSTR may throw so get this before modifying variant_t
  1644. BSTR bstrTmp = wbemtimespan.GetBSTR();
  1645. // Variant_t handles the VariantInit/VariantClear
  1646. variant_t v;
  1647. // Time is stored as a BSTR
  1648. v.vt = VT_BSTR;
  1649. v.bstrVal = bstrTmp;
  1650. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1651. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  1652. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1653. ASSERT_BREAK(SUCCEEDED(hr));
  1654. bRet = (bool)SUCCEEDED(hr);
  1655. if (!bRet)
  1656. {
  1657. dwLastErr = (hr);
  1658. LogError(IDS_FAILED, IDS_SETTIMESPAN, name, hr);
  1659. }
  1660. }
  1661. else
  1662. {
  1663. if (m_piClassObject)
  1664. {
  1665. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1666. LogError(IDS_InParam, IDS_SETTIMESPAN);
  1667. }
  1668. else
  1669. {
  1670. dwLastErr = (WBEM_E_FAILED);
  1671. LogError(IDS_NOCLASS, IDS_SETTIMESPAN);
  1672. }
  1673. }
  1674. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1675. SetLastError(dwLastErr);
  1676. return bRet;
  1677. }
  1678. ////////////////////////////////////////////////////////////////////////
  1679. //
  1680. // Function: GetTimeSpan
  1681. //
  1682. //
  1683. //
  1684. // Inputs: const LPCWSTR name - Name of property to set
  1685. // WBEMTimeSpan& wbemtimespan - Value to obtain from Name.
  1686. //
  1687. // Outputs:
  1688. //
  1689. // Return: false if the supplied timespan type is not correct
  1690. // for the property we are setting.
  1691. //
  1692. // Comments:
  1693. //
  1694. ////////////////////////////////////////////////////////////////////////
  1695. bool CInstance::GetTimeSpan( LPCWSTR name, WBEMTimeSpan& wbemtimespan ) const
  1696. {
  1697. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1698. bool bRet = false;
  1699. DWORD dwLastErr = 0;
  1700. if (m_piClassObject && name)
  1701. {
  1702. // Variant_t handles the VariantInit/VariantClear
  1703. variant_t v;
  1704. //
  1705. // Get the name as a BSTR and pass it into the
  1706. // wbemtimespan, which handles the conversion
  1707. // internally like a good little class.
  1708. //
  1709. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1710. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  1711. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1712. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  1713. // null properties from logging an error.
  1714. BOOL bSuccess = (SUCCEEDED(hr)) && (v.vt == VT_BSTR || v.vt == VT_NULL);
  1715. ASSERT_BREAK(bSuccess);
  1716. if (SUCCEEDED(hr))
  1717. {
  1718. if ((v.vt == VT_BSTR) && (v.bstrVal != NULL))
  1719. {
  1720. wbemtimespan = v.bstrVal;
  1721. bRet = wbemtimespan.IsOk();
  1722. // This is freed by the VariantClear
  1723. // SysFreeString(v.bstrVal);
  1724. if (!bRet)
  1725. {
  1726. dwLastErr = (WBEM_E_TYPE_MISMATCH);
  1727. }
  1728. }
  1729. else
  1730. {
  1731. dwLastErr = (WBEM_S_NO_MORE_DATA);
  1732. }
  1733. }
  1734. if (!bSuccess)
  1735. {
  1736. if (SUCCEEDED(hr))
  1737. {
  1738. hr = WBEM_E_TYPE_MISMATCH;
  1739. }
  1740. dwLastErr = (hr);
  1741. LogError(IDS_FAILED, IDS_GETTIMESPAN, name, hr);
  1742. }
  1743. }
  1744. else
  1745. {
  1746. if (m_piClassObject)
  1747. {
  1748. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1749. LogError(IDS_InParam, IDS_GETTIMESPAN);
  1750. }
  1751. else
  1752. {
  1753. dwLastErr = (WBEM_E_FAILED);
  1754. LogError(IDS_NOCLASS, IDS_GETTIMESPAN);
  1755. }
  1756. }
  1757. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1758. SetLastError(dwLastErr);
  1759. return bRet;
  1760. }
  1761. ////////////////////////////////////////////////////////////////////////
  1762. //
  1763. // Function: SetWBEMINT64
  1764. //
  1765. //
  1766. //
  1767. // Inputs: const LPCWSTR name - Name of property to set
  1768. // const WBEMINT64& wbemint64 - Value to assign to Name.
  1769. //
  1770. // Outputs:
  1771. //
  1772. // Return: false if the supplied wbemint64 type is not correct
  1773. // for the property we are setting.
  1774. //
  1775. // Comments:
  1776. //
  1777. ////////////////////////////////////////////////////////////////////////
  1778. bool CInstance::SetWBEMINT64( LPCWSTR name, const WBEMINT64& wbemint64 )
  1779. {
  1780. // For right now, this is just a CHString.
  1781. return SetWCHARSplat( name, wbemint64 );
  1782. }
  1783. bool CInstance::SetWBEMINT64( LPCWSTR name, const LONGLONG i64Value )
  1784. {
  1785. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1786. WCHAR szBuff[33];
  1787. _i64tow(i64Value, szBuff, 10);
  1788. return SetWCHARSplat(name, szBuff);
  1789. }
  1790. bool CInstance::SetWBEMINT64( LPCWSTR name, const ULONGLONG i64Value )
  1791. {
  1792. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1793. WCHAR szBuff[33];
  1794. _ui64tow(i64Value, szBuff, 10);
  1795. return SetWCHARSplat(name, szBuff);
  1796. }
  1797. ////////////////////////////////////////////////////////////////////////
  1798. //
  1799. // Function: GetWBEMINT64
  1800. //
  1801. //
  1802. //
  1803. // Inputs: const LPCWSTR name - Name of property to set
  1804. // WBEMINT64& wbemint64 - Value to assign to Name.
  1805. //
  1806. // Outputs:
  1807. //
  1808. // Return: false if the supplied wbemint64 type is not correct
  1809. // for the property we are setting.
  1810. //
  1811. // Comments:
  1812. //
  1813. ////////////////////////////////////////////////////////////////////////
  1814. bool CInstance::GetWBEMINT64( LPCWSTR name, WBEMINT64& wbemint64 ) const
  1815. {
  1816. // For right now, this is just a CHString.
  1817. return GetCHString( name, wbemint64 );
  1818. }
  1819. bool CInstance::GetWBEMINT64( LPCWSTR name, LONGLONG& i64Value) const
  1820. {
  1821. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1822. CHString s;
  1823. bool b = GetWBEMINT64(name, s);
  1824. if (b)
  1825. i64Value = _wtoi64(s);
  1826. return b;
  1827. }
  1828. bool CInstance::GetWBEMINT64( LPCWSTR name, ULONGLONG& i64Value) const
  1829. {
  1830. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1831. CHString s;
  1832. bool b = GetWBEMINT64(name, s);
  1833. if (b)
  1834. i64Value = _wtoi64(s);
  1835. return b;
  1836. }
  1837. ////////////////////////////////////////////////////////////////////////
  1838. //
  1839. // Function: SetWBEMINT16
  1840. //
  1841. //
  1842. //
  1843. // Inputs: const LPCWSTR name - Name of property to set
  1844. // const WBEMINT16& wbemint16 - Value to assign to Name.
  1845. //
  1846. // Outputs:
  1847. //
  1848. // Return: false if the supplied wbemint16 type is not correct
  1849. // for the property we are setting.
  1850. //
  1851. // Comments:
  1852. //
  1853. ////////////////////////////////////////////////////////////////////////
  1854. bool CInstance::SetWBEMINT16( LPCWSTR name, const WBEMINT16& wbemint16 )
  1855. {
  1856. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1857. bool bRet = false;
  1858. DWORD dwLastErr = 0;
  1859. if (m_piClassObject && name)
  1860. {
  1861. VARIANT v;
  1862. VariantInit(&v);
  1863. v.vt = VT_I2;
  1864. v.iVal = wbemint16;
  1865. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1866. HRESULT hr = m_piClassObject->Put(name, 0, &v, NULL);
  1867. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1868. ASSERT_BREAK(SUCCEEDED(hr));
  1869. bRet = (bool)SUCCEEDED(hr);
  1870. if (!bRet)
  1871. {
  1872. dwLastErr = (hr);
  1873. LogError(IDS_FAILED, IDS_SETWBEMINT16, name, hr);
  1874. }
  1875. VariantClear(&v);
  1876. }
  1877. else
  1878. {
  1879. if (m_piClassObject)
  1880. {
  1881. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1882. LogError(IDS_InParam, IDS_SETWBEMINT16);
  1883. }
  1884. else
  1885. {
  1886. dwLastErr = (WBEM_E_FAILED);
  1887. LogError(IDS_NOCLASS, IDS_SETWBEMINT16);
  1888. }
  1889. }
  1890. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1891. SetLastError(dwLastErr);
  1892. return bRet;
  1893. }
  1894. ////////////////////////////////////////////////////////////////////////
  1895. //
  1896. // Function: GetWBEMINT16
  1897. //
  1898. //
  1899. //
  1900. // Inputs: const LPCWSTR name - Name of property to set
  1901. // WBEMINT16& wbemint16 - Value to assign to Name.
  1902. //
  1903. // Outputs:
  1904. //
  1905. // Return: false if the supplied wbemint16 type is not correct
  1906. // for the property we are setting.
  1907. //
  1908. // Comments:
  1909. //
  1910. ////////////////////////////////////////////////////////////////////////
  1911. bool CInstance::GetWBEMINT16( LPCWSTR name, WBEMINT16& wbemint16 ) const
  1912. {
  1913. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1914. bool bRet = false;
  1915. DWORD dwLastErr = 0;
  1916. if (m_piClassObject && name)
  1917. {
  1918. // Variant_t handles the VariantInit/VariantClear
  1919. variant_t v;
  1920. CIMTYPE vtType;
  1921. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1922. HRESULT hr = m_piClassObject->Get(name, 0, &v, &vtType, NULL);
  1923. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1924. // If bSuccess is TRUE, we don't need to log an error. This keeps Gets on
  1925. // null properties from logging an error.
  1926. BOOL bSuccess = (SUCCEEDED(hr)) && (CIM_SINT16 == vtType) && ((v.vt == VT_I2) || (v.vt == VT_NULL));
  1927. ASSERT_BREAK(bSuccess);
  1928. if (SUCCEEDED(hr))
  1929. {
  1930. if ((vtType == CIM_SINT16) && (v.vt == VT_I2))
  1931. {
  1932. wbemint16 = v.iVal;
  1933. bRet = true;
  1934. }
  1935. else
  1936. {
  1937. dwLastErr = (WBEM_S_NO_MORE_DATA);
  1938. }
  1939. }
  1940. if (!bSuccess)
  1941. {
  1942. if (SUCCEEDED(hr))
  1943. {
  1944. hr = WBEM_E_TYPE_MISMATCH;
  1945. }
  1946. dwLastErr = (hr);
  1947. LogError(IDS_FAILED, IDS_GETWBEMINT16, name, hr);
  1948. }
  1949. }
  1950. else
  1951. {
  1952. if (m_piClassObject)
  1953. {
  1954. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  1955. LogError(IDS_InParam, IDS_GETWBEMINT16);
  1956. }
  1957. else
  1958. {
  1959. dwLastErr = (WBEM_E_FAILED);
  1960. LogError(IDS_NOCLASS, IDS_GETWBEMINT16);
  1961. }
  1962. }
  1963. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  1964. SetLastError(dwLastErr);
  1965. return bRet;
  1966. }
  1967. ////////////////////////////////////////////////////////////////////////
  1968. //
  1969. // Function: IsNull (LPCWSTR)
  1970. //
  1971. // Inputs: Name of property to check
  1972. //
  1973. // Outputs:
  1974. //
  1975. // Return: true if VT_NULL or (VT_BSTR and *bstr == NULL)
  1976. //
  1977. // Comments:
  1978. //
  1979. ////////////////////////////////////////////////////////////////////////
  1980. bool CInstance::IsNull(LPCWSTR name) const
  1981. {
  1982. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1983. bool bRet = true;
  1984. if (m_piClassObject && name)
  1985. {
  1986. // Variant_t handles the VariantInit/VariantClear
  1987. variant_t v;
  1988. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  1989. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  1990. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  1991. if (SUCCEEDED(hr))
  1992. {
  1993. if (( v.vt != VT_NULL ) &&
  1994. ( (v.vt != VT_BSTR) || (v.bstrVal != NULL) ))
  1995. {
  1996. bRet = false;
  1997. }
  1998. else
  1999. {
  2000. bRet = true;
  2001. }
  2002. }
  2003. else
  2004. {
  2005. ASSERT_BREAK(0);
  2006. LogError(IDS_FAILED, IDS_CINSTANCEISNULL, name, hr);
  2007. }
  2008. }
  2009. else
  2010. {
  2011. ASSERT_BREAK(0);
  2012. if (m_piClassObject)
  2013. {
  2014. LogError(IDS_InParam, IDS_CINSTANCEISNULL);
  2015. }
  2016. else
  2017. {
  2018. LogError(IDS_NOCLASS, IDS_CINSTANCEISNULL);
  2019. }
  2020. }
  2021. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  2022. return bRet;
  2023. }
  2024. ////////////////////////////////////////////////////////////////////////
  2025. //
  2026. // Function: GetStatus (LPCWSTR, bool&,VARTYPE &)
  2027. //
  2028. //
  2029. //
  2030. // Inputs: Name of property to check
  2031. //
  2032. // Outputs:
  2033. //
  2034. // Return: true if succeeded, false otherwise
  2035. //
  2036. // Comments:
  2037. //
  2038. ////////////////////////////////////////////////////////////////////////
  2039. bool CInstance::GetStatus (LPCWSTR name, bool &a_Exists , VARTYPE &a_VarType ) const
  2040. {
  2041. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  2042. bool t_Status = true ;
  2043. DWORD dwLastErr = 0;
  2044. if (m_piClassObject && name)
  2045. {
  2046. // Variant_t handles the VariantInit/VariantClear
  2047. variant_t v;
  2048. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::WinMgmtTimer);
  2049. HRESULT hr = m_piClassObject->Get(name, 0, &v, NULL, NULL);
  2050. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::FrameworkTimer);
  2051. if (SUCCEEDED(hr))
  2052. {
  2053. a_Exists = true ;
  2054. a_VarType = v.vt ;
  2055. }
  2056. else
  2057. {
  2058. a_Exists = false ;
  2059. }
  2060. }
  2061. else
  2062. {
  2063. if (m_piClassObject)
  2064. {
  2065. dwLastErr = (WBEM_E_INVALID_PARAMETER);
  2066. }
  2067. else
  2068. {
  2069. dwLastErr = (WBEM_E_FAILED);
  2070. }
  2071. t_Status = false ;
  2072. }
  2073. PROVIDER_INSTRUMENTATION_START(m_pMethodContext, StopWatch::ProviderTimer);
  2074. SetLastError(dwLastErr);
  2075. return t_Status ;
  2076. }
  2077. ////////////////////////////////////////////////////////////////////////
  2078. MethodContext* CInstance::GetMethodContext() const
  2079. {
  2080. return m_pMethodContext;
  2081. }
  2082. ////////////////////////////////////////////////////////////////////////
  2083. void CInstance::LogError(LPCWSTR errorStr, LPCWSTR pFunctionName, LPCWSTR pArgs /*= NULL*/, HRESULT hError /*= -1*/) const
  2084. {
  2085. if (IsErrorLoggingEnabled())
  2086. {
  2087. CHString className(IDS_UNKNOWNCLASS);
  2088. // GetCHString("__NAME", className);
  2089. // okay, I'm NOT going through GetCHString to get this
  2090. // why? what happens if it fails? it tries to call this function...
  2091. // can you say "stack overflow?"
  2092. if (m_piClassObject)
  2093. {
  2094. // Variant_t handles the VariantInit/VariantClear
  2095. variant_t v;
  2096. HRESULT hr = m_piClassObject->Get(IDS_CLASS, 0, &v, NULL, NULL);
  2097. ASSERT_BREAK((SUCCEEDED(hr)) && ((v.vt == VT_NULL) || (v.vt == VT_BSTR)));
  2098. if (SUCCEEDED(hr))
  2099. {
  2100. if ( v.bstrVal != NULL
  2101. && v.vt != VT_NULL )
  2102. {
  2103. className = v.bstrVal;
  2104. }
  2105. }
  2106. }
  2107. // intent is that the error string look like:
  2108. // ERROR CInstance(Win32_UnlogicalDisk)::SetDoohicky(argVal) thing broke! error code: 0xFF1234
  2109. if (hError != -1)
  2110. {
  2111. if (pArgs == NULL)
  2112. {
  2113. LogErrorMessage6(L"%s%s)::%s %s error# %X", IDS_CINSTANCEERROR, (LPCWSTR)className, pFunctionName, errorStr, hError);
  2114. }
  2115. else
  2116. {
  2117. LogErrorMessage7(L"%s%s)::%s(%s) %s error# %X", IDS_CINSTANCEERROR, (LPCWSTR)className, pFunctionName, pArgs, errorStr, hError);
  2118. }
  2119. }
  2120. else
  2121. {
  2122. if (pArgs == NULL)
  2123. {
  2124. LogErrorMessage5(L"%s%s)::%s %s", IDS_CINSTANCEERROR, (LPCWSTR)className, pFunctionName, errorStr);
  2125. }
  2126. else
  2127. {
  2128. LogErrorMessage6(L"%s%s)::%s(%s) %s", IDS_CINSTANCEERROR, (LPCWSTR)className, pFunctionName, pArgs, errorStr);
  2129. }
  2130. }
  2131. }
  2132. }
  2133. bool CInstance::SetCHString(LPCWSTR name, const CHString& str)
  2134. {
  2135. return SetWCHARSplat(name, str);
  2136. }
  2137. bool CInstance::SetCHString(LPCWSTR name, LPCWSTR str)
  2138. {
  2139. return SetWCHARSplat(name, str);
  2140. }
  2141. bool CInstance::SetCharSplat(LPCWSTR name, LPCWSTR pStr)
  2142. {
  2143. return SetWCHARSplat(name, pStr);
  2144. }
  2145. bool CInstance::SetCHString(LPCWSTR name, LPCSTR str)
  2146. {
  2147. return SetWCHARSplat(name, CHString(str));
  2148. }
  2149. bool CInstance::SetCharSplat( LPCWSTR name, LPCSTR pStr)
  2150. {
  2151. return SetWCHARSplat(name, CHString(pStr));
  2152. }
  2153. bool CInstance::SetCharSplat(LPCWSTR name, DWORD dwResID)
  2154. {
  2155. ASSERT_BREAK(DEPRECATED);
  2156. SetLastError(WBEM_E_NOT_SUPPORTED);
  2157. return false;
  2158. }