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.

294 lines
7.3 KiB

  1. /*++
  2. Copyright (C) 1995-2001 Microsoft Corporation
  3. Module Name:
  4. ENUMINST.CPP
  5. Abstract:
  6. Implements the CEnumInst class which enumerates instances.
  7. History:
  8. a-davj 19-Oct-95 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include "impdyn.h"
  12. //***************************************************************************
  13. //
  14. // CCFDyn::CCFDyn
  15. //
  16. // DESCRIPTION:
  17. //
  18. // Constructor.
  19. //
  20. // PARAMETERS:
  21. //
  22. // pEnumInfo Object which enumerates the key values
  23. // lFlags flags passed to the CreateInstanceEnum call
  24. // pClass name of the class
  25. // pWBEMGateway pointer to WBEM core
  26. // pProvider pointer to provider obect which was asked to create
  27. // the enumerator.
  28. //***************************************************************************
  29. CEnumInst::CEnumInst(
  30. IN CEnumInfo * pEnumInfo,
  31. IN long lFlags,
  32. IN WCHAR * pClass,
  33. IN IWbemServices FAR* pWBEMGateway,
  34. IN CImpDyn * pProvider,
  35. IWbemContext *pCtx):
  36. m_iIndex(0), m_pEnumInfo(0),m_pwcClass(0), m_lFlags(0), m_pCtx(0), m_pWBEMGateway(0),
  37. m_pProvider(0), m_cRef(0), m_bstrKeyName(0), m_PropContextCache()
  38. {
  39. m_pwcClass = new WCHAR[wcslen(pClass)+1];
  40. if(m_pwcClass == NULL) return;
  41. wcscpy(m_pwcClass,pClass);
  42. m_pWBEMGateway = pWBEMGateway;
  43. m_pWBEMGateway->AddRef();
  44. m_pProvider = pProvider;
  45. m_pProvider->AddRef();
  46. m_lFlags = lFlags;
  47. m_pEnumInfo = pEnumInfo;
  48. m_pEnumInfo->AddRef();
  49. m_pCtx = pCtx;
  50. if(pCtx) pCtx->AddRef();
  51. InterlockedIncrement(&lObj);
  52. // Get the KeyName
  53. IWbemClassObject * pClassObj = NULL;
  54. SCODE sc = m_pWBEMGateway->GetObject(pClass,0,m_pCtx,&pClassObj,NULL);
  55. if(FAILED(sc)) return;
  56. m_bstrKeyName = m_pProvider->GetKeyName(pClassObj);
  57. pClassObj->Release();
  58. }
  59. //***************************************************************************
  60. //
  61. // CCFDyn::~CCFDyn
  62. //
  63. // DESCRIPTION:
  64. //
  65. // Destructor.
  66. //
  67. //***************************************************************************
  68. CEnumInst::~CEnumInst(void)
  69. {
  70. if(m_pwcClass)
  71. delete m_pwcClass;
  72. if(m_pWBEMGateway != NULL) {
  73. m_pWBEMGateway->Release();
  74. m_pProvider->Release();
  75. m_pEnumInfo->Release();
  76. InterlockedDecrement(&lObj);
  77. }
  78. if(m_pEnumInfo != NULL)
  79. delete m_pEnumInfo;
  80. if(m_pCtx)
  81. m_pCtx->Release();
  82. if(m_bstrKeyName)
  83. SysFreeString(m_bstrKeyName);
  84. return;
  85. }
  86. //***************************************************************************
  87. // HRESULT CEnumInst::QueryInterface
  88. // long CEnumInst::AddRef
  89. // long CEnumInst::Release
  90. //
  91. // DESCRIPTION:
  92. //
  93. // Standard Com IUNKNOWN functions.
  94. //
  95. //***************************************************************************
  96. STDMETHODIMP CEnumInst::QueryInterface(
  97. IN REFIID riid,
  98. OUT PPVOID ppv)
  99. {
  100. *ppv=NULL;
  101. if ((IID_IUnknown==riid || IID_IEnumWbemClassObject==riid)
  102. && m_pWBEMGateway != NULL)
  103. {
  104. *ppv=this;
  105. AddRef();
  106. return NOERROR;
  107. }
  108. else
  109. return ResultFromScode(E_NOINTERFACE);
  110. }
  111. STDMETHODIMP_(ULONG) CEnumInst::AddRef(void)
  112. {
  113. return InterlockedIncrement(&m_cRef);
  114. }
  115. STDMETHODIMP_(ULONG) CEnumInst::Release(void)
  116. {
  117. long lRet = InterlockedDecrement(&m_cRef);
  118. if (0L!=lRet)
  119. return lRet;
  120. delete this;
  121. return 0L;
  122. }
  123. //***************************************************************************
  124. //
  125. // CEnumInst::Reset
  126. //
  127. // DESCRIPTION:
  128. //
  129. // Sets pointer back to first element.
  130. //
  131. // RETURN VALUES:
  132. //
  133. // S_OK
  134. //
  135. //***************************************************************************
  136. STDMETHODIMP CEnumInst::Reset()
  137. {
  138. m_iIndex = 0;
  139. return S_OK;
  140. }
  141. //***************************************************************************
  142. //
  143. // CEnumInst::Clone
  144. //
  145. // DESCRIPTION:
  146. //
  147. // Create a duplicate of the enumerator
  148. //
  149. // PARAMETERS:
  150. //
  151. // pEnum Set to point to duplicate.
  152. //
  153. // RETURN VALUES:
  154. //
  155. // S_OK if all is well
  156. // WBEM_E_OUT_OF_MEMORY if out of memory
  157. // WBEM_E_INVALID_PARAMETER if passed a null
  158. //
  159. //***************************************************************************
  160. STDMETHODIMP CEnumInst::Clone(
  161. OUT IEnumWbemClassObject FAR* FAR* pEnum)
  162. {
  163. CEnumInst * pEnumObj;
  164. SCODE sc;
  165. if(pEnum == NULL)
  166. return WBEM_E_INVALID_PARAMETER;
  167. pEnumObj=new CEnumInst(m_pEnumInfo,m_lFlags,m_pwcClass,
  168. m_pWBEMGateway,m_pProvider, m_pCtx);
  169. if(pEnumObj == NULL)
  170. return WBEM_E_OUT_OF_MEMORY;
  171. sc = pEnumObj->QueryInterface(IID_IEnumWbemClassObject,(void **) pEnum);
  172. if(FAILED(sc))
  173. delete pEnumObj;
  174. pEnumObj->m_iIndex = m_iIndex;
  175. return S_OK;
  176. }
  177. //***************************************************************************
  178. //
  179. // CEnumInst::Skip
  180. //
  181. // DESCRIPTION:
  182. //
  183. // Skips one or more elements in the enumeration.
  184. //
  185. // PARAMETERS:
  186. //
  187. // nNum number of elements to skip
  188. //
  189. // RETURN VALUES:
  190. //
  191. // S_OK if we still are not past the end of the list
  192. // S_FALSE if requested skip number would go beyond the end of the list
  193. //
  194. //***************************************************************************
  195. STDMETHODIMP CEnumInst::Skip(long lTimeout,
  196. IN ULONG nNum)
  197. {
  198. SCODE sc;;
  199. int iTest = m_iIndex + nNum;
  200. LPWSTR pwcKey;
  201. sc = m_pProvider->GetKey(m_pEnumInfo,iTest,&pwcKey);
  202. if(sc == S_OK) {
  203. delete pwcKey;
  204. m_iIndex = iTest;
  205. return S_OK;
  206. }
  207. return S_FALSE;
  208. }
  209. //***************************************************************************
  210. //
  211. // CEnumInst::Next
  212. //
  213. // DESCRIPTION:
  214. //
  215. // Returns one or more instances.
  216. //
  217. // PARAMETERS:
  218. //
  219. // uCount Number of instances to return.
  220. // pObj Pointer to array of objects.
  221. // puReturned Pointer to number of objects successfully returned.
  222. //
  223. // RETURN VALUES:
  224. // S_OK if all the request instances are returned. Note that WBEM_E_FAILED
  225. // is returned even if there are some instances returned so long as the
  226. // number is less than uCount. Also WBEM_E_INVALID_PARAMETER may be
  227. // return if the arguments are bogus.
  228. //
  229. //***************************************************************************
  230. STDMETHODIMP CEnumInst::Next(long lTimeout,
  231. IN ULONG uCount,
  232. OUT IWbemClassObject FAR* FAR* pObj,
  233. OUT ULONG FAR* puReturned)
  234. {
  235. ULONG uIndex;
  236. SCODE sc;
  237. LPWSTR pwcKey;
  238. if(pObj == NULL || puReturned == NULL)
  239. return WBEM_E_INVALID_PARAMETER;
  240. IWbemClassObject FAR* FAR* pNewInst = pObj;
  241. *puReturned = 0;
  242. for(uIndex = 0; uIndex < uCount; )
  243. {
  244. sc = m_pProvider->GetKey(m_pEnumInfo,m_iIndex,&pwcKey);
  245. m_iIndex++;
  246. if(sc != S_OK)
  247. break; // if no more in registry, then we are done
  248. sc = m_pProvider->CreateInst(m_pWBEMGateway,m_pwcClass,
  249. pwcKey,pNewInst,m_bstrKeyName,
  250. &m_PropContextCache, m_pCtx);
  251. delete pwcKey;
  252. if(sc == S_OK)
  253. {
  254. uIndex++;
  255. pNewInst++;
  256. (*puReturned)++; // add one to number of objects created
  257. }
  258. }
  259. return (uIndex == uCount) ? S_OK : WBEM_E_FAILED;
  260. }