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.

1972 lines
46 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1998-2000 Microsoft Corporation
  4. //
  5. // OBJECT.CPP
  6. //
  7. // alanbos 15-Aug-96 Created.
  8. //
  9. // Defines the implementation of ISWbemObjectEx
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. //***************************************************************************
  14. //
  15. // CSWbemObject::CSWbemObject
  16. //
  17. // DESCRIPTION:
  18. //
  19. // Constructor.
  20. //
  21. //***************************************************************************
  22. CSWbemObject::CSWbemObject(CSWbemServices *pService, IWbemClassObject *pObject,
  23. CSWbemSecurity *pSecurity,
  24. bool isErrorObject) :
  25. m_pSWbemServices (NULL),
  26. m_pSite (NULL),
  27. m_pIWbemRefresher (NULL),
  28. m_bCanUseRefresher (true)
  29. {
  30. m_cRef=0;
  31. m_isErrorObject = isErrorObject;
  32. m_pIWbemClassObject = pObject;
  33. m_pIWbemClassObject->AddRef ();
  34. m_pIServiceProvider = NULL;
  35. if (pService)
  36. {
  37. m_pSWbemServices = new CSWbemServices (pService, pSecurity);
  38. if (m_pSWbemServices)
  39. m_pSWbemServices->AddRef ();
  40. }
  41. m_pDispatch = new CWbemDispatchMgr (m_pSWbemServices, this);
  42. InterlockedIncrement(&g_cObj);
  43. }
  44. //***************************************************************************
  45. //
  46. // CSWbemObject::~CSWbemObject
  47. //
  48. // DESCRIPTION:
  49. //
  50. // Destructor.
  51. //
  52. //***************************************************************************
  53. CSWbemObject::~CSWbemObject(void)
  54. {
  55. InterlockedDecrement(&g_cObj);
  56. RELEASEANDNULL(m_pIWbemClassObject)
  57. RELEASEANDNULL(m_pSWbemServices)
  58. RELEASEANDNULL(m_pSite)
  59. RELEASEANDNULL(m_pIWbemRefresher)
  60. DELETEANDNULL(m_pDispatch);
  61. }
  62. //***************************************************************************
  63. // HRESULT CSWbemObject::QueryInterface
  64. // long CSWbemObject::AddRef
  65. // long CSWbemObject::Release
  66. //
  67. // DESCRIPTION:
  68. //
  69. // Standard Com IUNKNOWN functions.
  70. //
  71. //***************************************************************************
  72. STDMETHODIMP CSWbemObject::QueryInterface (
  73. IN REFIID riid,
  74. OUT LPVOID *ppv
  75. )
  76. {
  77. *ppv=NULL;
  78. /*
  79. * Only acknowledge the last error or object safety
  80. * interfaces if we are an error object.
  81. */
  82. if (IID_IUnknown==riid)
  83. *ppv = reinterpret_cast<IUnknown*>(this);
  84. else if (IID_ISWbemObject==riid)
  85. *ppv = (ISWbemObject *)this;
  86. else if (IID_ISWbemObjectEx==riid)
  87. *ppv = (ISWbemObjectEx *)this;
  88. else if (IID_IDispatch==riid)
  89. *ppv = (IDispatch *)((ISWbemObjectEx *)this);
  90. else if (IID_IDispatchEx==riid)
  91. *ppv = (IDispatchEx *)this;
  92. else if (IID_ISWbemInternalObject==riid)
  93. *ppv = (ISWbemInternalObject *) this;
  94. else if (IID_ISupportErrorInfo==riid)
  95. *ppv = (ISupportErrorInfo *)this;
  96. else if (IID_IProvideClassInfo==riid)
  97. *ppv = (IProvideClassInfo *)this;
  98. else if (m_isErrorObject)
  99. {
  100. if (IID_ISWbemLastError==riid)
  101. *ppv = (ISWbemObject *) this;
  102. else if (IID_IObjectSafety==riid)
  103. *ppv = (IObjectSafety *) this;
  104. }
  105. else if (IID_IObjectSafety==riid)
  106. {
  107. /*
  108. * Explicit check because we don't want
  109. * this interface hijacked by a custom interface.
  110. */
  111. *ppv = NULL;
  112. }
  113. if (NULL!=*ppv)
  114. {
  115. ((LPUNKNOWN)*ppv)->AddRef();
  116. return NOERROR;
  117. }
  118. return ResultFromScode(E_NOINTERFACE);
  119. }
  120. STDMETHODIMP_(ULONG) CSWbemObject::AddRef(void)
  121. {
  122. InterlockedIncrement(&m_cRef);
  123. return m_cRef;
  124. }
  125. STDMETHODIMP_(ULONG) CSWbemObject::Release(void)
  126. {
  127. InterlockedDecrement(&m_cRef);
  128. if (0L!=m_cRef)
  129. return m_cRef;
  130. delete this;
  131. return 0;
  132. }
  133. // IDispatch methods should be inline
  134. STDMETHODIMP CSWbemObject::GetTypeInfoCount(UINT* pctinfo)
  135. {
  136. _RD(static char *me = "CSWbemObject::GetTypeInfoCount()";)
  137. _RPrint(me, "Called", 0, "");
  138. return (m_pDispatch ? m_pDispatch->GetTypeInfoCount(pctinfo) : E_FAIL);}
  139. STDMETHODIMP CSWbemObject::GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
  140. {
  141. _RD(static char *me = "CSWbemObject::GetTypeInfo()";)
  142. _RPrint(me, "Called", 0, "");
  143. return (m_pDispatch ? m_pDispatch->GetTypeInfo(itinfo, lcid, pptinfo) : E_FAIL);}
  144. STDMETHODIMP CSWbemObject::GetIDsOfNames(REFIID riid, OLECHAR** rgszNames,
  145. UINT cNames, LCID lcid, DISPID* rgdispid)
  146. {
  147. _RD(static char *me = "CSWbemObject::GetIdsOfNames()";)
  148. _RPrint(me, "Called", 0, "");
  149. return (m_pDispatch ? m_pDispatch->GetIDsOfNames(riid, rgszNames, cNames,
  150. lcid,
  151. rgdispid) : E_FAIL);}
  152. STDMETHODIMP CSWbemObject::Invoke(DISPID dispidMember, REFIID riid, LCID lcid,
  153. WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
  154. EXCEPINFO* pexcepinfo, UINT* puArgErr)
  155. {
  156. _RD(static char *me = "CSWbemObject::Invoke()";)
  157. _RPrint(me, "Called", 0, "");
  158. return (m_pDispatch ? m_pDispatch->Invoke(dispidMember, riid, lcid, wFlags,
  159. pdispparams, pvarResult, pexcepinfo, puArgErr) : E_FAIL);}
  160. // IDispatchEx methods should be inline
  161. HRESULT STDMETHODCALLTYPE CSWbemObject::GetDispID(
  162. /* [in] */ BSTR bstrName,
  163. /* [in] */ DWORD grfdex,
  164. /* [out] */ DISPID __RPC_FAR *pid)
  165. {
  166. _RD(static char *me = "CSWbemObject::GetDispID()";)
  167. _RPrint(me, "Called", 0, "");
  168. return (m_pDispatch ? m_pDispatch->GetDispID(bstrName, grfdex, pid) : E_FAIL);
  169. }
  170. /* [local] */ HRESULT STDMETHODCALLTYPE CSWbemObject::InvokeEx(
  171. /* [in] */ DISPID id,
  172. /* [in] */ LCID lcid,
  173. /* [in] */ WORD wFlags,
  174. /* [in] */ DISPPARAMS __RPC_FAR *pdp,
  175. /* [out] */ VARIANT __RPC_FAR *pvarRes,
  176. /* [out] */ EXCEPINFO __RPC_FAR *pei,
  177. /* [unique][in] */ IServiceProvider __RPC_FAR *pspCaller)
  178. {
  179. HRESULT hr;
  180. _RD(static char *me = "CSWbemObject::InvokeEx()";)
  181. _RPrint(me, "Called", (long)id, "id");
  182. _RPrint(me, "Called", (long)wFlags, "wFlags");
  183. /*
  184. * Store away the service provider so that it can be accessed
  185. * by calls that remote to CIMOM
  186. */
  187. if (m_pDispatch)
  188. {
  189. m_pIServiceProvider = pspCaller;
  190. hr = m_pDispatch->InvokeEx(id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
  191. m_pIServiceProvider = NULL;
  192. }
  193. else
  194. {
  195. hr = E_FAIL;
  196. }
  197. return hr;
  198. }
  199. HRESULT STDMETHODCALLTYPE CSWbemObject::DeleteMemberByName(
  200. /* [in] */ BSTR bstr,
  201. /* [in] */ DWORD grfdex)
  202. {
  203. _RD(static char *me = "CSWbemObject::DeleteMemberByName()";)
  204. _RPrint(me, "Called", 0, "");
  205. return m_pDispatch->DeleteMemberByName(bstr, grfdex);
  206. }
  207. HRESULT STDMETHODCALLTYPE CSWbemObject::DeleteMemberByDispID(
  208. /* [in] */ DISPID id)
  209. {
  210. _RD(static char *me = "CSWbemObject::DeletememberByDispId()";)
  211. _RPrint(me, "Called", 0, "");
  212. return (m_pDispatch ? m_pDispatch->DeleteMemberByDispID(id) : E_FAIL);
  213. }
  214. HRESULT STDMETHODCALLTYPE CSWbemObject::GetMemberProperties(
  215. /* [in] */ DISPID id,
  216. /* [in] */ DWORD grfdexFetch,
  217. /* [out] */ DWORD __RPC_FAR *pgrfdex)
  218. {
  219. _RD(static char *me = "CSWbemObject::GetMemberProperties()";)
  220. _RPrint(me, "Called", 0, "");
  221. return (m_pDispatch ? m_pDispatch->GetMemberProperties(id, grfdexFetch, pgrfdex) : E_FAIL);
  222. }
  223. HRESULT STDMETHODCALLTYPE CSWbemObject::GetMemberName(
  224. /* [in] */ DISPID id,
  225. /* [out] */ BSTR __RPC_FAR *pbstrName)
  226. {
  227. _RD(static char *me = "CSWbemObject::GetMemberName()";)
  228. _RPrint(me, "Called", 0, "");
  229. return (m_pDispatch ? m_pDispatch->GetMemberName(id, pbstrName) : E_FAIL);
  230. }
  231. /*
  232. * I don't think this needs implementing
  233. */
  234. HRESULT STDMETHODCALLTYPE CSWbemObject::GetNextDispID(
  235. /* [in] */ DWORD grfdex,
  236. /* [in] */ DISPID id,
  237. /* [out] */ DISPID __RPC_FAR *pid)
  238. {
  239. _RD(static char *me = "CSWbemObject::GetNextDispID()";)
  240. _RPrint(me, "Called", 0, "");
  241. return S_FALSE;
  242. }
  243. HRESULT STDMETHODCALLTYPE CSWbemObject::GetNameSpaceParent(
  244. /* [out] */ IUnknown __RPC_FAR *__RPC_FAR *ppunk)
  245. {
  246. _RD(static char *me = "CSWbemObject::GetNamespaceParent()";)
  247. _RPrint(me, "Called", 0, "");
  248. return (m_pDispatch ? m_pDispatch->GetNameSpaceParent(ppunk) : E_FAIL);
  249. }
  250. //***************************************************************************
  251. // HRESULT CSWbemObject::InterfaceSupportsErrorInfo
  252. //
  253. // DESCRIPTION:
  254. //
  255. // Standard Com ISupportErrorInfo functions.
  256. //
  257. //***************************************************************************
  258. STDMETHODIMP CSWbemObject::InterfaceSupportsErrorInfo (IN REFIID riid)
  259. {
  260. return ((IID_ISWbemObject == riid) ||
  261. (IID_ISWbemObjectEx == riid)) ? S_OK : S_FALSE;
  262. }
  263. //***************************************************************************
  264. //
  265. // CSWbemObject::GetIWbemClassObject
  266. //
  267. // DESCRIPTION:
  268. //
  269. // Return the IWbemClassObject interface corresponding to this
  270. // scriptable wrapper.
  271. //
  272. // PARAMETERS:
  273. // ppObject holds the IWbemClassObject pointer on return
  274. //
  275. // RETURN VALUES:
  276. // S_OK success
  277. // E_FAIL otherwise
  278. //
  279. // NOTES:
  280. // If successful, the returned interface is AddRef'd; the caller is
  281. // responsible for release.
  282. //
  283. //***************************************************************************
  284. STDMETHODIMP CSWbemObject::GetIWbemClassObject (IWbemClassObject **ppObject)
  285. {
  286. HRESULT hr = E_FAIL;
  287. if (ppObject)
  288. *ppObject = NULL;
  289. if (m_pIWbemClassObject)
  290. {
  291. m_pIWbemClassObject->AddRef ();
  292. *ppObject = m_pIWbemClassObject;
  293. hr = S_OK;
  294. }
  295. return hr;
  296. }
  297. //***************************************************************************
  298. //
  299. // CSWbemObject::SetIWbemClassObject
  300. //
  301. // DESCRIPTION:
  302. //
  303. // Set a new IWbemClassObject interface inside this scriptable wrapper.
  304. //
  305. // PARAMETERS:
  306. // pIWbemClassObject - the new IWbemClassObject
  307. //
  308. // RETURN VALUES:
  309. // S_OK success
  310. // E_FAIL otherwise
  311. //
  312. // NOTES:
  313. // If successful, the returned interface is AddRef'd; the caller is
  314. // responsible for release.
  315. //
  316. //***************************************************************************
  317. void CSWbemObject::SetIWbemClassObject (
  318. IWbemClassObject *pIWbemClassObject
  319. )
  320. {
  321. if (m_pIWbemClassObject)
  322. m_pIWbemClassObject->Release ();
  323. m_pIWbemClassObject = pIWbemClassObject;
  324. if (m_pIWbemClassObject)
  325. m_pIWbemClassObject->AddRef ();
  326. if (m_pDispatch)
  327. m_pDispatch->SetNewObject (m_pIWbemClassObject);
  328. };
  329. //***************************************************************************
  330. //
  331. // SCODE CSWbemObject::Put_
  332. //
  333. // DESCRIPTION:
  334. //
  335. // Save/commit this class or instance into a namespace
  336. //
  337. // PARAMETERS:
  338. //
  339. // lFlags Flags
  340. // pContext Context
  341. // ppObjectPath Object Path
  342. //
  343. // RETURN VALUES:
  344. //
  345. // WBEM_S_NO_ERROR success
  346. // WBEM_E_INVALID_PARAMETER bad input parameters
  347. // WBEM_E_FAILED otherwise
  348. //
  349. //***************************************************************************
  350. HRESULT CSWbemObject::Put_ (
  351. long lFlags,
  352. /*ISWbemNamedValueSet*/ IDispatch *pContext,
  353. ISWbemObjectPath **ppObjectPath
  354. )
  355. {
  356. HRESULT hr = WBEM_E_FAILED;
  357. ResetLastErrors ();
  358. if (m_pSWbemServices)
  359. {
  360. if (m_pIWbemClassObject)
  361. {
  362. // Figure out whether this is a class or instance
  363. VARIANT var;
  364. VariantInit (&var);
  365. if (WBEM_S_NO_ERROR == m_pIWbemClassObject->Get (WBEMS_SP_GENUS, 0, &var, NULL, NULL))
  366. {
  367. IWbemContext *pIContext = CSWbemNamedValueSet::GetIWbemContext (pContext, m_pIServiceProvider);
  368. IWbemServices *pIService = m_pSWbemServices->GetIWbemServices ();
  369. IWbemCallResult *pResult = NULL;
  370. HRESULT hrCallResult = WBEM_E_FAILED;
  371. if (pIService)
  372. {
  373. CSWbemSecurity *pSecurity = m_pSWbemServices->GetSecurityInfo ();
  374. if (pSecurity)
  375. {
  376. bool needToResetSecurity = false;
  377. HANDLE hThreadToken = NULL;
  378. if (pSecurity->SetSecurity (needToResetSecurity, hThreadToken))
  379. {
  380. if (WBEM_GENUS_CLASS == var.lVal)
  381. hrCallResult = pIService->PutClass
  382. (m_pIWbemClassObject, lFlags | WBEM_FLAG_RETURN_IMMEDIATELY, pIContext, &pResult);
  383. else
  384. hrCallResult = pIService->PutInstance
  385. (m_pIWbemClassObject, lFlags | WBEM_FLAG_RETURN_IMMEDIATELY, pIContext, &pResult);
  386. }
  387. if (needToResetSecurity)
  388. pSecurity->ResetSecurity (hThreadToken);
  389. pSecurity->Release ();
  390. }
  391. pIService->Release ();
  392. }
  393. /*
  394. * Secure the IWbemCallResult interface
  395. */
  396. if (WBEM_S_NO_ERROR == hrCallResult)
  397. {
  398. CSWbemSecurity *pSecurity = m_pSWbemServices->GetSecurityInfo ();
  399. if (pSecurity)
  400. pSecurity->SecureInterface (pResult);
  401. if ((WBEM_S_NO_ERROR == (hrCallResult = pResult->GetCallStatus (INFINITE, &hr))) &&
  402. (WBEM_S_NO_ERROR == hr))
  403. {
  404. if (ppObjectPath)
  405. {
  406. ISWbemObjectPath *pObjectPath =
  407. new CSWbemObjectPath (pSecurity, m_pSWbemServices->GetLocale());
  408. if (!pObjectPath)
  409. hr = WBEM_E_OUT_OF_MEMORY;
  410. else
  411. {
  412. pObjectPath->AddRef ();
  413. pObjectPath->put_Path (m_pSWbemServices->GetPath ());
  414. if (WBEM_GENUS_CLASS == var.lVal)
  415. {
  416. VARIANT nameVar;
  417. VariantInit (&nameVar);
  418. /*
  419. * Note we must check that returned value is a BSTR - it could be a VT_NULL if
  420. * the __CLASS property has not yet been set.
  421. */
  422. if ((WBEM_S_NO_ERROR == m_pIWbemClassObject->Get (WBEMS_SP_CLASS, 0, &nameVar, NULL, NULL))
  423. && (VT_BSTR == V_VT(&nameVar)))
  424. {
  425. pObjectPath->put_Class (nameVar.bstrVal);
  426. *ppObjectPath = pObjectPath;
  427. }
  428. else
  429. pObjectPath->Release ();
  430. VariantClear (&nameVar);
  431. }
  432. else
  433. {
  434. // Now get the relpath string from the call result
  435. BSTR resultString = NULL;
  436. if (WBEM_S_NO_ERROR == pResult->GetResultString (INFINITE, &resultString))
  437. {
  438. pObjectPath->put_RelPath (resultString);
  439. *ppObjectPath = pObjectPath;
  440. SysFreeString (resultString);
  441. }
  442. else
  443. pObjectPath->Release ();
  444. }
  445. }
  446. }
  447. }
  448. if (pSecurity)
  449. pSecurity->Release ();
  450. }
  451. else
  452. hr = hrCallResult;
  453. if (pResult)
  454. pResult->Release ();
  455. SetWbemError (m_pSWbemServices);
  456. if (pIContext)
  457. pIContext->Release ();
  458. }
  459. VariantClear (&var);
  460. }
  461. }
  462. if (FAILED(hr) && m_pDispatch)
  463. m_pDispatch->RaiseException (hr);
  464. return hr;
  465. }
  466. //***************************************************************************
  467. //
  468. // SCODE CSWbemObject::Delete_
  469. //
  470. // DESCRIPTION:
  471. //
  472. // Delete this class or instance from the namespace
  473. //
  474. // PARAMETERS:
  475. //
  476. // lFlags Flags
  477. // pContext Context
  478. //
  479. // RETURN VALUES:
  480. //
  481. // WBEM_S_NO_ERROR success
  482. // WBEM_E_INVALID_PARAMETER bad input parameters
  483. // WBEM_E_FAILED otherwise
  484. //
  485. //***************************************************************************
  486. HRESULT CSWbemObject::Delete_ (
  487. long lFlags,
  488. /*ISWbemNamedValueSet*/ IDispatch *pContext
  489. )
  490. {
  491. HRESULT hr = WBEM_E_FAILED;
  492. ResetLastErrors ();
  493. if (m_pSWbemServices && m_pIWbemClassObject)
  494. {
  495. // Get the object path to pass to the IWbemServices call
  496. CComBSTR bsPath;
  497. if (CSWbemObjectPath::GetObjectPath (m_pIWbemClassObject, bsPath))
  498. hr = m_pSWbemServices->Delete (bsPath, lFlags, pContext);
  499. }
  500. if (FAILED(hr) && m_pDispatch)
  501. m_pDispatch->RaiseException (hr);
  502. return hr;
  503. }
  504. //***************************************************************************
  505. //
  506. // SCODE CSWbemObject::Instances_
  507. //
  508. // DESCRIPTION:
  509. //
  510. // returns instances of this class
  511. //
  512. // PARAMETERS:
  513. //
  514. // lFlags Flags
  515. // pContext Context
  516. // ppEnum Returned enumerator
  517. //
  518. //
  519. // RETURN VALUES:
  520. //
  521. // WBEM_S_NO_ERROR success
  522. // WBEM_E_INVALID_PARAMETER bad input parameters
  523. // WBEM_E_FAILED otherwise
  524. //
  525. //***************************************************************************
  526. HRESULT CSWbemObject::Instances_ (
  527. long lFlags,
  528. /*ISWbemNamedValueSet*/ IDispatch *pContext,
  529. /*[out]*/ ISWbemObjectSet **ppEnum
  530. )
  531. {
  532. HRESULT hr = WBEM_E_FAILED;
  533. ResetLastErrors ();
  534. if (m_pSWbemServices && m_pIWbemClassObject)
  535. {
  536. // Get the object path to pass to the IWbemServices call
  537. CComBSTR bsPath;
  538. if (CSWbemObjectPath::GetObjectPath (m_pIWbemClassObject, bsPath))
  539. hr = m_pSWbemServices->InstancesOf (bsPath, lFlags, pContext, ppEnum);
  540. }
  541. if (FAILED(hr) && m_pDispatch)
  542. m_pDispatch->RaiseException (hr);
  543. return hr;
  544. }
  545. //***************************************************************************
  546. //
  547. // SCODE CSWbemObject::Subclasses_
  548. //
  549. // DESCRIPTION:
  550. //
  551. // returns subclasses of this class
  552. //
  553. // PARAMETERS:
  554. //
  555. // lFlags Flags
  556. // pContext Context
  557. // ppEnum Returned enumerator
  558. //
  559. //
  560. // RETURN VALUES:
  561. //
  562. // WBEM_S_NO_ERROR success
  563. // WBEM_E_INVALID_PARAMETER bad input parameters
  564. // WBEM_E_FAILED otherwise
  565. //
  566. //***************************************************************************
  567. HRESULT CSWbemObject::Subclasses_ (
  568. long lFlags,
  569. /*ISWbemNamedValueSet*/ IDispatch *pContext,
  570. /*[out]*/ ISWbemObjectSet **ppEnum
  571. )
  572. {
  573. HRESULT hr = WBEM_E_FAILED;
  574. ResetLastErrors ();
  575. if (m_pSWbemServices && m_pIWbemClassObject)
  576. {
  577. // Get the object path to pass to the IWbemServices call
  578. CComBSTR bsPath;
  579. if (CSWbemObjectPath::GetObjectPath (m_pIWbemClassObject, bsPath))
  580. hr = m_pSWbemServices->SubclassesOf (bsPath, lFlags, pContext, ppEnum);
  581. }
  582. if (FAILED(hr) && m_pDispatch)
  583. m_pDispatch->RaiseException (hr);
  584. return hr;
  585. }
  586. //***************************************************************************
  587. //
  588. // SCODE CSWbemObject::Associators_
  589. //
  590. // DESCRIPTION:
  591. //
  592. // returns associators of this object
  593. //
  594. // PARAMETERS:
  595. //
  596. // lFlags Flags
  597. // pContext Context
  598. // ppEnum Returned enumerator
  599. //
  600. //
  601. // RETURN VALUES:
  602. //
  603. // WBEM_S_NO_ERROR success
  604. // WBEM_E_INVALID_PARAMETER bad input parameters
  605. // WBEM_E_FAILED otherwise
  606. //
  607. //***************************************************************************
  608. HRESULT CSWbemObject::Associators_ (
  609. BSTR assocClass,
  610. BSTR resultClass,
  611. BSTR resultRole,
  612. BSTR role,
  613. VARIANT_BOOL classesOnly,
  614. VARIANT_BOOL schemaOnly,
  615. BSTR requiredAssocQualifier,
  616. BSTR requiredQualifier,
  617. long lFlags,
  618. /*ISWbemNamedValueSet*/ IDispatch *pContext,
  619. ISWbemObjectSet **ppEnum
  620. )
  621. {
  622. HRESULT hr = WBEM_E_FAILED;
  623. ResetLastErrors ();
  624. if (m_pSWbemServices && m_pIWbemClassObject)
  625. {
  626. // Get the object path to pass to the IWbemServices call
  627. CComBSTR bsPath;
  628. if (CSWbemObjectPath::GetObjectPath (m_pIWbemClassObject, bsPath))
  629. hr = m_pSWbemServices->AssociatorsOf (bsPath, assocClass, resultClass,
  630. resultRole, role, classesOnly, schemaOnly,
  631. requiredAssocQualifier, requiredQualifier, lFlags,
  632. pContext, ppEnum);
  633. }
  634. if (FAILED(hr) && m_pDispatch)
  635. m_pDispatch->RaiseException (hr);
  636. return hr;
  637. }
  638. //***************************************************************************
  639. //
  640. // SCODE CSWbemObject::References_
  641. //
  642. // DESCRIPTION:
  643. //
  644. // returns references to this object
  645. //
  646. // PARAMETERS:
  647. //
  648. // lFlags Flags
  649. // pContext Context
  650. // ppEnum Returned enumerator
  651. //
  652. //
  653. // RETURN VALUES:
  654. //
  655. // WBEM_S_NO_ERROR success
  656. // WBEM_E_INVALID_PARAMETER bad input parameters
  657. // WBEM_E_FAILED otherwise
  658. //
  659. //***************************************************************************
  660. HRESULT CSWbemObject::References_ (
  661. BSTR resultClass,
  662. BSTR role,
  663. VARIANT_BOOL classesOnly,
  664. VARIANT_BOOL schemaOnly,
  665. BSTR requiredQualifier,
  666. long lFlags,
  667. /*ISWbemNamedValueSet*/ IDispatch *pContext,
  668. ISWbemObjectSet **ppEnum
  669. )
  670. {
  671. HRESULT hr = WBEM_E_FAILED;
  672. ResetLastErrors ();
  673. if (m_pSWbemServices && m_pIWbemClassObject)
  674. {
  675. CComBSTR bsPath;
  676. if (CSWbemObjectPath::GetObjectPath (m_pIWbemClassObject, bsPath))
  677. hr = m_pSWbemServices->ReferencesTo (bsPath, resultClass,
  678. role, classesOnly, schemaOnly,
  679. requiredQualifier, lFlags, pContext, ppEnum);
  680. }
  681. if (FAILED(hr) && m_pDispatch)
  682. m_pDispatch->RaiseException (hr);
  683. return hr;
  684. }
  685. //***************************************************************************
  686. //
  687. // SCODE CSWbemObject::ExecMethod_
  688. //
  689. // DESCRIPTION:
  690. //
  691. // Executes a method of this class (or instance)
  692. //
  693. // PARAMETERS:
  694. //
  695. // bsMethod The name of the method to call
  696. // pInParams The in-parameters
  697. // lFlags Flags
  698. // pContext Any context information
  699. // ppOutParams The out-parameters
  700. //
  701. // RETURN VALUES:
  702. //
  703. // WBEM_S_NO_ERROR success
  704. // WBEM_E_INVALID_PARAMETER bad input parameters
  705. // WBEM_E_FAILED otherwise
  706. //
  707. //***************************************************************************
  708. HRESULT CSWbemObject::ExecMethod_ (
  709. BSTR bsMethod,
  710. /*ISWbemObject*/ IDispatch *pInParams,
  711. long lFlags,
  712. /*ISWbemValueBag*/ IDispatch *pContext,
  713. ISWbemObject **ppOutParams
  714. )
  715. {
  716. HRESULT hr = WBEM_E_FAILED;
  717. ResetLastErrors ();
  718. if (m_pSWbemServices && m_pIWbemClassObject)
  719. {
  720. // Get the object path to pass to the IWbemServices call
  721. CComBSTR bsPath;
  722. if (CSWbemObjectPath::GetObjectPath (m_pIWbemClassObject, bsPath))
  723. hr = m_pSWbemServices->ExecMethod (bsPath, bsMethod,
  724. pInParams, lFlags, pContext, ppOutParams);
  725. }
  726. if (FAILED(hr) && m_pDispatch)
  727. m_pDispatch->RaiseException (hr);
  728. return hr;
  729. }
  730. //***************************************************************************
  731. //
  732. // SCODE CSWbemObject::Clone_
  733. //
  734. // DESCRIPTION:
  735. //
  736. // Clone object
  737. //
  738. // PARAMETERS:
  739. // ppCopy On successful return addresses the copy
  740. //
  741. // RETURN VALUES:
  742. //
  743. // WBEM_S_NO_ERROR success
  744. // WBEM_E_INVALID_PARAMETER bad input parameters
  745. // WBEM_E_FAILED otherwise
  746. //
  747. //***************************************************************************
  748. HRESULT CSWbemObject::Clone_ (
  749. ISWbemObject **ppCopy
  750. )
  751. {
  752. HRESULT hr = WBEM_E_FAILED;
  753. ResetLastErrors ();
  754. if (NULL == ppCopy)
  755. return WBEM_E_INVALID_PARAMETER;
  756. if (m_pIWbemClassObject)
  757. {
  758. IWbemClassObject *pWObject = NULL;
  759. if (WBEM_S_NO_ERROR == (hr = m_pIWbemClassObject->Clone (&pWObject)))
  760. {
  761. CSWbemObject *pObject = new CSWbemObject (m_pSWbemServices, pWObject);
  762. if (!pObject)
  763. hr = WBEM_E_OUT_OF_MEMORY;
  764. else
  765. {
  766. if (FAILED(hr = pObject->QueryInterface (IID_ISWbemObject,
  767. (PPVOID) ppCopy)))
  768. delete pObject;
  769. }
  770. pWObject->Release ();
  771. }
  772. }
  773. if (FAILED(hr) && m_pDispatch)
  774. m_pDispatch->RaiseException (hr);
  775. return hr;
  776. }
  777. //***************************************************************************
  778. //
  779. // SCODE CSWbemObject::GetObjectText_
  780. //
  781. // DESCRIPTION:
  782. //
  783. // Get MOF Description of Object
  784. //
  785. // PARAMETERS:
  786. // lFlags flags
  787. // pObjectText on successful return holds MOF text
  788. //
  789. // RETURN VALUES:
  790. //
  791. // WBEM_S_NO_ERROR success
  792. // WBEM_E_INVALID_PARAMETER bad input parameters
  793. // WBEM_E_FAILED otherwise
  794. //
  795. //***************************************************************************
  796. HRESULT CSWbemObject::GetObjectText_ (
  797. long lFlags,
  798. BSTR *pObjectText
  799. )
  800. {
  801. HRESULT hr = WBEM_E_FAILED;
  802. ResetLastErrors ();
  803. if (m_pIWbemClassObject)
  804. hr = m_pIWbemClassObject->GetObjectText (lFlags, pObjectText);
  805. if (FAILED(hr) && m_pDispatch)
  806. m_pDispatch->RaiseException (hr);
  807. return hr;
  808. }
  809. //***************************************************************************
  810. //
  811. // SCODE CSWbemObject::SpawnDerivedClass_
  812. //
  813. // DESCRIPTION:
  814. //
  815. // Create a subclass of this (class) object
  816. //
  817. // PARAMETERS:
  818. // lFlags Flags
  819. // ppNewClass On successful return addresses the subclass
  820. //
  821. // RETURN VALUES:
  822. //
  823. // WBEM_S_NO_ERROR success
  824. // WBEM_E_INVALID_PARAMETER bad input parameters
  825. // WBEM_E_FAILED otherwise
  826. //
  827. //***************************************************************************
  828. HRESULT CSWbemObject::SpawnDerivedClass_ (
  829. long lFlags,
  830. ISWbemObject **ppNewClass
  831. )
  832. {
  833. HRESULT hr = WBEM_E_FAILED;
  834. ResetLastErrors ();
  835. if (NULL == ppNewClass)
  836. hr = WBEM_E_INVALID_PARAMETER;
  837. else if (m_pIWbemClassObject)
  838. {
  839. IWbemClassObject *pWObject = NULL;
  840. if (WBEM_S_NO_ERROR == (hr = m_pIWbemClassObject->SpawnDerivedClass (lFlags, &pWObject)))
  841. {
  842. CSWbemObject *pObject = new CSWbemObject (m_pSWbemServices, pWObject);
  843. if (!pObject)
  844. hr = WBEM_E_OUT_OF_MEMORY;
  845. else if (FAILED(hr = pObject->QueryInterface (IID_ISWbemObject,
  846. (PPVOID) ppNewClass)))
  847. delete pObject;
  848. pWObject->Release ();
  849. }
  850. }
  851. if (FAILED(hr) && m_pDispatch)
  852. m_pDispatch->RaiseException (hr);
  853. return hr;
  854. }
  855. //***************************************************************************
  856. //
  857. // SCODE CSWbemObject::SpawnInstance_
  858. //
  859. // DESCRIPTION:
  860. //
  861. // Create an instance of this (class) object
  862. //
  863. // PARAMETERS:
  864. // lFlags Flags
  865. // ppNewInstance On successful return addresses the instance
  866. //
  867. // RETURN VALUES:
  868. //
  869. // WBEM_S_NO_ERROR success
  870. // WBEM_E_INVALID_PARAMETER bad input parameters
  871. // WBEM_E_FAILED otherwise
  872. //
  873. //***************************************************************************
  874. HRESULT CSWbemObject::SpawnInstance_ (
  875. long lFlags,
  876. ISWbemObject **ppNewInstance
  877. )
  878. {
  879. HRESULT hr = WBEM_E_FAILED;
  880. ResetLastErrors ();
  881. if (NULL == ppNewInstance)
  882. hr = WBEM_E_INVALID_PARAMETER;
  883. else if (m_pIWbemClassObject)
  884. {
  885. IWbemClassObject *pWObject = NULL;
  886. if (WBEM_S_NO_ERROR == (hr = m_pIWbemClassObject->SpawnInstance (lFlags, &pWObject)))
  887. {
  888. CSWbemObject *pObject = new CSWbemObject (m_pSWbemServices, pWObject);
  889. if (!pObject)
  890. hr = WBEM_E_OUT_OF_MEMORY;
  891. else if (FAILED(hr = pObject->QueryInterface (IID_ISWbemObject,
  892. (PPVOID) ppNewInstance)))
  893. delete pObject;
  894. pWObject->Release ();
  895. }
  896. }
  897. if (FAILED(hr) && m_pDispatch)
  898. m_pDispatch->RaiseException (hr);
  899. return hr;
  900. }
  901. //***************************************************************************
  902. //
  903. // SCODE CSWbemObject::CompareTo_
  904. //
  905. // DESCRIPTION:
  906. //
  907. // Compare this object against another
  908. //
  909. // PARAMETERS:
  910. // pCompareTo The object to compare this against
  911. // lFlags Flags
  912. // pResult On return contains the match status (TRUE/FALSE)
  913. //
  914. // RETURN VALUES:
  915. //
  916. // WBEM_S_NO_ERROR success
  917. // WBEM_E_INVALID_PARAMETER bad input parameters
  918. // WBEM_E_FAILED otherwise
  919. //
  920. //***************************************************************************
  921. HRESULT CSWbemObject::CompareTo_ (
  922. /*ISWbemObject*/ IDispatch *pCompareTo,
  923. long lFlags,
  924. VARIANT_BOOL *pResult
  925. )
  926. {
  927. HRESULT hr = WBEM_E_FAILED;
  928. ResetLastErrors ();
  929. if ((NULL == pCompareTo) || (NULL == pResult))
  930. hr = WBEM_E_INVALID_PARAMETER;
  931. else if (m_pIWbemClassObject)
  932. {
  933. IWbemClassObject *pObject = CSWbemObject::GetIWbemClassObject (pCompareTo);
  934. if (NULL != pObject)
  935. {
  936. if (SUCCEEDED (hr = m_pIWbemClassObject->CompareTo (lFlags, pObject)))
  937. *pResult = (WBEM_S_SAME == hr) ? VARIANT_TRUE : VARIANT_FALSE;
  938. pObject->Release ();
  939. }
  940. }
  941. if (FAILED(hr) && m_pDispatch)
  942. m_pDispatch->RaiseException (hr);
  943. return hr;
  944. }
  945. //***************************************************************************
  946. //
  947. // SCODE CSWbemObject::get_Qualifiers_
  948. //
  949. // DESCRIPTION:
  950. //
  951. // retrieve the qualifier set for this object
  952. //
  953. // PARAMETERS:
  954. //
  955. // ppQualSet holds the value on return
  956. //
  957. // RETURN VALUES:
  958. //
  959. // WBEM_S_NO_ERROR success
  960. // WBEM_E_INVALID_PARAMETER bad input parameters
  961. // WBEM_E_FAILED otherwise
  962. //
  963. //***************************************************************************
  964. HRESULT CSWbemObject::get_Qualifiers_ (
  965. ISWbemQualifierSet **ppQualSet
  966. )
  967. {
  968. HRESULT hr = WBEM_E_FAILED;
  969. ResetLastErrors ();
  970. if (NULL == ppQualSet)
  971. hr = WBEM_E_INVALID_PARAMETER;
  972. else
  973. {
  974. *ppQualSet = NULL;
  975. if (m_pIWbemClassObject)
  976. {
  977. IWbemQualifierSet *pQualSet = NULL;
  978. if (WBEM_S_NO_ERROR == (hr = m_pIWbemClassObject->GetQualifierSet (&pQualSet)))
  979. {
  980. if (!(*ppQualSet = new CSWbemQualifierSet (pQualSet, this)))
  981. hr = WBEM_E_OUT_OF_MEMORY;
  982. pQualSet->Release ();
  983. }
  984. }
  985. }
  986. if (FAILED(hr) && m_pDispatch)
  987. m_pDispatch->RaiseException (hr);
  988. return hr;
  989. }
  990. //***************************************************************************
  991. //
  992. // SCODE CSWbemObject::get_Properties_
  993. //
  994. // DESCRIPTION:
  995. //
  996. // retrieve the property set for this object
  997. //
  998. // PARAMETERS:
  999. //
  1000. // ppPropSet holds the value on return
  1001. //
  1002. // RETURN VALUES:
  1003. //
  1004. // WBEM_S_NO_ERROR success
  1005. // WBEM_E_INVALID_PARAMETER bad input parameters
  1006. // WBEM_E_FAILED otherwise
  1007. //
  1008. //***************************************************************************
  1009. HRESULT CSWbemObject::get_Properties_ (
  1010. ISWbemPropertySet **ppPropSet
  1011. )
  1012. {
  1013. HRESULT hr = WBEM_E_FAILED;
  1014. ResetLastErrors ();
  1015. if (NULL == ppPropSet)
  1016. hr = WBEM_E_INVALID_PARAMETER;
  1017. {
  1018. *ppPropSet = NULL;
  1019. if (m_pIWbemClassObject)
  1020. {
  1021. if (!(*ppPropSet = new CSWbemPropertySet (m_pSWbemServices, this)))
  1022. hr = WBEM_E_OUT_OF_MEMORY;
  1023. else
  1024. hr = WBEM_S_NO_ERROR;
  1025. }
  1026. }
  1027. if (FAILED(hr) && m_pDispatch)
  1028. m_pDispatch->RaiseException (hr);
  1029. return hr;
  1030. }
  1031. //***************************************************************************
  1032. //
  1033. // SCODE CSWbemObject::get_SystemProperties_
  1034. //
  1035. // DESCRIPTION:
  1036. //
  1037. // retrieve the system property set for this object
  1038. //
  1039. // PARAMETERS:
  1040. //
  1041. // ppPropSet holds the value on return
  1042. //
  1043. // RETURN VALUES:
  1044. //
  1045. // WBEM_S_NO_ERROR success
  1046. // WBEM_E_INVALID_PARAMETER bad input parameters
  1047. // WBEM_E_FAILED otherwise
  1048. //
  1049. //***************************************************************************
  1050. HRESULT CSWbemObject::get_SystemProperties_ (
  1051. ISWbemPropertySet **ppPropSet
  1052. )
  1053. {
  1054. HRESULT hr = WBEM_E_FAILED;
  1055. ResetLastErrors ();
  1056. if (NULL == ppPropSet)
  1057. hr = WBEM_E_INVALID_PARAMETER;
  1058. {
  1059. *ppPropSet = NULL;
  1060. if (m_pIWbemClassObject)
  1061. {
  1062. if (!(*ppPropSet = new CSWbemPropertySet (m_pSWbemServices, this, true)))
  1063. hr = WBEM_E_OUT_OF_MEMORY;
  1064. else
  1065. hr = WBEM_S_NO_ERROR;
  1066. }
  1067. }
  1068. if (FAILED(hr) && m_pDispatch)
  1069. m_pDispatch->RaiseException (hr);
  1070. return hr;
  1071. }
  1072. //***************************************************************************
  1073. //
  1074. // SCODE CSWbemObject::get_Methods_
  1075. //
  1076. // DESCRIPTION:
  1077. //
  1078. // retrieve the method set for this object
  1079. //
  1080. // PARAMETERS:
  1081. //
  1082. // ppMethodSet holds the value on return
  1083. //
  1084. // RETURN VALUES:
  1085. //
  1086. // WBEM_S_NO_ERROR success
  1087. // WBEM_E_INVALID_PARAMETER bad input parameters
  1088. // WBEM_E_FAILED otherwise
  1089. //
  1090. //***************************************************************************
  1091. HRESULT CSWbemObject::get_Methods_ (
  1092. ISWbemMethodSet **ppMethodSet
  1093. )
  1094. {
  1095. HRESULT hr = WBEM_E_FAILED;
  1096. ResetLastErrors ();
  1097. if (NULL == ppMethodSet)
  1098. hr = WBEM_E_INVALID_PARAMETER;
  1099. else
  1100. {
  1101. *ppMethodSet = NULL;
  1102. if (m_pIWbemClassObject)
  1103. {
  1104. /*
  1105. * For classes the IWbemClassObject will contain the method
  1106. * definition, but for instances it will be empty. In that
  1107. * case we need to try and get the underlying class.
  1108. */
  1109. VARIANT var;
  1110. VariantInit (&var);
  1111. if (WBEM_S_NO_ERROR == m_pIWbemClassObject->Get (WBEMS_SP_GENUS, 0, &var, NULL, NULL))
  1112. {
  1113. if (WBEM_GENUS_CLASS == var.lVal)
  1114. {
  1115. if (!(*ppMethodSet = new CSWbemMethodSet (m_pSWbemServices, m_pIWbemClassObject)))
  1116. hr = WBEM_E_OUT_OF_MEMORY;
  1117. else
  1118. hr = WBEM_S_NO_ERROR;
  1119. }
  1120. else
  1121. {
  1122. if (m_pSWbemServices)
  1123. {
  1124. // An instance; try to get the class
  1125. VariantClear (&var);
  1126. /*
  1127. * Note we must check that returned value is a BSTR - it could be a VT_NULL if
  1128. * the __CLASS property has not yet been set.
  1129. */
  1130. if ((WBEM_S_NO_ERROR == m_pIWbemClassObject->Get (WBEMS_SP_CLASS, 0, &var, NULL, NULL))
  1131. && (VT_BSTR == V_VT(&var)))
  1132. {
  1133. IWbemServices *pIService = m_pSWbemServices->GetIWbemServices ();
  1134. IWbemClassObject *pObject = NULL;
  1135. if (pIService)
  1136. {
  1137. // Check privileges are set ok
  1138. CSWbemSecurity *pSecurity = m_pSWbemServices->GetSecurityInfo ();
  1139. if (pSecurity)
  1140. {
  1141. bool needToResetSecurity = false;
  1142. HANDLE hThreadToken = NULL;
  1143. if (pSecurity->SetSecurity (needToResetSecurity, hThreadToken))
  1144. hr = pIService->GetObject (var.bstrVal,
  1145. 0, NULL, &pObject, NULL);
  1146. if (SUCCEEDED(hr))
  1147. {
  1148. if (!(*ppMethodSet =
  1149. new CSWbemMethodSet (m_pSWbemServices, pObject)))
  1150. hr = WBEM_E_OUT_OF_MEMORY;
  1151. pObject->Release ();
  1152. }
  1153. if (needToResetSecurity)
  1154. pSecurity->ResetSecurity (hThreadToken);
  1155. pSecurity->Release ();
  1156. }
  1157. pIService->Release ();
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }
  1163. VariantClear (&var);
  1164. }
  1165. }
  1166. if (FAILED(hr) && m_pDispatch)
  1167. m_pDispatch->RaiseException (hr);
  1168. return hr;
  1169. }
  1170. //***************************************************************************
  1171. //
  1172. // SCODE CSWbemObject::get_Path_
  1173. //
  1174. // DESCRIPTION:
  1175. //
  1176. // retrieve the object path for this object
  1177. //
  1178. // PARAMETERS:
  1179. //
  1180. // ppObjectPath holds the value on return
  1181. //
  1182. // RETURN VALUES:
  1183. //
  1184. // WBEM_S_NO_ERROR success
  1185. // WBEM_E_INVALID_PARAMETER bad input parameters
  1186. // WBEM_E_FAILED otherwise
  1187. //
  1188. //***************************************************************************
  1189. HRESULT CSWbemObject::get_Path_ (
  1190. ISWbemObjectPath **ppObjectPath
  1191. )
  1192. {
  1193. HRESULT hr = WBEM_E_FAILED;
  1194. ResetLastErrors ();
  1195. if (NULL == ppObjectPath)
  1196. hr = WBEM_E_INVALID_PARAMETER;
  1197. else
  1198. {
  1199. *ppObjectPath = NULL;
  1200. if (m_pIWbemClassObject)
  1201. {
  1202. CSWbemObjectObjectPath *pObjectPath =
  1203. new CSWbemObjectObjectPath (m_pSWbemServices, this);
  1204. if (!pObjectPath)
  1205. hr = WBEM_E_OUT_OF_MEMORY;
  1206. else if (FAILED(hr = pObjectPath->QueryInterface (IID_ISWbemObjectPath,
  1207. (PPVOID) ppObjectPath)))
  1208. delete pObjectPath;
  1209. }
  1210. }
  1211. if (FAILED(hr) && m_pDispatch)
  1212. m_pDispatch->RaiseException (hr);
  1213. return hr;
  1214. }
  1215. //***************************************************************************
  1216. //
  1217. // SCODE CSWbemObject::get_Derivation_
  1218. //
  1219. // DESCRIPTION:
  1220. //
  1221. // Get the class derivation array.
  1222. //
  1223. // PARAMETERS:
  1224. //
  1225. // ppNames Holds the names on successful return
  1226. //
  1227. // RETURN VALUES:
  1228. //
  1229. // WBEM_S_NO_ERROR success
  1230. // WBEM_E_INVALID_PARAMETER bad input parameters
  1231. // WBEM_E_FAILED otherwise
  1232. //
  1233. //***************************************************************************
  1234. HRESULT CSWbemObject::get_Derivation_ (
  1235. VARIANT *pNames
  1236. )
  1237. {
  1238. HRESULT hr = WBEM_E_FAILED;
  1239. ResetLastErrors ();
  1240. if (NULL == pNames)
  1241. hr = WBEM_E_INVALID_PARAMETER;
  1242. else
  1243. {
  1244. if (m_pIWbemClassObject)
  1245. {
  1246. VARIANT var;
  1247. VariantInit (&var);
  1248. if (WBEM_S_NO_ERROR == m_pIWbemClassObject->Get (WBEMS_SP_DERIVATION, 0, &var, NULL, NULL))
  1249. {
  1250. /* The value should be a VT_BSTR|VT_ARRAY */
  1251. if (((VT_ARRAY | VT_BSTR) == var.vt) && (NULL != var.parray))
  1252. {
  1253. // Make a safearray of VARIANTS from the array of BSTRs
  1254. SAFEARRAYBOUND rgsabound;
  1255. rgsabound.lLbound = 0;
  1256. long lBound = 0, uBound = 0;
  1257. SafeArrayGetUBound (var.parray, 1, &uBound);
  1258. SafeArrayGetLBound (var.parray, 1, &lBound);
  1259. rgsabound.cElements = uBound + 1 - lBound;
  1260. SAFEARRAY *pArray = SafeArrayCreate (VT_VARIANT, 1, &rgsabound);
  1261. BSTR bstrName = NULL;
  1262. VARIANT nameVar;
  1263. VariantInit (&nameVar);
  1264. for (long i = 0; i <= uBound; i++)
  1265. {
  1266. SafeArrayGetElement (var.parray, &i, &bstrName);
  1267. BSTR copy = SysAllocString (bstrName);
  1268. nameVar.vt = VT_BSTR;
  1269. nameVar.bstrVal = copy;
  1270. SafeArrayPutElement (pArray, &i, &nameVar);
  1271. SysFreeString (bstrName);
  1272. VariantClear (&nameVar);
  1273. }
  1274. // Now plug this array into the VARIANT
  1275. pNames->vt = VT_ARRAY | VT_VARIANT;
  1276. pNames->parray = pArray;
  1277. hr = S_OK;
  1278. }
  1279. }
  1280. VariantClear (&var);
  1281. }
  1282. }
  1283. if (FAILED(hr) && m_pDispatch)
  1284. m_pDispatch->RaiseException (hr);
  1285. return hr;
  1286. }
  1287. //***************************************************************************
  1288. //
  1289. // SCODE CSWbemObject::get_Security_
  1290. //
  1291. // DESCRIPTION:
  1292. //
  1293. // Return the security configurator
  1294. //
  1295. // WBEM_S_NO_ERROR success
  1296. // WBEM_E_INVALID_PARAMETER bad input parameters
  1297. // WBEM_E_FAILED otherwise
  1298. //
  1299. //***************************************************************************
  1300. HRESULT CSWbemObject::get_Security_ (
  1301. ISWbemSecurity **ppSecurity
  1302. )
  1303. {
  1304. HRESULT hr = WBEM_E_FAILED;
  1305. ResetLastErrors ();
  1306. if (NULL == ppSecurity)
  1307. hr = WBEM_E_INVALID_PARAMETER;
  1308. {
  1309. *ppSecurity = NULL;
  1310. if (m_pSWbemServices)
  1311. {
  1312. *ppSecurity = m_pSWbemServices->GetSecurityInfo ();
  1313. if (*ppSecurity)
  1314. hr = WBEM_S_NO_ERROR;
  1315. }
  1316. }
  1317. if (FAILED(hr) && m_pDispatch)
  1318. m_pDispatch->RaiseException (hr);
  1319. return hr;
  1320. }
  1321. //***************************************************************************
  1322. //
  1323. // SCODE CSWbemObject::Refresh_
  1324. //
  1325. // DESCRIPTION:
  1326. //
  1327. // Refresh the current object
  1328. //
  1329. // PARAMETERS:
  1330. // lFlags Flags
  1331. // pContext Operation context
  1332. //
  1333. // RETURN VALUES:
  1334. //
  1335. // WBEM_S_NO_ERROR success
  1336. // WBEM_E_INVALID_PARAMETER bad input parameters
  1337. // WBEM_E_FAILED otherwise
  1338. //
  1339. //***************************************************************************
  1340. HRESULT CSWbemObject::Refresh_ (
  1341. long iFlags,
  1342. /*ISWbemNamedValueSet*/ IDispatch *pContext
  1343. )
  1344. {
  1345. HRESULT hr = WBEM_E_FAILED;
  1346. ResetLastErrors ();
  1347. if (m_pSWbemServices)
  1348. {
  1349. if (m_pIWbemClassObject)
  1350. {
  1351. CComPtr<IWbemContext> pIContext;
  1352. //Can't assign directly because the raw pointer gets AddRef'd twice and we leak,
  1353. //So we use Attach() instead to prevent the smart pointer from AddRef'ing.
  1354. pIContext.Attach(CSWbemNamedValueSet::GetIWbemContext (pContext, m_pIServiceProvider));
  1355. // Order of preference:
  1356. // 1. IWbemConfigureRefresher::AddObjectByTemplate
  1357. // 2. IWbemServices::GetObject
  1358. CComPtr<IWbemServices> pIWbemServices = m_pSWbemServices->GetIWbemServices ();
  1359. if (pIWbemServices)
  1360. {
  1361. bool bUseRefresher = false;
  1362. bool bOperationFailed = false;
  1363. // Is this a class or an instance?
  1364. bool bIsClass = false;
  1365. CComVariant var;
  1366. if (WBEM_S_NO_ERROR == m_pIWbemClassObject->Get (WBEMS_SP_GENUS, 0, &var, NULL, NULL))
  1367. bIsClass = (WBEM_GENUS_CLASS == var.lVal);
  1368. /*
  1369. * IWbemConfigureRefresher cannot handle per-refresh context; if the caller
  1370. * gave us some context we'll have to drop down to loperf retrieval.
  1371. *
  1372. * Similarly the refresher cannot handle classes.
  1373. */
  1374. if (bIsClass || (!pIContext))
  1375. {
  1376. if (m_bCanUseRefresher)
  1377. {
  1378. // If we don't have one get ourselves a refresher
  1379. if (NULL == m_pIWbemRefresher)
  1380. {
  1381. m_bCanUseRefresher = false; // Until proven otherwise
  1382. if (SUCCEEDED(CoCreateInstance( CLSID_WbemRefresher, NULL, CLSCTX_INPROC_SERVER,
  1383. IID_IWbemRefresher, (void**) &m_pIWbemRefresher )))
  1384. {
  1385. IWbemConfigureRefresher *pConfigureRefresher = NULL;
  1386. // Get ourselves a refresher configurator
  1387. if (SUCCEEDED(m_pIWbemRefresher->QueryInterface (IID_IWbemConfigureRefresher,
  1388. (void**) &pConfigureRefresher)))
  1389. {
  1390. CComPtr<IWbemClassObject> pNewObject;
  1391. long lID = 0;
  1392. // Add our object into it; we mask out all flag bits other
  1393. // than WBEM_FLAG_USE_AMENDED_QUALIFIERS.
  1394. HRESULT hrRef = pConfigureRefresher->AddObjectByTemplate
  1395. (pIWbemServices, m_pIWbemClassObject,
  1396. iFlags & WBEM_FLAG_USE_AMENDED_QUALIFIERS,
  1397. pIContext, &pNewObject, &lID);
  1398. if (SUCCEEDED (hrRef))
  1399. {
  1400. m_bCanUseRefresher = true; // Now we can use it
  1401. // Swap in our refreshable object
  1402. SetIWbemClassObject (pNewObject);
  1403. }
  1404. else if ((WBEM_E_INVALID_OPERATION != hrRef) &&
  1405. (WBEM_E_INVALID_PARAMETER != hrRef))
  1406. bOperationFailed = true; // A real refresh-independent failure
  1407. pConfigureRefresher->Release ();
  1408. }
  1409. // If we can't use the refresher, release it now
  1410. if (!m_bCanUseRefresher)
  1411. {
  1412. m_pIWbemRefresher->Release ();
  1413. m_pIWbemRefresher = NULL;
  1414. }
  1415. }
  1416. }
  1417. bUseRefresher = m_bCanUseRefresher;
  1418. }
  1419. }
  1420. // Having successfully set up a refresher/non-refresher scenario, let's go refresh
  1421. if (!bOperationFailed)
  1422. {
  1423. if (bUseRefresher && m_pIWbemRefresher)
  1424. {
  1425. // Mask out all flags other than WBEM_FLAG_REFRESH_NO_AUTO_RECONNECT
  1426. hr = m_pIWbemRefresher->Refresh (iFlags & WBEM_FLAG_REFRESH_NO_AUTO_RECONNECT);
  1427. }
  1428. else
  1429. {
  1430. // Bah - not even a refresher can we use. Just do a GetObject instead
  1431. CComBSTR bsPath;
  1432. if (CSWbemObjectPath::GetObjectPath (m_pIWbemClassObject, bsPath))
  1433. {
  1434. // Fall pack to the low-perf way of doing things
  1435. CComPtr<IWbemClassObject> pNewObject;
  1436. // Mask out the WBEM_FLAG_REFRESH_NO_AUTO_RECONNECT flag
  1437. if (SUCCEEDED(hr = pIWbemServices->GetObject (bsPath,
  1438. iFlags & ~WBEM_FLAG_REFRESH_NO_AUTO_RECONNECT,
  1439. pIContext, &pNewObject, NULL)))
  1440. {
  1441. // Swap in the new object
  1442. SetIWbemClassObject (pNewObject);
  1443. }
  1444. }
  1445. }
  1446. }
  1447. }
  1448. }
  1449. }
  1450. SetWbemError (m_pSWbemServices);
  1451. if (FAILED(hr) && m_pDispatch)
  1452. m_pDispatch->RaiseException (hr);
  1453. return hr;
  1454. }
  1455. //***************************************************************************
  1456. //
  1457. // SCODE CSWbemObject::GetText_
  1458. //
  1459. // DESCRIPTION:
  1460. //
  1461. // Get the object text
  1462. //
  1463. // PARAMETERS:
  1464. // iObjectTextFormat Text format
  1465. // pContext Context
  1466. // pbsText On return holds text
  1467. //
  1468. // RETURN VALUES:
  1469. //
  1470. // WBEM_S_NO_ERROR success
  1471. // WBEM_E_INVALID_PARAMETER bad input parameters
  1472. // WBEM_E_FAILED otherwise
  1473. //
  1474. //***************************************************************************
  1475. HRESULT CSWbemObject::GetText_ (
  1476. WbemObjectTextFormatEnum iObjectTextFormat,
  1477. long iFlags,
  1478. /*ISWbemNamedValueSet*/ IDispatch *pContext,
  1479. BSTR *pbsText
  1480. )
  1481. {
  1482. HRESULT hr = WBEM_E_FAILED;
  1483. ResetLastErrors ();
  1484. if (NULL == pbsText)
  1485. hr = WBEM_E_INVALID_PARAMETER;
  1486. else if (m_pIWbemClassObject)
  1487. {
  1488. *pbsText = NULL;
  1489. CComPtr<IWbemContext> pIContext;
  1490. //Can't assign directly because the raw pointer gets AddRef'd twice and we leak,
  1491. //So we use Attach() instead to prevent the smart pointer from AddRef'ing.
  1492. pIContext.Attach(CSWbemNamedValueSet::GetIWbemContext (pContext, m_pIServiceProvider));
  1493. CComPtr<IWbemObjectTextSrc> pIWbemObjectTextSrc;
  1494. if (SUCCEEDED(CoCreateInstance (CLSID_WbemObjectTextSrc, NULL, CLSCTX_INPROC_SERVER,
  1495. IID_IWbemObjectTextSrc, (PPVOID) &pIWbemObjectTextSrc)))
  1496. {
  1497. hr = pIWbemObjectTextSrc->GetText (iFlags, m_pIWbemClassObject, (ULONG) iObjectTextFormat,
  1498. pIContext, pbsText);
  1499. }
  1500. }
  1501. if (FAILED(hr) && m_pDispatch)
  1502. m_pDispatch->RaiseException (hr);
  1503. return hr;
  1504. }
  1505. //***************************************************************************
  1506. //
  1507. // SCODE CSWbemObject::SetFromText_
  1508. //
  1509. // DESCRIPTION:
  1510. //
  1511. // Set the object using the supplied text
  1512. //
  1513. // PARAMETERS:
  1514. // bsText The text
  1515. // iObjectTextFormat Text format
  1516. // pContext Context
  1517. //
  1518. // RETURN VALUES:
  1519. //
  1520. // WBEM_S_NO_ERROR success
  1521. // WBEM_E_INVALID_PARAMETER bad input parameters
  1522. // WBEM_E_FAILED otherwise
  1523. //
  1524. //***************************************************************************
  1525. HRESULT CSWbemObject::SetFromText_ (
  1526. BSTR bsText,
  1527. WbemObjectTextFormatEnum iObjectTextFormat,
  1528. long iFlags,
  1529. /*ISWbemNamedValueSet*/ IDispatch *pContext
  1530. )
  1531. {
  1532. HRESULT hr = WBEM_E_FAILED;
  1533. ResetLastErrors ();
  1534. if (NULL == bsText)
  1535. hr = WBEM_E_INVALID_PARAMETER;
  1536. else if (m_pIWbemClassObject)
  1537. {
  1538. CComPtr<IWbemContext> pIContext;
  1539. //Can't assign directly because the raw pointer gets AddRef'd twice and we leak,
  1540. //So we use Attach() instead to prevent the smart pointer from AddRef'ing.
  1541. pIContext.Attach(CSWbemNamedValueSet::GetIWbemContext (pContext, m_pIServiceProvider));
  1542. CComPtr<IWbemObjectTextSrc> pIWbemObjectTextSrc;
  1543. if (SUCCEEDED(CoCreateInstance (CLSID_WbemObjectTextSrc, NULL, CLSCTX_INPROC_SERVER,
  1544. IID_IWbemObjectTextSrc, (PPVOID) &pIWbemObjectTextSrc)))
  1545. {
  1546. CComPtr<IWbemClassObject> pIWbemClassObject;
  1547. if (SUCCEEDED(hr = pIWbemObjectTextSrc->CreateFromText (iFlags, bsText, (ULONG) iObjectTextFormat,
  1548. pIContext, &pIWbemClassObject)))
  1549. {
  1550. // Set the new object into our object
  1551. SetIWbemClassObject (pIWbemClassObject);
  1552. }
  1553. }
  1554. }
  1555. if (FAILED(hr) && m_pDispatch)
  1556. m_pDispatch->RaiseException (hr);
  1557. return hr;
  1558. }
  1559. //***************************************************************************
  1560. //
  1561. // CSWbemObject::GetIWbemClassObject
  1562. //
  1563. // DESCRIPTION:
  1564. //
  1565. // Given an IDispatch interface which we hope is also an ISWbemObjectEx
  1566. // interface, return the underlying IWbemClassObject interface.
  1567. //
  1568. // PARAMETERS:
  1569. // pDispatch the IDispatch in question
  1570. //
  1571. // RETURN VALUES:
  1572. // The underlying IWbemClassObject interface, or NULL.
  1573. //
  1574. // NOTES:
  1575. // If successful, the returned interface is AddRef'd; the caller is
  1576. // responsible for release.
  1577. //
  1578. //***************************************************************************
  1579. IWbemClassObject *CSWbemObject::GetIWbemClassObject (
  1580. IDispatch *pDispatch
  1581. )
  1582. {
  1583. IWbemClassObject *pObject = NULL;
  1584. ISWbemInternalObject *pIObject = NULL;
  1585. if (NULL != pDispatch)
  1586. {
  1587. if (SUCCEEDED (pDispatch->QueryInterface
  1588. (IID_ISWbemInternalObject, (PPVOID) &pIObject)))
  1589. {
  1590. pIObject->GetIWbemClassObject (&pObject);
  1591. pIObject->Release ();
  1592. }
  1593. }
  1594. return pObject;
  1595. }
  1596. //***************************************************************************
  1597. //
  1598. // CSWbemObject::UpdateSite
  1599. //
  1600. // DESCRIPTION:
  1601. //
  1602. // If this object represents an embedded CIM object property value, then
  1603. // as a result of changes to properties/qualifiers/path on this object it
  1604. // is necessary to update the object in its parent.
  1605. //
  1606. // This is to allow the following code to work:
  1607. //
  1608. // Object.EmbeddedProperty.SimpleProperty = 3
  1609. //
  1610. // i.e. so that the set to the value of SimpleProperty triggers an
  1611. // automatic set of EmbeddedProperty to Object.
  1612. //
  1613. // RETURN VALUES:
  1614. // The underlying IWbemClassObject interface, or NULL.
  1615. //
  1616. // NOTES:
  1617. // If successful, the returned interface is AddRef'd; the caller is
  1618. // responsible for release.
  1619. //
  1620. //***************************************************************************
  1621. STDMETHODIMP CSWbemObject::UpdateSite ()
  1622. {
  1623. // Update the site if it exists
  1624. if (m_pSite)
  1625. m_pSite->Update ();
  1626. return S_OK;
  1627. }
  1628. //***************************************************************************
  1629. //
  1630. // CSWbemObject::SetSite
  1631. //
  1632. // DESCRIPTION:
  1633. //
  1634. // Set the site of this object; this is used to anchor an embedded object
  1635. // to a property (possibly indexed, if that property is an array).
  1636. //
  1637. // PARAMETERS:
  1638. // pParentObject The parent of this object
  1639. // propertyName The property name for this object
  1640. // index The array index into the property (or -1)
  1641. //
  1642. // RETURN VALUES:
  1643. // S_OK success
  1644. // E_FAIL otherwise
  1645. //
  1646. // NOTES:
  1647. // If successful, the returned interface is AddRef'd; the caller is
  1648. // responsible for release.
  1649. //
  1650. //***************************************************************************
  1651. STDMETHODIMP CSWbemObject::SetSite (
  1652. ISWbemInternalObject *pParentObject,
  1653. BSTR propertyName,
  1654. long index
  1655. )
  1656. {
  1657. if (m_pSite)
  1658. {
  1659. m_pSite->Release ();
  1660. m_pSite = NULL;
  1661. }
  1662. CSWbemProperty *pSProperty = new CSWbemProperty (m_pSWbemServices,
  1663. pParentObject, propertyName);
  1664. m_pSite = new CWbemPropertySite (pSProperty, m_pIWbemClassObject, index);
  1665. if (pSProperty)
  1666. pSProperty->Release ();
  1667. return S_OK;
  1668. }
  1669. void CSWbemObject::SetSite (IDispatch *pDispatch,
  1670. ISWbemInternalObject *pSObject, BSTR propertyName, long index)
  1671. {
  1672. if (NULL != pDispatch)
  1673. {
  1674. ISWbemInternalObject *pObject = NULL;
  1675. if (SUCCEEDED (pDispatch->QueryInterface
  1676. (IID_ISWbemInternalObject, (PPVOID) &pObject)))
  1677. {
  1678. pObject->SetSite (pSObject, propertyName, index);
  1679. pObject->Release ();
  1680. }
  1681. }
  1682. }