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.

306 lines
6.1 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. LONG cRef = InterlockedDecrement(&m_cRef);
  82. if (0 != cRef)
  83. {
  84. _ASSERT(cRef > 0);
  85. return cRef;
  86. }
  87. delete this;
  88. return 0;
  89. }
  90. //***************************************************************************
  91. //
  92. // SCODE CEnumVar::Reset
  93. //
  94. // DESCRIPTION:
  95. //
  96. // Reset the enumeration
  97. //
  98. // PARAMETERS:
  99. //
  100. // RETURN VALUES:
  101. //
  102. // S_OK success
  103. // S_FALSE otherwise
  104. //
  105. //***************************************************************************
  106. HRESULT CEnumVar::Reset ()
  107. {
  108. HRESULT hr = S_FALSE;
  109. if (m_pEnumObject)
  110. {
  111. if (WBEM_S_NO_ERROR == m_pEnumObject->Reset ())
  112. hr = S_OK;
  113. SetWbemError (m_pEnumObject->GetSWbemServices ());
  114. }
  115. return hr;
  116. }
  117. //***************************************************************************
  118. //
  119. // SCODE CEnumVar::Next
  120. //
  121. // DESCRIPTION:
  122. //
  123. // Get the next object in the enumeration
  124. //
  125. // PARAMETERS:
  126. //
  127. // lTimeout Number of ms to wait for object (or WBEM_INFINITE for
  128. // indefinite)
  129. // ppObject On return may contain the next element (if any)
  130. //
  131. // RETURN VALUES:
  132. //
  133. // S_OK success
  134. // S_FALSE not all elements could be returned
  135. //
  136. //***************************************************************************
  137. HRESULT CEnumVar::Next (
  138. ULONG cElements,
  139. VARIANT FAR* pVar,
  140. ULONG FAR* pcElementFetched
  141. )
  142. {
  143. HRESULT hr = S_OK;
  144. ULONG l2 = 0;
  145. if (NULL != pcElementFetched)
  146. *pcElementFetched = 0;
  147. if ((NULL != pVar) && (m_pEnumObject))
  148. {
  149. for (ULONG l = 0; l < cElements; l++)
  150. VariantInit (&pVar [l]);
  151. // Retrieve the next cElements elements.
  152. for (l2 = 0; l2 < cElements; l2++)
  153. {
  154. ISWbemObject *pObject = NULL;
  155. if (SUCCEEDED(hr = m_pEnumObject->Next (INFINITE, &pObject)))
  156. {
  157. if (NULL == pObject)
  158. {
  159. break;
  160. }
  161. else
  162. {
  163. // Set the object into the variant array; note that pObject
  164. // has been addref'd as a result of the Next() call above
  165. pVar[l2].vt = VT_DISPATCH;
  166. pVar[l2].pdispVal = pObject;
  167. }
  168. }
  169. else
  170. break;
  171. }
  172. if (NULL != pcElementFetched)
  173. *pcElementFetched = l2;
  174. SetWbemError (m_pEnumObject->GetSWbemServices ());
  175. }
  176. if (FAILED(hr))
  177. return hr;
  178. return (l2 < cElements) ? S_FALSE : S_OK;
  179. }
  180. //***************************************************************************
  181. //
  182. // SCODE CEnumVar::Clone
  183. //
  184. // DESCRIPTION:
  185. //
  186. // Create a copy of this enumeration
  187. //
  188. // PARAMETERS:
  189. //
  190. // ppEnum on successful return addresses the clone
  191. //
  192. // RETURN VALUES:
  193. //
  194. // WBEM_S_NO_ERROR success
  195. // WBEM_E_FAILED otherwise
  196. //
  197. //***************************************************************************
  198. HRESULT CEnumVar::Clone (
  199. IEnumVARIANT **ppEnum
  200. )
  201. {
  202. HRESULT hr = E_FAIL;
  203. if (NULL != ppEnum)
  204. {
  205. *ppEnum = NULL;
  206. if (m_pEnumObject)
  207. {
  208. CSWbemObjectSet *pEnum = NULL;
  209. if (WBEM_S_NO_ERROR == (hr = m_pEnumObject->CloneObjectSet (&pEnum)))
  210. {
  211. CEnumVar *pEnumVar = new CEnumVar (pEnum);
  212. if (!pEnumVar)
  213. hr = WBEM_E_OUT_OF_MEMORY;
  214. else if (FAILED(hr = pEnumVar->QueryInterface (IID_IEnumVARIANT, (PPVOID) ppEnum)))
  215. delete pEnumVar;
  216. pEnum->Release ();
  217. }
  218. SetWbemError (m_pEnumObject->GetSWbemServices ());
  219. }
  220. else
  221. {
  222. CEnumVar *pEnumVar = new CEnumVar;
  223. if (!pEnumVar)
  224. hr = WBEM_E_OUT_OF_MEMORY;
  225. else if (FAILED(hr = pEnumVar->QueryInterface (IID_IEnumVARIANT, (PPVOID) ppEnum)))
  226. delete pEnumVar;
  227. }
  228. }
  229. return hr;
  230. }
  231. //***************************************************************************
  232. //
  233. // SCODE CEnumVar::Skip
  234. //
  235. // DESCRIPTION:
  236. //
  237. // Create a copy of this enumeration
  238. //
  239. // PARAMETERS:
  240. //
  241. // ppEnum on successful return addresses the clone
  242. //
  243. // RETURN VALUES:
  244. //
  245. // S_OK success
  246. // S_FALSE end of sequence reached prematurely
  247. //
  248. //***************************************************************************
  249. HRESULT CEnumVar::Skip(
  250. ULONG cElements
  251. )
  252. {
  253. HRESULT hr = S_FALSE;
  254. if (m_pEnumObject)
  255. {
  256. hr = m_pEnumObject->Skip (cElements, INFINITE);
  257. SetWbemError (m_pEnumObject->GetSWbemServices ());
  258. }
  259. return hr;
  260. }