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.

946 lines
18 KiB

  1. //***************************************************************************
  2. //
  3. // WMIHELPER.CPP
  4. //
  5. // Module: HEALTHMON SERVER AGENT
  6. //
  7. // Purpose: Helper functions and wrappers around WMI
  8. //
  9. // Copyright (c)1999 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "global.h"
  13. #include "wmihelper.h"
  14. extern HMODULE g_hWbemComnModule;
  15. /////////////////////////////////////////////////////////////////////////////////
  16. /////////////////////////////////PROPERTY GETS///////////////////////////////////
  17. /////////////////////////////////////////////////////////////////////////////////
  18. HRESULT GetStrProperty(IWbemClassObject *pObj, LPTSTR pszProp, LPTSTR *pszString)
  19. {
  20. LPTSTR pszPropIn;
  21. VARIANT v;
  22. HRESULT hRetRes = S_OK;
  23. MY_OUTPUT(L"ENTER ***** GetStrProperty...", 0);
  24. MY_ASSERT(pszProp);
  25. if (!pszProp)
  26. {
  27. return WBEM_E_INVALID_PROPERTY;
  28. }
  29. if (pszProp[0] == '\0')
  30. {
  31. pszPropIn = L" ";
  32. }
  33. else
  34. {
  35. pszPropIn = pszProp;
  36. }
  37. VariantInit(&v);
  38. if ((hRetRes = pObj->Get(pszPropIn, 0L, &v, NULL, NULL)) == S_OK)
  39. {
  40. if (V_VT(&v)==VT_NULL)
  41. {
  42. *pszString = new TCHAR[2];
  43. MY_ASSERT(*pszString);
  44. if (!*pszString)
  45. {
  46. hRetRes = WBEM_E_OUT_OF_MEMORY;
  47. }
  48. else
  49. {
  50. wcscpy(*pszString , L"");
  51. }
  52. }
  53. else
  54. {
  55. MY_ASSERT(V_VT(&v)==VT_BSTR);
  56. *pszString = new TCHAR[wcslen(V_BSTR(&v))+2];
  57. MY_ASSERT(*pszString);
  58. if (!*pszString)
  59. {
  60. hRetRes = WBEM_E_OUT_OF_MEMORY;
  61. }
  62. else
  63. {
  64. wcscpy(*pszString , V_BSTR(&v));
  65. }
  66. }
  67. }
  68. else
  69. {
  70. MY_HRESASSERT(hRetRes);
  71. }
  72. VariantClear(&v);
  73. MY_OUTPUT(L"EXIT ***** GetStrProperty...", 0);
  74. return hRetRes;
  75. }
  76. HRESULT GetAsStrProperty(IWbemClassObject *pObj, LPTSTR pszProp, LPTSTR *pszString)
  77. {
  78. LPTSTR pszPropIn;
  79. TCHAR szTemp[1024];
  80. HRESULT hRetRes = S_OK;
  81. VARIANT v;
  82. BOOL bBool;
  83. MY_OUTPUT(L"ENTER ***** GetAsStrProperty...", 0);
  84. MY_ASSERT(pszProp);
  85. if (!pszProp)
  86. {
  87. return WBEM_E_INVALID_PROPERTY;
  88. }
  89. if (pszProp[0] == '\0')
  90. {
  91. pszPropIn = L" ";
  92. }
  93. else
  94. {
  95. pszPropIn = pszProp;
  96. }
  97. VariantInit(&v);
  98. if ((hRetRes = pObj->Get(pszPropIn, 0L, &v, NULL, NULL)) == S_OK)
  99. {
  100. if (V_VT(&v)==VT_NULL)
  101. {
  102. *pszString = new TCHAR[6];
  103. MY_ASSERT(*pszString);
  104. if (!*pszString)
  105. {
  106. hRetRes = WBEM_E_OUT_OF_MEMORY;
  107. }
  108. else
  109. {
  110. wcscpy(*pszString , L"NULL");
  111. }
  112. }
  113. else if (V_VT(&v)==VT_I4)
  114. {
  115. _ultow(V_I4(&v), szTemp, 10);
  116. *pszString = new TCHAR[wcslen(szTemp)+1];
  117. MY_ASSERT(*pszString);
  118. if (!*pszString)
  119. {
  120. hRetRes = WBEM_E_OUT_OF_MEMORY;
  121. }
  122. else
  123. {
  124. wcscpy(*pszString, szTemp);
  125. }
  126. }
  127. else if (V_VT(&v)==VT_BSTR)
  128. {
  129. *pszString = new TCHAR[wcslen(V_BSTR(&v))+2];
  130. MY_ASSERT(*pszString);
  131. if (!*pszString)
  132. {
  133. hRetRes = WBEM_E_OUT_OF_MEMORY;
  134. }
  135. else
  136. {
  137. wcscpy(*pszString, V_BSTR(&v));
  138. }
  139. }
  140. else if (V_VT(&v)==VT_R4)
  141. {
  142. swprintf(szTemp, L"%f.", V_R4(&v));
  143. *pszString = new TCHAR[wcslen(szTemp)+1];
  144. MY_ASSERT(*pszString);
  145. if (!*pszString)
  146. {
  147. hRetRes = WBEM_E_OUT_OF_MEMORY;
  148. }
  149. else
  150. {
  151. wcscpy(*pszString, szTemp);
  152. }
  153. }
  154. else if (V_VT(&v)==VT_BOOL)
  155. {
  156. bBool = V_BOOL(&v);
  157. *pszString = new TCHAR[2];
  158. MY_ASSERT(*pszString);
  159. if (!*pszString)
  160. {
  161. hRetRes = WBEM_E_OUT_OF_MEMORY;
  162. }
  163. else
  164. {
  165. if (bBool != 0.0)
  166. {
  167. wcscpy(*pszString, L"1");
  168. }
  169. else
  170. {
  171. wcscpy(*pszString, L"2");
  172. }
  173. }
  174. }
  175. else if (V_VT(&v)==VT_R8)
  176. {
  177. swprintf(szTemp, L"%lf", V_R8(&v));
  178. *pszString = new TCHAR[wcslen(szTemp)+1];
  179. MY_ASSERT(*pszString);
  180. if (!*pszString)
  181. {
  182. hRetRes = WBEM_E_OUT_OF_MEMORY;
  183. }
  184. else
  185. {
  186. wcscpy(*pszString, szTemp);
  187. }
  188. }
  189. else if (V_VT(&v)==VT_I2)
  190. {
  191. swprintf(szTemp, L"%i", V_I2(&v));
  192. *pszString = new TCHAR[wcslen(szTemp)+1];
  193. MY_ASSERT(*pszString);
  194. if (!*pszString)
  195. {
  196. hRetRes = WBEM_E_OUT_OF_MEMORY;
  197. }
  198. else
  199. {
  200. wcscpy(*pszString, szTemp);
  201. }
  202. }
  203. else if (V_VT(&v)==VT_UI1)
  204. {
  205. swprintf(szTemp, L"%u", V_UI1(&v));
  206. *pszString = new TCHAR[wcslen(szTemp)+1];
  207. MY_ASSERT(*pszString);
  208. if (!*pszString)
  209. {
  210. hRetRes = WBEM_E_OUT_OF_MEMORY;
  211. }
  212. else
  213. {
  214. wcscpy(*pszString, szTemp);
  215. }
  216. }
  217. else
  218. {
  219. // If for example we had a VT_BSTR | VT_ARRAY would we end up here.
  220. *pszString = new TCHAR[4];
  221. MY_ASSERT(*pszString);
  222. if (!*pszString)
  223. {
  224. hRetRes = WBEM_E_OUT_OF_MEMORY;
  225. }
  226. else
  227. {
  228. wcscpy(*pszString, L"???");
  229. }
  230. }
  231. }
  232. else
  233. {
  234. MY_HRESASSERT(hRetRes);
  235. }
  236. VariantClear(&v);
  237. MY_OUTPUT(L"EXIT ***** GetAsStrProperty...", 0);
  238. return hRetRes;
  239. }
  240. HRESULT GetReal32Property(IWbemClassObject *pObj, LPTSTR pszProp, float *pFloat)
  241. {
  242. LPTSTR pszPropIn;
  243. VARIANT v;
  244. HRESULT hRetRes = S_OK;
  245. MY_OUTPUT(L"ENTER ***** GetReal32Property...", 0);
  246. MY_ASSERT(pszProp);
  247. if (!pszProp)
  248. {
  249. return WBEM_E_INVALID_PROPERTY;
  250. }
  251. if (pszProp[0] == '\0')
  252. {
  253. pszPropIn = L" ";
  254. }
  255. else
  256. {
  257. pszPropIn = pszProp;
  258. }
  259. VariantInit(&v);
  260. if ((hRetRes = pObj->Get(pszPropIn, 0L, &v, NULL, NULL)) == S_OK)
  261. {
  262. if (V_VT(&v)==VT_NULL)
  263. {
  264. MY_ASSERT(FALSE);
  265. *pFloat = 0;
  266. }
  267. else
  268. {
  269. MY_ASSERT(V_VT(&v)==VT_R4);
  270. *pFloat = V_R4(&v);
  271. }
  272. }
  273. else
  274. {
  275. MY_HRESASSERT(hRetRes);
  276. }
  277. VariantClear(&v);
  278. MY_OUTPUT(L"EXIT ***** GetReal32Property...", 0);
  279. return hRetRes;
  280. }
  281. HRESULT GetUint8Property(IWbemClassObject *pObj, LPTSTR pszProp, int *pInt)
  282. {
  283. LPTSTR pszPropIn;
  284. VARIANT v;
  285. HRESULT hRetRes = S_OK;
  286. MY_OUTPUT(L"ENTER ***** GetUint8Property...", 0);
  287. MY_ASSERT(pszProp);
  288. if (!pszProp)
  289. {
  290. return WBEM_E_INVALID_PROPERTY;
  291. }
  292. if (pszProp[0] == '\0')
  293. {
  294. pszPropIn = L" ";
  295. }
  296. else
  297. {
  298. pszPropIn = pszProp;
  299. }
  300. VariantInit(&v);
  301. if ((hRetRes = pObj->Get(pszPropIn, 0L, &v, NULL, NULL)) == S_OK)
  302. {
  303. if (V_VT(&v)==VT_NULL)
  304. {
  305. MY_ASSERT(FALSE);
  306. *pInt = 0;
  307. }
  308. else
  309. {
  310. MY_ASSERT(V_VT(&v)==VT_UI1);
  311. *pInt = V_UI1(&v);
  312. }
  313. }
  314. else
  315. {
  316. MY_HRESASSERT(hRetRes);
  317. }
  318. VariantClear(&v);
  319. MY_OUTPUT(L"EXIT ***** GetUint8Property...", 0);
  320. return hRetRes;
  321. }
  322. HRESULT GetUint32Property(IWbemClassObject *pObj, LPTSTR pszProp, long *pLong)
  323. {
  324. LPTSTR pszPropIn;
  325. VARIANT v;
  326. HRESULT hRetRes = S_OK;
  327. MY_OUTPUT(L"ENTER ***** GetUint32Property...", 0);
  328. MY_ASSERT(pszProp);
  329. if (!pszProp)
  330. {
  331. return WBEM_E_INVALID_PROPERTY;
  332. }
  333. if (pszProp[0] == '\0')
  334. {
  335. pszPropIn = L" ";
  336. }
  337. else
  338. {
  339. pszPropIn = pszProp;
  340. }
  341. VariantInit(&v);
  342. if ((hRetRes = pObj->Get(pszPropIn, 0L, &v, NULL, NULL)) == S_OK)
  343. {
  344. if (V_VT(&v)==VT_NULL)
  345. {
  346. MY_ASSERT(FALSE);
  347. *pLong = 0;
  348. }
  349. else
  350. {
  351. MY_ASSERT(V_VT(&v)==VT_I4);
  352. *pLong = V_I4(&v);
  353. }
  354. }
  355. else
  356. {
  357. MY_HRESASSERT(hRetRes);
  358. }
  359. VariantClear(&v);
  360. MY_OUTPUT(L"EXIT ***** GetUint32Property...", 0);
  361. return hRetRes;
  362. }
  363. HRESULT GetUint64Property(IWbemClassObject *pObj, LPTSTR pszProp, unsigned __int64 *pInt64)
  364. {
  365. LPTSTR pszPropIn;
  366. VARIANT v;
  367. HRESULT hRetRes = S_OK;
  368. MY_OUTPUT(L"ENTER ***** GetUint64Property...", 0);
  369. MY_ASSERT(pszProp);
  370. if (!pszProp)
  371. {
  372. return WBEM_E_INVALID_PROPERTY;
  373. }
  374. if (pszProp[0] == '\0')
  375. {
  376. pszPropIn = L" ";
  377. }
  378. else
  379. {
  380. pszPropIn = pszProp;
  381. }
  382. VariantInit(&v);
  383. if ((hRetRes = pObj->Get(pszPropIn, 0L, &v, NULL, NULL)) == S_OK)
  384. {
  385. if (V_VT(&v)==VT_NULL)
  386. {
  387. MY_ASSERT(FALSE);
  388. *pInt64 = 0;
  389. }
  390. else
  391. {
  392. MY_ASSERT(V_VT(&v)==VT_BSTR);
  393. *pInt64 = 0;
  394. ReadUI64(V_BSTR(&v), *pInt64);
  395. }
  396. }
  397. else
  398. {
  399. MY_HRESASSERT(hRetRes);
  400. }
  401. VariantClear(&v);
  402. MY_OUTPUT(L"EXIT ***** GetUint64Property...", 0);
  403. return hRetRes;
  404. }
  405. HRESULT GetBoolProperty(IWbemClassObject *pObj, LPTSTR pszProp, BOOL *pBool)
  406. {
  407. LPTSTR pszPropIn;
  408. VARIANT v;
  409. HRESULT hRetRes = S_OK;
  410. MY_OUTPUT(L"ENTER ***** GetBoolProperty...", 0);
  411. MY_ASSERT(pszProp);
  412. if (!pszProp)
  413. {
  414. return WBEM_E_INVALID_PROPERTY;
  415. }
  416. if (pszProp[0] == '\0')
  417. {
  418. pszPropIn = L" ";
  419. }
  420. else
  421. {
  422. pszPropIn = pszProp;
  423. }
  424. VariantInit(&v);
  425. if ((hRetRes = pObj->Get(pszPropIn, 0L, &v, NULL, NULL)) == S_OK)
  426. {
  427. if (V_VT(&v)==VT_NULL)
  428. {
  429. MY_ASSERT(FALSE);
  430. *pBool = (BOOL)0.0;
  431. }
  432. else
  433. {
  434. MY_ASSERT(V_VT(&v)==VT_BOOL);
  435. *pBool = V_BOOL(&v);
  436. if (*pBool != 0.0)
  437. {
  438. *pBool = (BOOL)1.0;
  439. }
  440. }
  441. }
  442. else
  443. {
  444. MY_HRESASSERT(hRetRes);
  445. }
  446. VariantClear(&v);
  447. MY_OUTPUT(L"EXIT ***** GetBoolProperty...", 0);
  448. return hRetRes;
  449. }
  450. /////////////////////////////////////////////////////////////////////////////////
  451. /////////////////////////////////PROPERTY PUTS///////////////////////////////////
  452. /////////////////////////////////////////////////////////////////////////////////
  453. HRESULT PutSAProperty(IWbemClassObject *pClassObject, LPTSTR pszProp, SAFEARRAY* psa)
  454. {
  455. LPTSTR pszPropIn;
  456. VARIANT v;
  457. HRESULT hRetRes = S_OK;
  458. MY_OUTPUT(L"ENTER ***** PutSAProperty...", 0);
  459. MY_ASSERT(pszProp);
  460. if (!pszProp)
  461. {
  462. return WBEM_E_INVALID_PROPERTY;
  463. }
  464. if (pszProp[0] == '\0')
  465. {
  466. pszPropIn = L" ";
  467. }
  468. else
  469. {
  470. pszPropIn = pszProp;
  471. }
  472. VariantInit(&v);
  473. V_VT(&v) = VT_UNKNOWN | VT_ARRAY;
  474. V_ARRAY(&v) = psa;
  475. if ((hRetRes = pClassObject->Put(pszPropIn, 0, &v, 0)) != S_OK)
  476. {
  477. MY_OUTPUT(L"Couldn't do an SA put", 4);
  478. MY_HRESASSERT(hRetRes);
  479. }
  480. VariantClear(&v);
  481. MY_OUTPUT(L"EXIT ***** PutSAProperty...", 0);
  482. return hRetRes;
  483. }
  484. HRESULT PutStrProperty(IWbemClassObject *pClassObject, LPTSTR pszProp, LPTSTR pszString)
  485. {
  486. BSTR bstrString;
  487. LPTSTR pszPropIn;
  488. VARIANT v;
  489. HRESULT hRetRes = S_OK;
  490. MY_OUTPUT(L"ENTER ***** PutSAProperty...", 0);
  491. MY_ASSERT(pszProp);
  492. if (!pszProp)
  493. {
  494. return WBEM_E_INVALID_PROPERTY;
  495. }
  496. //XXXWhat want to put a NULL??? Same question for all Put methods.
  497. //On the other hand, if the default for the class property is NULL, then don't need it
  498. if (!pszString)
  499. {
  500. return S_OK;
  501. }
  502. if (pszProp[0] == '\0')
  503. {
  504. pszPropIn = L" ";
  505. }
  506. else
  507. {
  508. pszPropIn = pszProp;
  509. }
  510. VariantInit(&v);
  511. V_VT(&v) = VT_BSTR;
  512. if (pszString[0] == '\0')
  513. {
  514. bstrString = SysAllocString(L" ");
  515. MY_ASSERT(bstrString); if (!bstrString) {return WBEM_E_OUT_OF_MEMORY;}
  516. bstrString[0] = '\0';
  517. }
  518. else
  519. {
  520. bstrString = SysAllocString(pszString);
  521. }
  522. MY_ASSERT(bstrString); if (!bstrString) {return WBEM_E_OUT_OF_MEMORY;}
  523. V_BSTR(&v) = bstrString;
  524. if ((hRetRes = pClassObject->Put(pszPropIn, 0, &v, 0)) != S_OK)
  525. {
  526. MY_OUTPUT(L"Couldn't do a string put", 4);
  527. MY_HRESASSERT(hRetRes);
  528. }
  529. VariantClear(&v);
  530. MY_OUTPUT(L"EXIT ***** PutStrProperty...", 0);
  531. return hRetRes;
  532. }
  533. HRESULT PutUint32Property(IWbemClassObject *pClassObject, LPTSTR pszProp, long lValue)
  534. {
  535. LPTSTR pszPropIn;
  536. VARIANT v;
  537. HRESULT hRetRes = S_OK;
  538. MY_OUTPUT(L"ENTER ***** PutUint32Property...", 0);
  539. MY_ASSERT(pszProp);
  540. if (!pszProp)
  541. {
  542. return WBEM_E_INVALID_PROPERTY;
  543. }
  544. if (pszProp[0] == '\0')
  545. {
  546. pszPropIn = L" ";
  547. }
  548. else
  549. {
  550. pszPropIn = pszProp;
  551. }
  552. VariantInit(&v);
  553. V_VT(&v) = VT_I4;
  554. V_I4(&v) = lValue;
  555. if ((hRetRes = pClassObject->Put(pszPropIn, 0, &v, 0)) != S_OK)
  556. {
  557. MY_OUTPUT(L"Couldn't do a uint32 put", 4);
  558. MY_HRESASSERT(hRetRes);
  559. }
  560. VariantClear(&v);
  561. MY_OUTPUT(L"EXIT ***** PutUint32Property...", 0);
  562. return hRetRes;
  563. }
  564. HRESULT PutUUint32Property(IWbemClassObject *pClassObject, LPTSTR pszProp, unsigned long ulValue)
  565. {
  566. LPTSTR pszPropIn;
  567. VARIANT v;
  568. HRESULT hRetRes = S_OK;
  569. MY_OUTPUT(L"ENTER ***** PutUint32Property...", 0);
  570. MY_ASSERT(pszProp);
  571. if (!pszProp)
  572. {
  573. return WBEM_E_INVALID_PROPERTY;
  574. }
  575. if (pszProp[0] == '\0')
  576. {
  577. pszPropIn = L" ";
  578. }
  579. else
  580. {
  581. pszPropIn = pszProp;
  582. }
  583. VariantInit(&v);
  584. V_VT(&v) = VT_I4;
  585. V_I4(&v) = ulValue;
  586. if ((hRetRes = pClassObject->Put(pszPropIn, 0, &v, 0)) != S_OK)
  587. {
  588. MY_OUTPUT(L"Couldn't do a uint32 put", 4);
  589. MY_HRESASSERT(hRetRes);
  590. }
  591. VariantClear(&v);
  592. MY_OUTPUT(L"EXIT ***** PutUint32Property...", 0);
  593. return hRetRes;
  594. }
  595. HRESULT PutReal32Property(IWbemClassObject *pClassObject, LPTSTR pszProp, float fValue)
  596. {
  597. LPTSTR pszPropIn;
  598. VARIANT v;
  599. HRESULT hRetRes = S_OK;
  600. MY_OUTPUT(L"ENTER ***** PutReal32Property...", 0);
  601. MY_ASSERT(pszProp);
  602. if (!pszProp)
  603. {
  604. return WBEM_E_INVALID_PROPERTY;
  605. }
  606. if (pszProp[0] == '\0')
  607. {
  608. pszPropIn = L" ";
  609. }
  610. else
  611. {
  612. pszPropIn = pszProp;
  613. }
  614. VariantInit(&v);
  615. V_VT(&v) = VT_R4;
  616. V_R4(&v) = fValue;
  617. if ((hRetRes = pClassObject->Put(pszPropIn, 0, &v, 0)) != S_OK)
  618. {
  619. MY_OUTPUT(L"Couldn't do a real32 put", 4);
  620. MY_HRESASSERT(hRetRes);
  621. }
  622. VariantClear(&v);
  623. MY_OUTPUT(L"EXIT ***** PutReal32Property...", 0);
  624. return hRetRes;
  625. }
  626. HRESULT PutBoolProperty(IWbemClassObject *pClassObject, LPTSTR pszProp, BOOL bValue)
  627. {
  628. LPTSTR pszPropIn;
  629. VARIANT v;
  630. HRESULT hRetRes = S_OK;
  631. MY_OUTPUT(L"ENTER ***** PutBoolProperty...", 0);
  632. MY_ASSERT(pszProp);
  633. if (!pszProp)
  634. {
  635. return WBEM_E_INVALID_PROPERTY;
  636. }
  637. if (pszProp[0] == '\0')
  638. {
  639. pszPropIn = L" ";
  640. }
  641. else
  642. {
  643. pszPropIn = pszProp;
  644. }
  645. VariantInit(&v);
  646. V_VT(&v) = VT_BOOL;
  647. V_BOOL(&v) = (short) bValue;
  648. if ((hRetRes = pClassObject->Put(pszPropIn, 0, &v, 0)) != S_OK)
  649. {
  650. MY_OUTPUT(L"Couldn't do a bool put", 4);
  651. MY_HRESASSERT(hRetRes);
  652. }
  653. VariantClear(&v);
  654. MY_OUTPUT(L"EXIT ***** PutBoolProperty...", 0);
  655. return hRetRes;
  656. }
  657. /////////////////////////////////////////////////////////////////////////////////
  658. /////////////////////////////////QUALIFIER GETS//////////////////////////////////
  659. /////////////////////////////////////////////////////////////////////////////////
  660. /////////////////////////////////////////////////////////////////////////////////
  661. /////////////////////////////////MISC GETS///////////////////////////////////////
  662. /////////////////////////////////////////////////////////////////////////////////
  663. HRESULT GetWbemObjectInst(IWbemServices** ppSvc, LPCTSTR pszName, IWbemContext *pContext, IWbemClassObject **pOutObj)
  664. {
  665. HRESULT hRetRes = S_OK;
  666. BSTR bstrClassName = NULL;
  667. *pOutObj = NULL;
  668. MY_OUTPUT(L"ENTER ***** GetWbemObjectInst...", 0);
  669. MY_ASSERT(pszName);
  670. if (!pszName)
  671. {
  672. return WBEM_E_INVALID_PROPERTY;
  673. }
  674. if (pszName[0] == '\0')
  675. {
  676. bstrClassName = SysAllocString(L" ");
  677. MY_ASSERT(bstrClassName); if (!bstrClassName) return WBEM_E_OUT_OF_MEMORY;
  678. bstrClassName[0] = '\0';
  679. }
  680. else
  681. {
  682. bstrClassName = SysAllocString(pszName);
  683. }
  684. MY_ASSERT(bstrClassName); if (!bstrClassName) return WBEM_E_OUT_OF_MEMORY;
  685. hRetRes = (*ppSvc)->GetObject(bstrClassName, 0L, pContext, pOutObj, NULL);
  686. if (FAILED(hRetRes))
  687. {
  688. MY_OUTPUT(L"GetWbemObjectInst:Failed to get object instance", 0);
  689. // MY_HRESASSERT(hRetRes);
  690. }
  691. SysFreeString(bstrClassName);
  692. MY_OUTPUT(L"EXIT ***** GetWbemObjectInst...", 0);
  693. return hRetRes;
  694. }
  695. HRESULT GetWbemObjectInstSemiSync(IWbemServices** ppSvc, LPCTSTR pszName, IWbemContext *pContext, IWbemCallResult **pOutObj)
  696. {
  697. HRESULT hRetRes = S_OK;
  698. BSTR bstrPropName = NULL;
  699. MY_OUTPUT(L"ENTER ***** GetWbemObjectInstSemiSync...", 0);
  700. MY_ASSERT(pszName);
  701. if (!pszName)
  702. {
  703. return WBEM_E_INVALID_PROPERTY;
  704. }
  705. if (pszName[0] == '\0')
  706. {
  707. bstrPropName = SysAllocString(L" ");
  708. MY_ASSERT(bstrPropName); if (!bstrPropName) return WBEM_E_OUT_OF_MEMORY;
  709. bstrPropName[0] = '\0';
  710. }
  711. else
  712. {
  713. bstrPropName = SysAllocString(pszName);
  714. }
  715. MY_ASSERT(bstrPropName); if (!bstrPropName) return WBEM_E_OUT_OF_MEMORY;
  716. hRetRes = (*ppSvc)->GetObject(bstrPropName, WBEM_FLAG_RETURN_IMMEDIATELY, pContext, NULL, pOutObj);
  717. if (FAILED(hRetRes))
  718. {
  719. MY_OUTPUT(L"GetWbemObjectInst:Failed to get object instance", 0);
  720. MY_HRESASSERT(hRetRes);
  721. }
  722. SysFreeString(bstrPropName);
  723. MY_OUTPUT(L"EXIT ***** GetWbemObjectInstSemiSync...", 0);
  724. return hRetRes;
  725. }
  726. HRESULT SendEvents(IWbemObjectSink* pEventSink, IWbemClassObject** pInstances, int iSize)
  727. {
  728. HRESULT hRetRes = S_OK;
  729. if (iSize == 0)
  730. return S_FALSE;
  731. // Send event to the sink if there's a listener
  732. if (pEventSink)
  733. {
  734. hRetRes = pEventSink->Indicate(iSize, pInstances);
  735. //WBEM_E_SERVER_TOO_BUSY is Ok. Wbem will deliver.
  736. if (FAILED(hRetRes) && hRetRes != WBEM_E_SERVER_TOO_BUSY)
  737. {
  738. MY_OUTPUT(L"Failed on Indicate!", 4);
  739. MY_HRESASSERT(hRetRes);
  740. }
  741. }
  742. return hRetRes;
  743. }
  744. HRESULT GetWbemClassObject(IWbemClassObject** ppObj, VARIANT* v)
  745. {
  746. HRESULT hRetRes = S_OK;
  747. IUnknown* pDispatch = (IUnknown*)V_UNKNOWN(v);
  748. pDispatch->AddRef();
  749. hRetRes = pDispatch->QueryInterface(IID_IWbemClassObject, (LPVOID*)ppObj);
  750. MY_HRESASSERT(hRetRes);
  751. pDispatch->Release();
  752. return hRetRes;
  753. }
  754. // This is only filled in if the provider supplies it, and in most cases it does not.
  755. // Try using IWbemStatusText, or some other interface
  756. BOOL GetLatestWMIError(int code, HRESULT hResIn, LPTSTR pszString)
  757. {
  758. LPVOID lpMsgBuf;
  759. VARIANT varString;
  760. SCODE sc;
  761. IWbemClassObject *pErrorObject = NULL;
  762. IErrorInfo* pEI = NULL;
  763. TCHAR szError[1024];
  764. if(GetErrorInfo(0, &pEI) == S_OK)
  765. {
  766. pEI->QueryInterface(IID_IWbemClassObject, (void**)&pErrorObject);
  767. pEI->Release();
  768. }
  769. if (pErrorObject != NULL)
  770. {
  771. VariantInit(&varString);
  772. if (pErrorObject->InheritsFrom(L"__ExtendedStatus") == WBEM_NO_ERROR)
  773. {
  774. sc = pErrorObject->Get(L"Description", 0L, &varString, NULL, NULL);
  775. if (sc != S_OK)
  776. {
  777. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_MISSINGDESC, pszString, 1023))
  778. {
  779. wcscpy(pszString, L"Unknown string. (Can't locate resource DLL)");
  780. }
  781. else
  782. {
  783. swprintf(szError, L"0x%08x : ", sc);
  784. wcsncat(pszString, szError, 1023-wcslen(pszString));
  785. pszString[1023] = '\0';
  786. }
  787. }
  788. else if (V_VT(&varString) == VT_BSTR)
  789. {
  790. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_DESC, pszString, 1023))
  791. {
  792. wcscpy(pszString, L"Unknown string. (Can't locate resource DLL)");
  793. }
  794. else
  795. {
  796. swprintf(szError, L"%256wS.", V_BSTR(&varString));
  797. wcscat(pszString, szError);
  798. }
  799. }
  800. VariantClear(&varString);
  801. }
  802. else
  803. {
  804. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_BADERROR, pszString, 1023))
  805. {
  806. wcscpy(pszString, L"Unknown string. (Can't locate resource DLL)");
  807. }
  808. }
  809. pErrorObject->Release();
  810. }
  811. szError[0] = '\0';
  812. GetLatestAgentError(code, szError);
  813. wcsncat(pszString, szError, 1023-wcslen(pszString));
  814. pszString[1023] = '\0';
  815. wsprintf(szError, L" 0x%08x : ", hResIn);
  816. wcsncat(pszString, szError, 1023-wcslen(pszString));
  817. pszString[1023] = '\0';
  818. if (g_hWbemComnModule)
  819. {
  820. FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_HMODULE,
  821. g_hWbemComnModule, hResIn, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
  822. //MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
  823. if (lpMsgBuf)
  824. {
  825. wcsncat(pszString, (LPCTSTR)lpMsgBuf, 1023-wcslen(pszString));
  826. pszString[1023] = '\0';
  827. LocalFree(lpMsgBuf);
  828. wcsncat(pszString, L". ", 1023-wcslen(pszString));
  829. pszString[1023] = '\0';
  830. }
  831. }
  832. return TRUE;
  833. }
  834. BOOL GetLatestAgentError(int code, LPTSTR pszString)
  835. {
  836. if (g_hResLib == NULL || !LoadString(g_hResLib, code, pszString, 1023))
  837. {
  838. wcscpy(pszString, L"Unknown state. (Can't locate resource DLL)");
  839. }
  840. return TRUE;
  841. }