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.

123 lines
2.2 KiB

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