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.

302 lines
5.8 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation
  4. //
  5. // ENUMVAR.CPP
  6. //
  7. // alanbos 15-Aug-96 Created.
  8. //
  9. // Defines the implementation of IEnumVARIANT
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. //***************************************************************************
  14. //
  15. // CEnumVar::CEnumVar
  16. //
  17. // DESCRIPTION:
  18. //
  19. // Constructors.
  20. //
  21. //***************************************************************************
  22. CEnumVar::CEnumVar(CSWbemObjectSet *pObject)
  23. {
  24. m_cRef=0;
  25. m_pEnumObject = pObject;
  26. m_pEnumObject->AddRef ();
  27. InterlockedIncrement(&g_cObj);
  28. }
  29. CEnumVar::CEnumVar(void)
  30. {
  31. m_cRef=0;
  32. m_pEnumObject = NULL;
  33. InterlockedIncrement(&g_cObj);
  34. }
  35. //***************************************************************************
  36. //
  37. // CEnumVar::~CEnumVar
  38. //
  39. // DESCRIPTION:
  40. //
  41. // Destructor.
  42. //
  43. //***************************************************************************
  44. CEnumVar::~CEnumVar(void)
  45. {
  46. InterlockedDecrement(&g_cObj);
  47. RELEASEANDNULL(m_pEnumObject)
  48. }
  49. //***************************************************************************
  50. // HRESULT CEnumVar::QueryInterface
  51. // long CEnumVar::AddRef
  52. // long CEnumVar::Release
  53. //
  54. // DESCRIPTION:
  55. //
  56. // Standard Com IUNKNOWN functions.
  57. //
  58. //***************************************************************************
  59. STDMETHODIMP CEnumVar::QueryInterface (
  60. IN REFIID riid,
  61. OUT LPVOID *ppv
  62. )
  63. {
  64. *ppv=NULL;
  65. if (IID_IUnknown==riid || IID_IEnumVARIANT==riid)
  66. *ppv=this;
  67. if (NULL!=*ppv)
  68. {
  69. ((LPUNKNOWN)*ppv)->AddRef();
  70. return NOERROR;
  71. }
  72. return ResultFromScode(E_NOINTERFACE);
  73. }
  74. STDMETHODIMP_(ULONG) CEnumVar::AddRef(void)
  75. {
  76. InterlockedIncrement(&m_cRef);
  77. return m_cRef;
  78. }
  79. STDMETHODIMP_(ULONG) CEnumVar::Release(void)
  80. {
  81. InterlockedDecrement(&m_cRef);
  82. if (0L!=m_cRef)
  83. return m_cRef;
  84. delete this;
  85. return 0;
  86. }
  87. //***************************************************************************
  88. //
  89. // SCODE CEnumVar::Reset
  90. //
  91. // DESCRIPTION:
  92. //
  93. // Reset the enumeration
  94. //
  95. // PARAMETERS:
  96. //
  97. // RETURN VALUES:
  98. //
  99. // S_OK success
  100. // S_FALSE otherwise
  101. //
  102. //***************************************************************************
  103. HRESULT CEnumVar::Reset ()
  104. {
  105. HRESULT hr = S_FALSE;
  106. if (m_pEnumObject)
  107. {
  108. if (WBEM_S_NO_ERROR == m_pEnumObject->Reset ())
  109. hr = S_OK;
  110. SetWbemError (m_pEnumObject->GetSWbemServices ());
  111. }
  112. return hr;
  113. }
  114. //***************************************************************************
  115. //
  116. // SCODE CEnumVar::Next
  117. //
  118. // DESCRIPTION:
  119. //
  120. // Get the next object in the enumeration
  121. //
  122. // PARAMETERS:
  123. //
  124. // lTimeout Number of ms to wait for object (or WBEM_INFINITE for
  125. // indefinite)
  126. // ppObject On return may contain the next element (if any)
  127. //
  128. // RETURN VALUES:
  129. //
  130. // S_OK success
  131. // S_FALSE not all elements could be returned
  132. //
  133. //***************************************************************************
  134. HRESULT CEnumVar::Next (
  135. ULONG cElements,
  136. VARIANT FAR* pVar,
  137. ULONG FAR* pcElementFetched
  138. )
  139. {
  140. HRESULT hr = S_OK;
  141. ULONG l2 = 0;
  142. if (NULL != pcElementFetched)
  143. *pcElementFetched = 0;
  144. if ((NULL != pVar) && (m_pEnumObject))
  145. {
  146. for (ULONG l = 0; l < cElements; l++)
  147. VariantInit (&pVar [l]);
  148. // Retrieve the next cElements elements.
  149. for (l2 = 0; l2 < cElements; l2++)
  150. {
  151. ISWbemObject *pObject = NULL;
  152. if (SUCCEEDED(hr = m_pEnumObject->Next (INFINITE, &pObject)))
  153. {
  154. if (NULL == pObject)
  155. {
  156. break;
  157. }
  158. else
  159. {
  160. // Set the object into the variant array; note that pObject
  161. // has been addref'd as a result of the Next() call above
  162. pVar[l2].vt = VT_DISPATCH;
  163. pVar[l2].pdispVal = pObject;
  164. }
  165. }
  166. else
  167. break;
  168. }
  169. if (NULL != pcElementFetched)
  170. *pcElementFetched = l2;
  171. SetWbemError (m_pEnumObject->GetSWbemServices ());
  172. }
  173. if (FAILED(hr))
  174. return hr;
  175. return (l2 < cElements) ? S_FALSE : S_OK;
  176. }
  177. //***************************************************************************
  178. //
  179. // SCODE CEnumVar::Clone
  180. //
  181. // DESCRIPTION:
  182. //
  183. // Create a copy of this enumeration
  184. //
  185. // PARAMETERS:
  186. //
  187. // ppEnum on successful return addresses the clone
  188. //
  189. // RETURN VALUES:
  190. //
  191. // WBEM_S_NO_ERROR success
  192. // WBEM_E_FAILED otherwise
  193. //
  194. //***************************************************************************
  195. HRESULT CEnumVar::Clone (
  196. IEnumVARIANT **ppEnum
  197. )
  198. {
  199. HRESULT hr = E_FAIL;
  200. if (NULL != ppEnum)
  201. {
  202. *ppEnum = NULL;
  203. if (m_pEnumObject)
  204. {
  205. CSWbemObjectSet *pEnum = NULL;
  206. if (WBEM_S_NO_ERROR == (hr = m_pEnumObject->CloneObjectSet (&pEnum)))
  207. {
  208. CEnumVar *pEnumVar = new CEnumVar (pEnum);
  209. if (!pEnumVar)
  210. hr = WBEM_E_OUT_OF_MEMORY;
  211. else if (FAILED(hr = pEnumVar->QueryInterface (IID_IEnumVARIANT, (PPVOID) ppEnum)))
  212. delete pEnumVar;
  213. pEnum->Release ();
  214. }
  215. SetWbemError (m_pEnumObject->GetSWbemServices ());
  216. }
  217. else
  218. {
  219. CEnumVar *pEnumVar = new CEnumVar;
  220. if (!pEnumVar)
  221. hr = WBEM_E_OUT_OF_MEMORY;
  222. else if (FAILED(hr = pEnumVar->QueryInterface (IID_IEnumVARIANT, (PPVOID) ppEnum)))
  223. delete pEnumVar;
  224. }
  225. }
  226. return hr;
  227. }
  228. //***************************************************************************
  229. //
  230. // SCODE CEnumVar::Skip
  231. //
  232. // DESCRIPTION:
  233. //
  234. // Create a copy of this enumeration
  235. //
  236. // PARAMETERS:
  237. //
  238. // ppEnum on successful return addresses the clone
  239. //
  240. // RETURN VALUES:
  241. //
  242. // S_OK success
  243. // S_FALSE end of sequence reached prematurely
  244. //
  245. //***************************************************************************
  246. HRESULT CEnumVar::Skip(
  247. ULONG cElements
  248. )
  249. {
  250. HRESULT hr = S_FALSE;
  251. if (m_pEnumObject)
  252. {
  253. hr = m_pEnumObject->Skip (cElements, INFINITE);
  254. SetWbemError (m_pEnumObject->GetSWbemServices ());
  255. }
  256. return hr;
  257. }