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.

120 lines
2.2 KiB

  1. // DevicesEnum.cpp : Implementation of CDevCon2App and DLL registration.
  2. #include "stdafx.h"
  3. #include "DevCon2.h"
  4. #include "Device.h"
  5. #include "Devices.h"
  6. #include "DevicesEnum.h"
  7. /////////////////////////////////////////////////////////////////////////////
  8. //
  9. CDevicesEnum::~CDevicesEnum()
  10. {
  11. DWORD c;
  12. if(pDevices) {
  13. for(c=0;c<Count;c++) {
  14. pDevices[c]->Release();
  15. }
  16. delete [] pDevices;
  17. }
  18. }
  19. HRESULT CDevicesEnum::Next(
  20. ULONG celt,
  21. VARIANT * rgVar,
  22. ULONG * pCeltFetched
  23. )
  24. {
  25. ULONG fetched;
  26. CDevice *pDev;
  27. if(pCeltFetched) {
  28. *pCeltFetched = 0;
  29. }
  30. for(fetched = 0; fetched<celt && Position<Count ; fetched++,Position++) {
  31. VariantInit(&rgVar[fetched]);
  32. pDev = pDevices[Position];
  33. pDev->AddRef();
  34. V_VT(&rgVar[fetched]) = VT_DISPATCH;
  35. V_DISPATCH(&rgVar[fetched]) = pDev;
  36. }
  37. if(pCeltFetched) {
  38. *pCeltFetched = fetched;
  39. }
  40. return (fetched<celt) ? S_FALSE : S_OK;
  41. }
  42. HRESULT CDevicesEnum::Skip(
  43. ULONG celt
  44. )
  45. {
  46. DWORD remaining = Count-Position;
  47. if(remaining<celt) {
  48. Position = Count;
  49. return S_FALSE;
  50. } else {
  51. Position += (DWORD)celt;
  52. return S_OK;
  53. }
  54. }
  55. HRESULT CDevicesEnum::Reset(
  56. )
  57. {
  58. Position = 0;
  59. return S_OK;
  60. }
  61. HRESULT CDevicesEnum::Clone(
  62. IEnumVARIANT ** ppEnum
  63. )
  64. {
  65. *ppEnum = NULL;
  66. HRESULT hr;
  67. CComObject<CDevicesEnum> *pEnum = NULL;
  68. hr = CComObject<CDevicesEnum>::CreateInstance(&pEnum);
  69. if(FAILED(hr)) {
  70. return hr;
  71. }
  72. if(!pEnum) {
  73. return E_OUTOFMEMORY;
  74. }
  75. if(!pEnum->CopyDevices(pDevices,Count)) {
  76. delete pEnum;
  77. return E_OUTOFMEMORY;
  78. }
  79. pEnum->Position = Position;
  80. pEnum->AddRef();
  81. *ppEnum = pEnum;
  82. return S_OK;
  83. }
  84. BOOL CDevicesEnum::CopyDevices(CDevice **pArray, DWORD NewCount)
  85. {
  86. DWORD c;
  87. if(pDevices) {
  88. delete [] pDevices;
  89. pDevices = NULL;
  90. }
  91. Count = 0;
  92. Position = 0;
  93. pDevices = new CDevice*[NewCount];
  94. if(!pDevices) {
  95. return FALSE;
  96. }
  97. for(c=0;c<NewCount;c++) {
  98. pArray[c]->AddRef();
  99. pDevices[c] = pArray[c];
  100. if(!pDevices[c]) {
  101. Count = c;
  102. return FALSE;
  103. }
  104. }
  105. Count = NewCount;
  106. return TRUE;
  107. }