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.

398 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. SessEnum.cpp
  5. Abstract:
  6. SessEnum.cpp : Implementation of CRemoteDesktopHelpSessionEnum
  7. Author:
  8. HueiWang 2/17/2000
  9. --*/
  10. #include "stdafx.h"
  11. #include "Sessmgr.h"
  12. #include "helpmgr.h"
  13. #include "helpsess.h"
  14. #include "SessEnum.h"
  15. /////////////////////////////////////////////////////////////////////////////
  16. //
  17. // CRemoteDesktopHelpSessionEnum
  18. //
  19. STDMETHODIMP
  20. CRemoteDesktopHelpSessionEnum::Next(
  21. IN ULONG uCount,
  22. IN OUT IRemoteDesktopHelpSession **ppIHelpSession,
  23. IN ULONG *pcFetched
  24. )
  25. /*++
  26. Routine Description:
  27. Retrieve number of items, refer to standard COM enumerator for detail.
  28. Parameters:
  29. uCount : Number of item to retrieves.
  30. ppIHelpSession : Pointer to buffer to receive items, buffer must be
  31. big enough to hold at least uCount items.
  32. pcFetched : actual number of item returned.
  33. Returns:
  34. E_POINTER Invalid input parameter
  35. S_FALSE No more item in the list or number items retrieve is less than uCount.
  36. S_OK Success.
  37. Error code from QueryInterface(), normally, internal error.
  38. Note:
  39. Code modified from ATL's IEnumOnSTLImpl::Next()
  40. --*/
  41. {
  42. if( ppIHelpSession == NULL || uCount != 1 && pcFetched == NULL )
  43. {
  44. return E_POINTER;
  45. }
  46. if( m_iter == m_pcollection.end() )
  47. {
  48. return S_FALSE;
  49. }
  50. ULONG nActual = 0;
  51. HRESULT hRes = S_OK;
  52. IRemoteDesktopHelpSession** pelt = ppIHelpSession;
  53. while( SUCCEEDED(hRes) && m_iter != m_pcollection.end() && nActual < uCount )
  54. {
  55. hRes = (*m_iter)->QueryInterface(
  56. IID_IRemoteDesktopHelpSession,
  57. reinterpret_cast<void **>(pelt)
  58. );
  59. if( FAILED(hRes) )
  60. {
  61. while( ppIHelpSession < pelt )
  62. {
  63. (*ppIHelpSession)->Release();
  64. ppIHelpSession++;
  65. }
  66. nActual = 0;
  67. }
  68. else
  69. {
  70. pelt++;
  71. m_iter++;
  72. nActual++;
  73. }
  74. }
  75. if( NULL != pcFetched )
  76. {
  77. *pcFetched = nActual;
  78. }
  79. if( SUCCEEDED(hRes) && (nActual < uCount) )
  80. {
  81. hRes = S_FALSE;
  82. }
  83. return hRes;
  84. }
  85. STDMETHODIMP
  86. CRemoteDesktopHelpSessionEnum::Reset()
  87. /*++
  88. Routine Description:
  89. Reset enumeration from beginning, refer to standard COM enumerator for detail.
  90. Parameters:
  91. None.
  92. Returns:
  93. S_OK
  94. --*/
  95. {
  96. m_iter = m_pcollection.begin();
  97. return S_OK;
  98. }
  99. STDMETHODIMP
  100. CRemoteDesktopHelpSessionEnum::Skip(
  101. IN ULONG celt
  102. )
  103. /*++
  104. Routine Description:
  105. Skip number of items in the list and start enumeration from there.
  106. Parameters:
  107. celt : number of item to skip.
  108. Returns:
  109. S_OK or S_FALSE if end of the list reached.
  110. Note:
  111. Refer to standard COM enumerator for detail.
  112. --*/
  113. {
  114. //
  115. // Code copied from ATL's IEnumOnSTLImpl::Skip()
  116. //
  117. HRESULT hr = S_OK;
  118. while (celt--)
  119. {
  120. if (m_iter != m_pcollection.end())
  121. {
  122. m_iter++;
  123. }
  124. else
  125. {
  126. hr = S_FALSE;
  127. break;
  128. }
  129. }
  130. return hr;
  131. }
  132. STDMETHODIMP
  133. CRemoteDesktopHelpSessionEnum::Clone(
  134. OUT IRemoteDesktopHelpSessionEnum **ppIEnum
  135. )
  136. /*++
  137. Routine Description:
  138. Make a copy of this enumerator.
  139. Parameters:
  140. ppIEnum : Return a new enumerator
  141. Returns:
  142. S_OK or E_OUTOFMEMORY.
  143. Note:
  144. Refer to standard COM enumerator for detail.
  145. --*/
  146. {
  147. HRESULT hRes;
  148. CComObject<CRemoteDesktopHelpSessionEnum>* p = NULL;
  149. hRes = CComObject< CRemoteDesktopHelpSessionEnum >::CreateInstance( &p );
  150. if( SUCCEEDED(hRes) )
  151. {
  152. HelpSessionObjList::iterator it = m_pcollection.begin();
  153. // copy item to new enumerator
  154. for( ; it != m_pcollection.end() && SUCCEEDED(hRes); it++ )
  155. {
  156. // add will addref() to HelpSession object
  157. hRes = p->Add( *it );
  158. }
  159. p->Reset();
  160. hRes = p->QueryInterface( IID_IRemoteDesktopHelpSessionEnum, reinterpret_cast<void **>(ppIEnum) );
  161. }
  162. return hRes;
  163. }
  164. STDMETHODIMP
  165. CRemoteDesktopHelpSessionEnum::get_Count(
  166. OUT long *pVal
  167. )
  168. /*++
  169. Routine Description:
  170. Return number of item in the list.
  171. Parameters:
  172. pVal : return number of item
  173. Returns:
  174. S_OK
  175. Note:
  176. Refer to standard COM enumerator for detail.
  177. --*/
  178. {
  179. *pVal = m_pcollection.size();
  180. return S_OK;
  181. }
  182. STDMETHODIMP
  183. CRemoteDesktopHelpSessionEnum::get_Item(
  184. IN long Index,
  185. OUT VARIANT *pVal
  186. )
  187. /*++
  188. Routine Description:
  189. Given an index, return item in the list.
  190. Parameters
  191. Index : item index.
  192. pVal : Return (VT_DISPATCH) IRemoteDesktopHelpSession interface.
  193. Returns:
  194. E_POINTER, E_FAIL, or S_OK.
  195. Note:
  196. Refer to standard COM enumerator for detail, code
  197. copied from ICollectionOnSTLImpl::get_Item()
  198. --*/
  199. {
  200. //Index is 1-based
  201. if (pVal == NULL)
  202. {
  203. return E_POINTER;
  204. }
  205. HRESULT hr = E_FAIL;
  206. Index--;
  207. HelpSessionObjList::iterator iter = m_pcollection.begin();
  208. while (iter != m_pcollection.end() && Index > 0)
  209. {
  210. iter++;
  211. Index--;
  212. }
  213. if (iter != m_pcollection.end())
  214. {
  215. VariantInit(pVal);
  216. pVal->vt = VT_DISPATCH;
  217. hr = (*iter)->QueryInterface( IID_IDispatch, (void **)&(pVal->pdispVal) );
  218. }
  219. return hr;
  220. }
  221. STDMETHODIMP
  222. CRemoteDesktopHelpSessionEnum::get__NewEnum(
  223. LPUNKNOWN *pVal
  224. )
  225. /*++
  226. Routine Description:
  227. Identical to Clone() except this routine exists to support
  228. VB scripting.
  229. Parameters
  230. pVal : Return an IRemoteDesktopHelpSessionEnum interface.
  231. Returns:
  232. S_OK, E_OUTOFMEMORY.
  233. Note:
  234. Refer to standard COM enumerator for detail, code copied
  235. from Alhen's WMI provider.
  236. --*/
  237. {
  238. HRESULT hRes;
  239. HelpSessionObjList::iterator it;
  240. VARIANT * rgVariant = NULL;
  241. typedef CComObject< CComEnum< IEnumVARIANT,&IID_IEnumVARIANT ,VARIANT, _Copy< VARIANT > > > CComEnumSessions;
  242. CComObject< CComEnumSessions > *pEnum = NULL;
  243. hRes = CComObject< CComEnumSessions >::CreateInstance( &pEnum );
  244. if( SUCCEEDED(hRes) )
  245. {
  246. rgVariant = new VARIANT [ m_pcollection.size() ];
  247. if( NULL != rgVariant )
  248. {
  249. VARIANT * pVariant = rgVariant;
  250. // package a list of dispinterfaces to requester
  251. for( it = m_pcollection.begin(); it != m_pcollection.end() && SUCCEEDED(hRes); it++, pVariant++ )
  252. {
  253. pVariant->vt = VT_DISPATCH;
  254. hRes = (*it)->QueryInterface( IID_IDispatch, reinterpret_cast<void **>(&(pVariant->pdispVal)) );
  255. }
  256. if( SUCCEEDED (hRes) )
  257. {
  258. pEnum->AddRef();
  259. hRes = pEnum->Init( &rgVariant[0], &rgVariant[ m_pcollection.size() ] , NULL, AtlFlagTakeOwnership);
  260. if( SUCCEEDED( hRes ) )
  261. {
  262. hRes = pEnum->QueryInterface (
  263. IID_IUnknown,
  264. reinterpret_cast<void **>(pVal)
  265. );
  266. }
  267. pEnum->Release( );
  268. }
  269. }
  270. else
  271. {
  272. hRes = E_OUTOFMEMORY;
  273. }
  274. if( FAILED(hRes) )
  275. {
  276. pEnum->Release();
  277. if( NULL != rgVariant )
  278. {
  279. delete [] rgVariant;
  280. }
  281. }
  282. }
  283. return hRes;
  284. }