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.

1255 lines
34 KiB

  1. /*++
  2. Copyright (C) 1995-2001 Microsoft Corporation
  3. Module Name:
  4. IMPDYN.CPP
  5. Abstract:
  6. Defines the virtual base class for the Dynamic Provider
  7. objects. The base class is overriden for each specific
  8. provider which provides the details of how an actual
  9. property "Put" or "Get" is done.
  10. History:
  11. a-davj 27-Sep-95 Created.
  12. --*/
  13. #include "precomp.h"
  14. #include <wbemidl.h>
  15. //#define _MT
  16. #include <process.h>
  17. #include "impdyn.h"
  18. #include "CVariant.h"
  19. #include <genlex.h>
  20. #include <objpath.h>
  21. #include <genutils.h>
  22. #include <cominit.h>
  23. //***************************************************************************
  24. //
  25. // CImpDyn::CImpDyn
  26. //
  27. // DESCRIPTION:
  28. //
  29. // Constructor.
  30. //
  31. // PARAMETERS:
  32. //
  33. // ObjectPath Full path to the namespace
  34. // User User name
  35. // Password Password
  36. //
  37. //***************************************************************************
  38. CImpDyn::CImpDyn() : m_pGateway(NULL), m_cRef(0)
  39. {
  40. wcCLSID[0] = 0; // Set to correct value in derived class constructors
  41. }
  42. HRESULT STDMETHODCALLTYPE CImpDyn::Initialize(LPWSTR wszUser, long lFlags,
  43. LPWSTR wszNamespace, LPWSTR wszLocale,
  44. IWbemServices* pNamespace, IWbemContext* pContext,
  45. IWbemProviderInitSink* pSink)
  46. {
  47. m_pGateway = pNamespace;
  48. m_pGateway->AddRef();
  49. pSink->SetStatus(WBEM_S_NO_ERROR, 0);
  50. return WBEM_S_NO_ERROR;
  51. }
  52. //***************************************************************************
  53. //
  54. // CImpDyn::~CImpDyn
  55. //
  56. // DESCRIPTION:
  57. //
  58. // Destructor.
  59. //
  60. //***************************************************************************
  61. CImpDyn::~CImpDyn(void)
  62. {
  63. if(m_pGateway)
  64. m_pGateway->Release();
  65. return;
  66. }
  67. //***************************************************************************
  68. //
  69. // SCODE CImpDyn::CreateInstanceEnum
  70. //
  71. // DESCRIPTION:
  72. //
  73. // Creates an enumerator object that can be used to enumerate
  74. // the instances of this class.
  75. //
  76. // PARAMETERS:
  77. //
  78. // Class Path specifying the class to be enumerated
  79. // lFlags flags to control the enumeration
  80. // phEnum returned pointer to enumerator
  81. // ppErrorObject returned pointer to error object
  82. //
  83. // RETURN VALUE:
  84. //
  85. // S_OK if all is well
  86. // WBEM_E_OUT_OF_MEMORY if not enough memory
  87. // various error codes from IWbemServices::GetObject
  88. //
  89. //***************************************************************************
  90. SCODE CImpDyn::CreateInstanceEnum(
  91. IN const BSTR Class,
  92. IN long lFlags,
  93. IWbemContext *pCtx,
  94. OUT IN IEnumWbemClassObject FAR* FAR* phEnum)
  95. {
  96. CEnumInst * pEnumObj;
  97. SCODE sc;
  98. WCHAR * pwcClassContext = NULL;
  99. IWbemClassObject * pClassInt = NULL;
  100. CEnumInfo * pInfo = NULL;
  101. // Get the class
  102. sc = m_pGateway->GetObject(Class,0, pCtx, &pClassInt,NULL);
  103. if(sc != S_OK)
  104. return sc;
  105. // Get the class context string
  106. sc = GetAttString(pClassInt, NULL, L"classcontext", &pwcClassContext);
  107. pClassInt->Release();
  108. if(sc != S_OK)
  109. return sc;
  110. // Get the info needed for enumeration. This is overrided by any
  111. // provider that supports dynamic instances.
  112. CProvObj ProvObj(pwcClassContext,MAIN_DELIM, NeedsEscapes());
  113. sc = MakeEnum(pClassInt,ProvObj,&pInfo);
  114. delete pwcClassContext;
  115. if(sc != S_OK)
  116. return sc;
  117. pEnumObj=new CEnumInst(pInfo,lFlags,Class,m_pGateway,this, pCtx);
  118. if(pEnumObj == NULL)
  119. {
  120. delete pInfo;
  121. return WBEM_E_OUT_OF_MEMORY;
  122. }
  123. // Once the enumerator object is created, it owns the info object and
  124. // will free it appropriatly
  125. sc = pEnumObj->QueryInterface(IID_IEnumWbemClassObject,(void **) phEnum);
  126. if(FAILED(sc))
  127. delete pEnumObj;
  128. return sc;
  129. }
  130. //***************************************************************************
  131. //
  132. // CreateInstanceEnumAsyncThread
  133. //
  134. // DESCRIPTION:
  135. //
  136. // Routine for which does the work and sends the
  137. // subsequent notify for the Instance provider's CreateInstanceEnumAsync
  138. // routine.
  139. //
  140. // PARAMETERS:
  141. //
  142. // pIEnum Our enumerator
  143. // pSink Core's sink
  144. // pGateway IWbemServices pointer to the core
  145. // pCtx context to be used.
  146. //
  147. //***************************************************************************
  148. void CreateInstanceEnumAsyncThread( IEnumWbemClassObject FAR* pIEnum,
  149. IWbemObjectSink FAR* pSink, IWbemServices FAR * pGateway, IWbemContext *pCtx)
  150. {
  151. IWbemClassObject * pNewInst = NULL;
  152. SCODE sc = S_OK;
  153. // enumerate and send each object to the notify interface
  154. while (sc == S_OK)
  155. {
  156. ULONG uRet;
  157. sc = pIEnum->Next(-1, 1,&pNewInst,&uRet);
  158. if(sc == S_OK)
  159. {
  160. pSink->Indicate(1,&pNewInst);
  161. pNewInst->Release();
  162. }
  163. }
  164. pSink->SetStatus(0,0,NULL, NULL);
  165. }
  166. //***************************************************************************
  167. //
  168. // SCODE CInstPro::CreateInstanceEnumAsync
  169. //
  170. // DESCRIPTION:
  171. //
  172. // Asynchronously enumeratres the instances of this class.
  173. //
  174. // PARAMETERS:
  175. //
  176. // RefStr Path which defines the object class
  177. // lFlags enumeration flags
  178. // pSink Pointer to the notify object
  179. // plAsyncRequestHandle pointer to the enumeration cancel handle (future use)
  180. //
  181. // RETURN VALUE:
  182. //
  183. // S_OK All is well
  184. // WBEM_E_INVALID_PARAMETER Bad argument
  185. // WBEM_E_OUT_OF_MEMORY Lacked resources to create a thread
  186. // otherwise, error from CreateInstanceEnum
  187. //
  188. //***************************************************************************
  189. SCODE CImpDyn::CreateInstanceEnumAsync(
  190. IN const BSTR RefStr,
  191. IN long lFlags,
  192. IWbemContext __RPC_FAR *pCtx,
  193. OUT IWbemObjectSink FAR* pSink)
  194. {
  195. SCODE sc = S_OK;
  196. IEnumWbemClassObject * pIEnum = NULL;
  197. if(pSink == NULL || RefStr == NULL)
  198. return WBEM_E_INVALID_PARAMETER;
  199. if(IsNT() && IsDcomEnabled())
  200. sc = WbemCoImpersonateClient();
  201. if(sc == S_OK)
  202. sc = CreateInstanceEnum(RefStr,lFlags, pCtx, &pIEnum);
  203. if(sc != S_OK)
  204. {
  205. pSink->SetStatus(0, sc, NULL, NULL);
  206. return S_OK;
  207. }
  208. CreateInstanceEnumAsyncThread(pIEnum, pSink, m_pGateway, pCtx);
  209. pIEnum->Release();
  210. return S_OK;
  211. }
  212. //***************************************************************************
  213. //
  214. // SCODE CImpDyn::CreateClassEnumAsync
  215. //
  216. // DESCRIPTION:
  217. //
  218. // Asynchronously enumeratres the classes that this provider supplies. This is
  219. // acutally just a ruse to get the core to load the dll.
  220. //
  221. // PARAMETERS:
  222. //
  223. // SuperClass Parent to be enumerated.
  224. // lFlags enumeration flags
  225. // pResponseHandler Pointer to the notify object
  226. // plAsyncRequestHandle pointer to the enumeration cancel handle (future use)
  227. //
  228. // RETURN VALUE:
  229. //
  230. // S_OK All is well
  231. //
  232. //***************************************************************************
  233. IWbemServices * gGateway;
  234. SCODE CImpDyn::CreateClassEnumAsync(BSTR Superclass, long lFlags,IWbemContext *pCtx, IWbemObjectSink FAR* pResponseHandler)
  235. {
  236. return E_NOTIMPL;
  237. }
  238. //***************************************************************************
  239. //
  240. // SCODE CImpDyn::GetObject
  241. //
  242. // DESCRIPTION:
  243. //
  244. // Creates an instance given a particular path value.
  245. //
  246. // PARAMETERS:
  247. //
  248. // ObjectPath Object path
  249. // lFlags flags
  250. // pObj Set to point to the created object
  251. // ppErrorObject Optional error object pointer
  252. //
  253. // RETURN VALUE:
  254. //
  255. // S_OK All is well
  256. // WBEM_E_NOT_FOUND bad path
  257. // otherwise the return code from CreateInst
  258. //
  259. //***************************************************************************
  260. SCODE CImpDyn::GetObject(
  261. IN BSTR ObjectPath,
  262. IN long lFlags,
  263. IWbemContext *pCtx,
  264. OUT IWbemClassObject FAR* FAR* pObj,
  265. IWbemCallResult **ppCallResult)
  266. {
  267. SCODE sc = S_OK;
  268. // Parse the object path.
  269. // ======================
  270. ParsedObjectPath * pOutput = 0;
  271. CObjectPathParser p;
  272. int nStatus = p.Parse(ObjectPath, &pOutput);
  273. if(nStatus != 0)
  274. return ReturnAndSetObj(WBEM_E_NOT_FOUND, ppCallResult);
  275. WCHAR wcKeyValue[BUFF_SIZE];
  276. sc = WBEM_E_NOT_FOUND;
  277. if(pOutput->m_dwNumKeys > 0 || pOutput->m_bSingletonObj)
  278. {
  279. if(pOutput->m_bSingletonObj)
  280. StringCchCopyW(wcKeyValue, BUFF_SIZE, L"@");
  281. else
  282. {
  283. KeyRef *pTmp = pOutput->m_paKeys[0];
  284. switch (V_VT(&pTmp->m_vValue))
  285. {
  286. case VT_I4:
  287. StringCchPrintfW (wcKeyValue, BUFF_SIZE, L"%d", V_I4(&pTmp->m_vValue));
  288. break;
  289. case VT_BSTR:
  290. wcsncpy(wcKeyValue, V_BSTR(&pTmp->m_vValue), BUFF_SIZE-1);
  291. break;
  292. default:
  293. StringCchCopyW(wcKeyValue, BUFF_SIZE, L"<unknown>");;
  294. }
  295. }
  296. sc = CreateInst(m_pGateway,pOutput->m_pClass,wcKeyValue,pObj, NULL, NULL, pCtx);
  297. }
  298. // Create the instance.
  299. p.Free(pOutput);
  300. return ReturnAndSetObj(sc, ppCallResult);
  301. }
  302. //***************************************************************************
  303. //
  304. // GetObjectAsyncThread
  305. //
  306. // DESCRIPTION:
  307. //
  308. // Routine for the thread which does the work and sends the
  309. // subsequent notify to the Instance provider's GetObjectAsync
  310. // routine.
  311. //
  312. // PARAMETERS:
  313. //
  314. // pTemp pointer to the argument structure
  315. //
  316. //***************************************************************************
  317. void GetObjectAsyncThread(WCHAR * pObjPath, long lFlags, IWbemObjectSink FAR* pSink,
  318. CImpDyn * pThis, IWbemContext *pCtx)
  319. {
  320. IWbemClassObject FAR* pNewInst = NULL;
  321. SCODE sc = pThis->GetObject(pObjPath, lFlags, pCtx, &pNewInst, NULL);
  322. if(sc == WBEM_NO_ERROR)
  323. {
  324. pSink->Indicate(1,&pNewInst);
  325. pNewInst->Release();
  326. }
  327. pSink->SetStatus(0, sc, NULL, NULL);
  328. }
  329. //***************************************************************************
  330. //
  331. // SCODE CInstPro::GetObjectAsync
  332. //
  333. // DESCRIPTION:
  334. //
  335. // Asynchronously gets an instance of this class.
  336. //
  337. // PARAMETERS:
  338. //
  339. // RefStr Path which defines the object class
  340. // lFlags enumeration flags
  341. // pSink Pointer to the notify object
  342. // plAsyncRequestHandle pointer to the enumeration cancel handle (future use)
  343. //
  344. // RETURN VALUE:
  345. //
  346. // S_OK All is well
  347. // WBEM_E_INVALID_PARAMETER Bad argument
  348. // WBEM_E_OUT_OF_MEMORY Lacked resources to create a thread
  349. //
  350. //***************************************************************************
  351. SCODE CImpDyn::GetObjectAsync(
  352. IN BSTR ObjPath,
  353. IN long lFlags,
  354. IWbemContext __RPC_FAR *pCtx,
  355. IN IWbemObjectSink FAR* pSink)
  356. {
  357. if(pSink == NULL || ObjPath == NULL)
  358. return WBEM_E_INVALID_PARAMETER;
  359. if(IsNT() && IsDcomEnabled())
  360. {
  361. SCODE sc = WbemCoImpersonateClient();
  362. if(sc != S_OK)
  363. {
  364. pSink->SetStatus(0, sc, NULL, NULL);
  365. return S_OK;
  366. }
  367. }
  368. GetObjectAsyncThread(ObjPath, lFlags, pSink, this, pCtx);
  369. return S_OK;
  370. }
  371. //***************************************************************************
  372. //
  373. // SCODE CImpDyn::StartBatch
  374. //
  375. // DESCRIPTION:
  376. //
  377. // Called at the start of a batch of gets or puts. Overriden by
  378. // derived classes which need to do something.
  379. //
  380. // PARAMETERS:
  381. //
  382. // lFlags flags
  383. // pClassInt Points to an instance object
  384. // pObj Misc object pointer
  385. // bGet TRUE if we will be getting data.
  386. //
  387. // RETURN VALUE:
  388. //
  389. // S_OK
  390. //***************************************************************************
  391. SCODE CImpDyn::StartBatch(
  392. IN long lFlags,
  393. IN IWbemClassObject FAR * pClassInt,
  394. IN CObject **pObj,
  395. IN BOOL bGet)
  396. {
  397. *pObj = NULL;
  398. return S_OK;
  399. }
  400. //***************************************************************************
  401. //
  402. // CImpDyn::EndBatch
  403. //
  404. // DESCRIPTION:
  405. //
  406. // Called at the end of a batch of gets or puts. Overriden by
  407. // derived classes which need to do something.
  408. //
  409. // lFlags flags
  410. // pClassInt Points to an instance object
  411. // pObj Misc object pointer
  412. // bGet TRUE if we will be getting data.
  413. //
  414. // RETURN VALUE:
  415. //
  416. // S_OK
  417. //***************************************************************************
  418. void CImpDyn::EndBatch(
  419. IN long lFlags,
  420. IN IWbemClassObject FAR * pClassInt,
  421. IN CObject *pObj,
  422. IN BOOL bGet)
  423. {
  424. if(pObj)
  425. delete pObj;
  426. }
  427. //***************************************************************************
  428. // HRESULT CImpDyn::QueryInterface
  429. // long CImpDyn::AddRef
  430. // long CImpDyn::Release
  431. //
  432. // DESCRIPTION:
  433. //
  434. // Standard Com IUNKNOWN functions.
  435. //
  436. //***************************************************************************
  437. STDMETHODIMP CImpDyn::QueryInterface(
  438. IN REFIID riid,
  439. OUT PPVOID ppv)
  440. {
  441. *ppv=NULL;
  442. // The only calls for IUnknown are either in a nonaggregated
  443. // case or when created in an aggregation, so in either case
  444. // always return our IUnknown for IID_IUnknown.
  445. if (IID_IUnknown==riid || IID_IWbemServices == riid)
  446. *ppv=(IWbemServices*)this;
  447. else if (IID_IWbemProviderInit==riid)
  448. *ppv=(IWbemProviderInit*)this;
  449. if (NULL!=*ppv)
  450. {
  451. ((LPUNKNOWN)*ppv)->AddRef();
  452. return NOERROR;
  453. }
  454. return ResultFromScode(E_NOINTERFACE);
  455. }
  456. STDMETHODIMP_(ULONG) CImpDyn::AddRef(void)
  457. {
  458. return InterlockedIncrement(&m_cRef);
  459. }
  460. STDMETHODIMP_(ULONG) CImpDyn::Release(void)
  461. {
  462. long lRet = InterlockedDecrement(&m_cRef);
  463. if (0L!=lRet)
  464. return lRet;
  465. /*
  466. * Tell the housing that an object is going away so it can
  467. * shut down if appropriate.
  468. */
  469. delete this; // do before decrementing module obj count.
  470. InterlockedDecrement(&lObj);
  471. return 0;
  472. }
  473. //***************************************************************************
  474. //
  475. // CImpDyn::EnumPropDoFunc
  476. //
  477. // DESCRIPTION:
  478. //
  479. // This gets an objects attributes and then loops through the propeties
  480. // calling UpdateProperty or RefreshProperty to put or get the data.
  481. //
  482. // PARAMETERS:
  483. //
  484. // lFlags flags
  485. // pClassInt Object being refreshed or updated
  486. // FuncType Indicates if refresh or update
  487. // pwcKey Optional Key value of the object
  488. // pCache Option cache
  489. //
  490. // RETURN VALUE:
  491. //
  492. // S_OK all is well
  493. // otherwise an WBEMSVC type error.
  494. //
  495. //***************************************************************************
  496. SCODE CImpDyn::EnumPropDoFunc(
  497. IN long lFlags,
  498. OUT IN IWbemClassObject FAR* pInstance,
  499. IN FUNCTYPE FuncType,
  500. IN WCHAR * pwcKey,
  501. OUT IN CIndexCache * pCache,
  502. IN IWbemClassObject * pClass)
  503. {
  504. SCODE sc;
  505. BSTR PropName;
  506. WCHAR * pwcClassContext = NULL;
  507. WCHAR * pwcKeyValue = NULL;
  508. WCHAR * pwcTemp = NULL;
  509. BOOL bClsidSetForClass = FALSE;
  510. CProvObj * pProvObj = NULL;
  511. BSTR bstrKeyName = NULL;
  512. BOOL bGotOurClass = FALSE;
  513. int iWorked = 0;
  514. int iCurr = 0;
  515. CVariant vProp;
  516. BOOL bTesterDetails = FALSE;
  517. bool bAccessDenied = false;
  518. // Make sure we have a class object. In some cases, such as enumeration, it
  519. // is passed. In others, it must be obtained
  520. if(pClass == NULL)
  521. {
  522. VARIANT var;
  523. VariantInit(&var);
  524. sc = pInstance->Get(L"__Class",0,&var,NULL,NULL);
  525. if(sc != S_OK)
  526. return sc;
  527. if(m_pGateway == NULL)
  528. return WBEM_E_FAILED;
  529. sc = m_pGateway->GetObject(var.bstrVal,0, NULL, &pClass, NULL);
  530. VariantClear(&var);
  531. if(FAILED(sc))
  532. return sc;
  533. bGotOurClass = TRUE;
  534. }
  535. // Find out if expensive test data is desired
  536. {
  537. IWbemQualifierSet * pQualifier = NULL;
  538. CVariant var;
  539. sc = pClass->GetQualifierSet(&pQualifier); // Get instance Qualifier
  540. if(FAILED(sc))
  541. return sc;
  542. sc = pQualifier->Get(L"TesterDetails" ,0,var.GetVarPtr(), NULL);
  543. pQualifier->Release(); // free up the interface
  544. if(sc == S_OK && var.GetType() == VT_BOOL && var.bGetBOOL())
  545. bTesterDetails = TRUE;
  546. }
  547. CObject * pPackageObj = NULL;
  548. sc = StartBatch(lFlags,pInstance,&pPackageObj,FuncType == REFRESH);
  549. if(sc != S_OK)
  550. return WBEM_E_FAILED;
  551. sc = GetAttString(pClass, NULL, L"classcontext", &pwcClassContext);
  552. if(pwcKey)
  553. {
  554. // This is a special case and means that we are being called
  555. // as part of instance enumeration. This means that the key value
  556. // is already known, and that the class is one of ours
  557. pwcKeyValue = pwcKey;
  558. bClsidSetForClass = TRUE;
  559. }
  560. else
  561. {
  562. // Get the Key property. Note that this doesnt have to work since
  563. // there might be single instance classes supported by this provider!
  564. bstrKeyName = GetKeyName(pClass);
  565. if(bstrKeyName != NULL)
  566. {
  567. sc = pInstance->Get(bstrKeyName,0,vProp.GetVarPtr(),NULL,NULL);
  568. SysFreeString(bstrKeyName);
  569. if(sc == S_OK)
  570. {
  571. VARIANT * pTemp = vProp.GetVarPtr();
  572. if(pTemp->vt == VT_BSTR && pTemp->bstrVal != 0)
  573. {
  574. DWORD dwLen = wcslen(vProp.GetBstr())+1;
  575. pwcKeyValue = new WCHAR[dwLen];
  576. if(pwcKeyValue == NULL)
  577. {
  578. sc = WBEM_E_OUT_OF_MEMORY;
  579. goto EnumCleanUp;
  580. }
  581. StringCchCopyW(pwcKeyValue, dwLen, vProp.GetBstr());
  582. }
  583. }
  584. vProp.Clear();
  585. }
  586. }
  587. // For each property, Get the properties Qualifiers and
  588. // call the appropriate function to do the a refreshing/updating
  589. pInstance->BeginEnumeration(0);
  590. while(WBEM_NO_ERROR == pInstance->Next(0,&PropName, vProp.GetVarPtr(), NULL, NULL))
  591. {
  592. vProp.Clear();
  593. pwcTemp = NULL;
  594. sc = GetAttString(pClass, PropName, L"propertycontext", &pwcTemp, pCache, iCurr);
  595. iCurr++;
  596. if(sc== S_OK)
  597. {
  598. LPWSTR pwcFullContext = NULL;
  599. sc = MergeStrings(&pwcFullContext,pwcClassContext,pwcKeyValue,pwcTemp);
  600. if(pwcTemp)
  601. {
  602. delete pwcTemp;
  603. pwcTemp = NULL;
  604. }
  605. if(sc == S_OK)
  606. {
  607. if(pProvObj == NULL)
  608. {
  609. pProvObj = new CProvObj(pwcFullContext,MAIN_DELIM,NeedsEscapes());
  610. if(pProvObj == NULL)
  611. {
  612. sc = WBEM_E_OUT_OF_MEMORY;
  613. break;
  614. }
  615. }
  616. else if (!pProvObj->Update(pwcFullContext))
  617. {
  618. sc = WBEM_E_FAILED;
  619. break;
  620. }
  621. sc = pProvObj->dwGetStatus(iGetMinTokens());
  622. if(FuncType == REFRESH && sc == S_OK)
  623. {
  624. sc = RefreshProperty(lFlags,pInstance,
  625. PropName, *pProvObj, pPackageObj, NULL,bTesterDetails);
  626. if(sc == S_OK)
  627. iWorked++;
  628. else if (sc == 5)
  629. bAccessDenied = true;
  630. }
  631. else if(FuncType == UPDATE && sc == S_OK)
  632. {
  633. sc = UpdateProperty(lFlags,pInstance,
  634. PropName, *pProvObj, pPackageObj, NULL);
  635. if(sc == S_OK)
  636. iWorked++;
  637. else if (sc == 5)
  638. bAccessDenied = true;
  639. }
  640. if(pwcFullContext)
  641. delete pwcFullContext;
  642. }
  643. }
  644. else
  645. sc = S_OK; // ignore props without propertyContext
  646. SysFreeString(PropName);
  647. }
  648. EnumCleanUp:
  649. if(iWorked > 0)
  650. sc = S_OK;
  651. else if(bAccessDenied)
  652. sc = WBEM_E_ACCESS_DENIED;
  653. else
  654. sc = WBEM_E_INVALID_OBJECT_PATH;
  655. if(pProvObj)
  656. delete pProvObj;
  657. if(pwcTemp)
  658. delete pwcTemp;
  659. if(pwcClassContext)
  660. delete pwcClassContext;
  661. if(pwcKeyValue && pwcKey == NULL)
  662. delete pwcKeyValue;
  663. if(bGotOurClass)
  664. pClass->Release();
  665. EndBatch(lFlags,pInstance,pPackageObj,FuncType == REFRESH);
  666. return sc;
  667. }
  668. //***************************************************************************
  669. //
  670. // SCODE CImpDyn::GetAttString
  671. //
  672. // DESCRIPTION:
  673. //
  674. // Gets an Qualifier string. The string is will be pointed to by
  675. // the ppResult argument and should be freed by "delete".
  676. //
  677. // PARAMETERS:
  678. //
  679. // pClassInt Class object pointer
  680. // pPropName Property Name, could be null if going after
  681. // class attribute
  682. // pAttName Attribute name
  683. // ppResult Set to point to the retuned value.
  684. // pCache Optional cache of values
  685. // iIndex Optional index in the cache
  686. //
  687. // RETURN VALUE:
  688. //
  689. // S_OK all is well
  690. // WBEM_E_OUT_OF_MEMORY
  691. // otherwise the error is set by the class object's GetQualifierSet function
  692. // or by the qualifier sets "Get" function.
  693. //
  694. //***************************************************************************
  695. SCODE CImpDyn::GetAttString(
  696. IN IWbemClassObject FAR* pClassInt,
  697. IN LPWSTR pPropName,
  698. IN LPWSTR pAttName,
  699. OUT IN LPWSTR * ppResult,
  700. OUT IN CIndexCache * pCache,
  701. IN int iIndex)
  702. {
  703. SCODE sc;
  704. IWbemQualifierSet * pQualifier = NULL;
  705. CVariant var;
  706. if(*ppResult)
  707. delete *ppResult;
  708. *ppResult = NULL;
  709. // if there is a cache, try to get it from there
  710. if(pCache && iIndex != -1)
  711. {
  712. *ppResult = pCache->GetWString(iIndex);
  713. if(*ppResult != NULL)
  714. return S_OK;
  715. }
  716. // Get an Qualifier set interface.
  717. if(pPropName == NULL)
  718. sc = pClassInt->GetQualifierSet(&pQualifier); // Get instance Qualifier
  719. else
  720. sc = pClassInt->GetPropertyQualifierSet(pPropName,&pQualifier); // Get prop att
  721. if(FAILED(sc))
  722. return sc;
  723. // Get the string and free the Qualifier interface
  724. sc = pQualifier->Get(pAttName,0,var.GetVarPtr(), NULL);
  725. pQualifier->Release(); // free up the interface
  726. if(FAILED(sc))
  727. return sc;
  728. // make sure the type is OK
  729. if(var.GetType() != VT_BSTR)
  730. return WBEM_E_FAILED;
  731. // Allocate data for the buffer and copy the results
  732. DWORD dwLen = wcslen(var.GetBstr())+1;
  733. *ppResult = new WCHAR[dwLen];
  734. if(*ppResult)
  735. {
  736. StringCchCopyW(*ppResult, dwLen, var.GetBstr());
  737. sc = S_OK;
  738. }
  739. else
  740. sc = WBEM_E_OUT_OF_MEMORY;
  741. // If there is a cache, add this to it
  742. if(pCache && iIndex != -1 && *ppResult)
  743. {
  744. pCache->SetAt(*ppResult, iIndex);
  745. }
  746. return sc;
  747. }
  748. //***************************************************************************
  749. //
  750. // BSTR CImpDyn::GetKeyName
  751. //
  752. // DESCRIPTION:
  753. //
  754. // Gets the name of the property with the the key Qualifier.
  755. //
  756. // PARAMETERS:
  757. //
  758. // pClassInt Class object pointer
  759. //
  760. // RETURN VALUE:
  761. //
  762. // NULL if error,
  763. // other wise a BSTR which the caller must free
  764. //
  765. //***************************************************************************
  766. BSTR CImpDyn::GetKeyName(
  767. IN IWbemClassObject FAR* pClassInt)
  768. {
  769. IWbemQualifierSet * pQualifier = NULL;
  770. BSTR PropName = NULL;
  771. pClassInt->BeginEnumeration(WBEM_FLAG_KEYS_ONLY);
  772. // Loop through each of the properties and stop once one if found that
  773. // has a "Key" Qualifier
  774. while(WBEM_NO_ERROR == pClassInt->Next(0,&PropName,NULL, NULL, NULL))
  775. {
  776. return PropName;
  777. }
  778. return NULL;
  779. }
  780. //***************************************************************************
  781. //
  782. // SCODE CImpDyn::PutInstanceAsync
  783. //
  784. // DESCRIPTION:
  785. //
  786. // PutInstaceAsync writes instance data out.
  787. //
  788. // PARAMETERS:
  789. //
  790. // pClassInt Class Object pointer
  791. // lFlags flags.
  792. // pCtx Context object, not used anymore.
  793. // pResponseHandler Where to set the return code.
  794. //
  795. // RETURN VALUE:
  796. //
  797. // Set by EnumPropDoFunc
  798. //
  799. //***************************************************************************
  800. STDMETHODIMP CImpDyn::PutInstanceAsync(
  801. /* [in] */ IWbemClassObject __RPC_FAR *pInst,
  802. /* [in] */ long lFlags,
  803. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  804. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  805. {
  806. SCODE sc = S_OK;
  807. if(pInst == NULL || pResponseHandler == NULL)
  808. return WBEM_E_INVALID_PARAMETER;
  809. if(IsNT() && IsDcomEnabled())
  810. sc = WbemCoImpersonateClient();
  811. if(sc == S_OK)
  812. sc = EnumPropDoFunc(lFlags,pInst,UPDATE);
  813. // Set the status
  814. pResponseHandler->SetStatus(0,sc, NULL, NULL);
  815. return sc;
  816. }
  817. //***************************************************************************
  818. //
  819. // SCODE CImpDyn::ExecMethodAsync
  820. //
  821. // DESCRIPTION:
  822. //
  823. // Executes methods.
  824. //
  825. // PARAMETERS:
  826. //
  827. // pClassInt Class Object pointer
  828. // lFlags flags.
  829. // pCtx Context object, not used anymore.
  830. // pResponseHandler Where to set the return code.
  831. //
  832. // RETURN VALUE:
  833. //
  834. // Set by EnumPropDoFunc
  835. //
  836. //***************************************************************************
  837. STDMETHODIMP CImpDyn::ExecMethodAsync(
  838. /* [in] */ const BSTR ObjectPath,
  839. /* [in] */ const BSTR MethodName,
  840. /* [in] */ long lFlags,
  841. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  842. /* [in] */ IWbemClassObject __RPC_FAR *pInParams,
  843. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  844. )
  845. {
  846. return MethodAsync(ObjectPath, MethodName,
  847. lFlags,pCtx, pInParams, pResponseHandler);
  848. }
  849. //***************************************************************************
  850. //
  851. // SCODE CImpDyn::RefreshInstance
  852. //
  853. // DESCRIPTION:
  854. //
  855. // gets fresh values for the properties.
  856. //
  857. // PARAMETERS:
  858. //
  859. // pClassInt Class Object pointer
  860. // lFlags flags.
  861. // ppErrorObject Optional error object
  862. //
  863. // RETURN VALUE:
  864. //
  865. // Set by EnumPropDoFunc
  866. //
  867. //***************************************************************************
  868. STDMETHODIMP CImpDyn::RefreshInstance(
  869. IN long lFlags,
  870. OUT IN IWbemClassObject FAR* pClassInt)
  871. {
  872. return EnumPropDoFunc(lFlags,pClassInt,REFRESH);
  873. }
  874. //***************************************************************************
  875. //
  876. // SCODE CImpDyn::CreateInst
  877. //
  878. // DESCRIPTION:
  879. //
  880. // Creates a new instance via the gateway provider and sets
  881. // the inital values of the properties.
  882. //
  883. // PARAMETERS:
  884. //
  885. // pGateway Pointer to WBEM
  886. // pwcClass Class Name
  887. // pKey Key value
  888. // pNewInst returned pointer to created instance
  889. // pwcKeyName Name of key property
  890. // pCache Optional cache
  891. //
  892. // RETURN VALUE:
  893. //
  894. // Return: S_OK if all is well,
  895. // otherwise an error code is returned by either GetObject(most likely)
  896. // or spawn instance.
  897. //
  898. //***************************************************************************
  899. SCODE CImpDyn::CreateInst(
  900. IN IWbemServices * pGateway,
  901. IN LPWSTR pwcClass,
  902. IN LPWSTR pKey,
  903. OUT IN IWbemClassObject ** pNewInst,
  904. IN LPWSTR pwcKeyName,
  905. OUT IN CIndexCache * pCache,
  906. IWbemContext *pCtx)
  907. {
  908. SCODE sc;
  909. IWbemClassObject * pClass = NULL;
  910. // Create the new instance
  911. sc = pGateway->GetObject(pwcClass,0, pCtx, &pClass, NULL);
  912. if(FAILED(sc))
  913. return sc;
  914. sc = pClass->SpawnInstance(0, pNewInst);
  915. if(FAILED(sc))
  916. {
  917. pClass->Release();
  918. return sc;
  919. }
  920. // Set the key value. Note that CVariant isnt used because
  921. // it assumes that the input is a TCHAR.
  922. VARIANT var;
  923. var.vt = VT_BSTR;
  924. var.bstrVal = SysAllocString(pKey);
  925. if(var.bstrVal == NULL)
  926. {
  927. (*pNewInst)->Release();
  928. pClass->Release();
  929. return WBEM_E_OUT_OF_MEMORY;
  930. }
  931. BSTR bstrKeyName;
  932. if(pwcKeyName == NULL)
  933. bstrKeyName = GetKeyName(*pNewInst);
  934. else
  935. bstrKeyName = SysAllocString(pwcKeyName);
  936. if(bstrKeyName != NULL)
  937. {
  938. sc = (*pNewInst)->Put(bstrKeyName,0,&var,0);
  939. SysFreeString(bstrKeyName);
  940. }
  941. VariantClear(&var);
  942. // Use the RefreshInstance routine to set all the other properties
  943. sc = EnumPropDoFunc(0,*pNewInst,REFRESH, pKey, pCache, pClass);
  944. pClass->Release();
  945. if (FAILED(sc))
  946. (*pNewInst)->Release();
  947. // Clean up
  948. return sc;
  949. }
  950. //***************************************************************************
  951. //
  952. // SCODE CImpDyn::MergeStrings
  953. //
  954. // DESCRIPTION:
  955. //
  956. // Combines the Class Context, Key, and Property Context strings.
  957. //
  958. // PARAMETERS:
  959. //
  960. // ppOut output combined string, must be freed via delete
  961. // pClassContext class context
  962. // pKey key value
  963. // pPropContext property context
  964. //
  965. // RETURN VALUE:
  966. //
  967. // S_OK if all is well,
  968. // WBEM_E_INVALID_PARAMETER no property context string or ppOut is NULL
  969. // WBEM_E_OUT_OF_MEMORY
  970. //***************************************************************************
  971. SCODE CImpDyn::MergeStrings(
  972. OUT LPWSTR * ppOut,
  973. IN LPWSTR pClassContext,
  974. IN LPWSTR pKey,
  975. IN LPWSTR pPropContext)
  976. {
  977. // Allocate space for output
  978. int iLen = 3;
  979. if(pClassContext)
  980. iLen += wcslen(pClassContext);
  981. if(pKey)
  982. iLen += wcslen(pKey);
  983. if(pPropContext)
  984. iLen += wcslen(pPropContext);
  985. else
  986. return WBEM_E_INVALID_PARAMETER; // should always have this!
  987. if(ppOut == NULL)
  988. return WBEM_E_INVALID_PARAMETER; // should always have this!
  989. *ppOut = new WCHAR[iLen];
  990. if(*ppOut == NULL)
  991. return WBEM_E_OUT_OF_MEMORY;
  992. // simple case is that everything is in the property context. That would
  993. // be the case when the provider is being used as a simple dynamic
  994. // property provider
  995. if(pClassContext == NULL || pKey == NULL) {
  996. StringCchCopyW(*ppOut, iLen, pPropContext);
  997. return S_OK;
  998. }
  999. // Copy the class context, then search for the delimeter
  1000. StringCchCopyW(*ppOut, iLen, pClassContext);
  1001. WCHAR * pTest;
  1002. for(pTest = *ppOut; *pTest; pTest++)
  1003. if(*pTest == MAIN_DELIM)
  1004. break;
  1005. // Three cases;
  1006. if(*pTest == NULL)
  1007. StringCchCatW(*ppOut, iLen,L"|"); // HKLM BLA VALUE add |
  1008. else if( *(pTest +1))
  1009. StringCchCatW(*ppOut, iLen,L"\\"); // HKLM|BLA BLA VALUE add \
  1010. else; // HKLM| BLA VALUE do nothing!
  1011. StringCchCatW(*ppOut, iLen,pKey);
  1012. if(pPropContext[0] != L'|' && pPropContext[0] != L'\\')
  1013. StringCchCatW(*ppOut, iLen,L"|");
  1014. StringCchCatW(*ppOut, iLen,pPropContext);
  1015. return S_OK;
  1016. }
  1017. //***************************************************************************
  1018. //
  1019. // SCODE CImpDyn::ReturnAndSetObj
  1020. //
  1021. // DESCRIPTION:
  1022. //
  1023. // Takes care of creating and setting an error object.
  1024. //
  1025. // PARAMETERS:
  1026. //
  1027. // sc value to set
  1028. // ppErrorObject point which will point to the error object.
  1029. //
  1030. // RETURN VALUE:
  1031. //
  1032. // what ever was passed in.
  1033. //***************************************************************************
  1034. SCODE CImpDyn::ReturnAndSetObj(
  1035. IN SCODE sc,
  1036. OUT IN IWbemCallResult FAR* FAR* ppCallResult)
  1037. {
  1038. // if(ppErrorObject)
  1039. // *ppErrorObject = GetNotifyObj(m_pGateway,sc);
  1040. return sc;
  1041. }
  1042. //***************************************************************************
  1043. //
  1044. // long CImpDyn::AddRef
  1045. //
  1046. // DESCRIPTION:
  1047. //
  1048. // Adds to the reference count.
  1049. //
  1050. // RETURN VALUE:
  1051. //
  1052. // current reference count.
  1053. //
  1054. //***************************************************************************
  1055. long CEnumInfo::AddRef(void)
  1056. {
  1057. return InterlockedIncrement(&m_cRef);
  1058. }
  1059. //***************************************************************************
  1060. //
  1061. // long CImpDyn::Release
  1062. //
  1063. // DESCRIPTION:
  1064. //
  1065. // Interface has been released. Object will be deleted if the
  1066. // usage count is zero.
  1067. //
  1068. // RETURN VALUE:
  1069. //
  1070. // current reference count.
  1071. //
  1072. //***************************************************************************
  1073. long CEnumInfo::Release(void)
  1074. {
  1075. long lRet = InterlockedDecrement(&m_cRef);
  1076. if (0L!=lRet)
  1077. return lRet;
  1078. delete this;
  1079. return 0;
  1080. }