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.

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