Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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