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.

152 lines
3.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // enumerators.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines the class Enumerators.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/25/1999 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <windows.h>
  19. #include <enumerators.h>
  20. #include <iasdb.h>
  21. #include <simtable.h>
  22. Enumerators::~Enumerators() throw ()
  23. {
  24. for ( ; next != end; ++next)
  25. {
  26. SysFreeString(next->name);
  27. }
  28. CoTaskMemFree(begin);
  29. }
  30. HRESULT Enumerators::getEnumerators(
  31. LONG id,
  32. VARIANT* pNames,
  33. VARIANT* pValues
  34. ) throw ()
  35. {
  36. VariantInit(pNames);
  37. VariantInit(pValues);
  38. HRESULT hr = getEnumerators(
  39. id,
  40. &V_ARRAY(pNames),
  41. &V_ARRAY(pValues)
  42. );
  43. if (SUCCEEDED(hr) && V_ARRAY(pNames))
  44. {
  45. V_VT(pNames) = VT_ARRAY | VT_VARIANT;
  46. V_VT(pValues) = VT_ARRAY | VT_VARIANT;
  47. }
  48. return hr;
  49. }
  50. HRESULT Enumerators::getEnumerators(
  51. LONG id,
  52. LPSAFEARRAY* pNames,
  53. LPSAFEARRAY* pValues
  54. ) throw ()
  55. {
  56. // Initialize the out parameters.
  57. *pNames = *pValues = NULL;
  58. // If this is less than next, it must not be an enumerator.
  59. if (id < next->enumerates) { return S_OK; }
  60. // If this is greater than next, we skipped one.
  61. if (id > next->enumerates) { return E_INVALIDARG; }
  62. // Find the range of enumerations for this id.
  63. Enumeration* last = next;
  64. do { ++last; } while (last->enumerates == id);
  65. ULONG num = (ULONG)(last - next);
  66. // Create a SAFEARRAY to hold the names.
  67. *pNames = SafeArrayCreateVector(VT_VARIANT, 0, num);
  68. if (*pNames == NULL) { return E_OUTOFMEMORY; }
  69. // Create a SAFEARRAY to hold the values.
  70. *pValues = SafeArrayCreateVector(VT_VARIANT, 0, num);
  71. if (*pValues == NULL)
  72. {
  73. SafeArrayDestroy(*pNames);
  74. *pNames = NULL;
  75. return E_OUTOFMEMORY;
  76. }
  77. // Fill in the VARIANTs in the array.
  78. VARIANT* name = (VARIANT*)(*pNames)->pvData;
  79. VARIANT* value = (VARIANT*)(*pValues)->pvData;
  80. for ( ; next != last; ++next, ++name, ++value)
  81. {
  82. VariantInit(name);
  83. V_VT(name) = VT_BSTR;
  84. V_BSTR(name) = next->name;
  85. VariantInit(value);
  86. V_VT(value) = VT_I4;
  87. V_I4(value) = next->value;
  88. }
  89. return S_OK;
  90. }
  91. HRESULT Enumerators::initialize(IUnknown* session) throw ()
  92. {
  93. HRESULT hr;
  94. LONG count;
  95. hr = IASExecuteSQLFunction(
  96. session,
  97. L"SELECT Count(*) AS X From Enumerators;",
  98. &count
  99. );
  100. if (FAILED(hr)) { return hr; }
  101. CComPtr<IRowset> rowset;
  102. hr = IASExecuteSQLCommand(
  103. session,
  104. L"SELECT Name, Enumerates, Value FROM Enumerators "
  105. L"ORDER BY Enumerates, Value;",
  106. &rowset
  107. );
  108. if (FAILED(hr)) { return hr; }
  109. CSimpleTable table;
  110. hr = table.Attach(rowset);
  111. if (FAILED(hr)) { return hr; }
  112. // Allocate one extra slot for the sentinel.
  113. begin = (Enumeration*)CoTaskMemAlloc((count + 1) * sizeof(Enumeration));
  114. if (!begin) { return E_OUTOFMEMORY; }
  115. // Iterate through the rowset.
  116. for (end = begin; count-- && !table.MoveNext(); ++end)
  117. {
  118. end->enumerates = *(PLONG)table.GetValue(2);
  119. end->value = *(PLONG)table.GetValue(3);
  120. end->name = SysAllocString((PCWSTR)table.GetValue(1));
  121. if (!end->name) { return E_OUTOFMEMORY; }
  122. }
  123. // Set the sentinel.
  124. end->enumerates = MAXLONG;
  125. // We start at the beginning.
  126. next = begin;
  127. return S_OK;
  128. }