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.

178 lines
3.2 KiB

  1. #include "stdafx.h"
  2. #include "device.h"
  3. #include "devenum.h"
  4. //#include "findleak.h"
  5. //DECLARE_THIS_FILE;
  6. //
  7. // Initialization
  8. //
  9. CDeviceEnum::CDeviceEnum()
  10. {
  11. m_iCurItem = NULL;
  12. m_cItems = NULL;
  13. m_rgDevices = NULL;
  14. }
  15. HRESULT CDeviceEnum::Init( CComDevice **rgDevice, UINT cItems, UINT iCurItem )
  16. {
  17. HRESULT hr = S_OK;
  18. if( NULL == rgDevice || 0 == cItems || iCurItem > cItems )
  19. {
  20. return( E_INVALIDARG );
  21. }
  22. m_rgDevices = new CComDevice*[cItems];
  23. if( NULL == m_rgDevices )
  24. {
  25. hr = E_OUTOFMEMORY;
  26. }
  27. if( SUCCEEDED( hr ) )
  28. {
  29. m_iCurItem = iCurItem;
  30. m_cItems = cItems;
  31. while( cItems-- )
  32. {
  33. m_rgDevices[cItems] = rgDevice[cItems];
  34. if( m_rgDevices[cItems] )
  35. {
  36. m_rgDevices[cItems]->AddRef();
  37. }
  38. }
  39. }
  40. return( hr );
  41. }
  42. //
  43. // Destruction
  44. //
  45. void CDeviceEnum::FinalRelease()
  46. {
  47. UINT i = 0;
  48. if( m_rgDevices )
  49. {
  50. for( i = 0; i < m_cItems; i ++ )
  51. {
  52. if( m_rgDevices[i] )
  53. {
  54. m_rgDevices[i]->Release();
  55. }
  56. }
  57. delete [] m_rgDevices;
  58. }
  59. }
  60. //
  61. // IMDSPEnumDevice interface
  62. //
  63. STDMETHODIMP CDeviceEnum::Next( ULONG celt, IMDSPDevice ** ppDevice, ULONG *pceltFetched )
  64. {
  65. ULONG celtFetched = 0;
  66. HRESULT hr = S_OK;
  67. ULONG i;
  68. if( NULL == pceltFetched && celt != 1 )
  69. {
  70. return( E_INVALIDARG );
  71. }
  72. if( NULL == ppDevice )
  73. {
  74. return( E_POINTER );
  75. }
  76. for( i = 0; i < celt; i ++ )
  77. {
  78. ppDevice[i] = NULL;
  79. }
  80. while( celtFetched != celt )
  81. {
  82. if( m_iCurItem >= m_cItems )
  83. {
  84. hr = S_FALSE;
  85. break;
  86. }
  87. ppDevice[celtFetched] = m_rgDevices[m_iCurItem++];
  88. if( ppDevice[celtFetched] )
  89. {
  90. ppDevice[celtFetched++]->AddRef();
  91. }
  92. }
  93. if( NULL != pceltFetched )
  94. {
  95. *pceltFetched = celtFetched;
  96. }
  97. return( hr );
  98. }
  99. STDMETHODIMP CDeviceEnum::Skip( ULONG celt, ULONG *pceltFetched )
  100. {
  101. ULONG celtSkipped = 0;
  102. HRESULT hr = S_OK;
  103. if( celt + m_iCurItem >= m_cItems )
  104. {
  105. celtSkipped = m_cItems - m_iCurItem;
  106. m_iCurItem = m_cItems;
  107. hr = S_FALSE;
  108. }
  109. else
  110. {
  111. celtSkipped = celt;
  112. m_iCurItem += celt;
  113. }
  114. if( NULL != pceltFetched )
  115. {
  116. *pceltFetched = celtSkipped;
  117. }
  118. return( hr );
  119. }
  120. STDMETHODIMP CDeviceEnum::Reset( void )
  121. {
  122. m_iCurItem = 0;
  123. return( S_OK );
  124. }
  125. STDMETHODIMP CDeviceEnum::Clone( IMDSPEnumDevice ** ppEnumDevice )
  126. {
  127. CComEnumDevice *pNewEnum;
  128. CComPtr<IMDSPEnumDevice> spEnum;
  129. HRESULT hr = S_OK;
  130. if( SUCCEEDED(hr) )
  131. {
  132. hr = CComEnumDevice ::CreateInstance(&pNewEnum);
  133. spEnum = pNewEnum;
  134. }
  135. if( SUCCEEDED(hr) )
  136. {
  137. hr = pNewEnum->Init( m_rgDevices, m_cItems, m_iCurItem );
  138. }
  139. if( SUCCEEDED(hr) )
  140. {
  141. *ppEnumDevice = spEnum;
  142. spEnum.Detach();
  143. }
  144. return( S_OK );
  145. }