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.

219 lines
4.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // dsobj.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines the class DataStoreObject.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/12/2000 Original version.
  16. // 04/17/2000 Must pass BSTRs to data store objects.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #include <proxypch.h>
  20. #include <dsobj.h>
  21. using _com_util::CheckError;
  22. DataStoreObject::DataStoreObject() throw ()
  23. {
  24. }
  25. DataStoreObject::DataStoreObject(IUnknown* pUnk, PCWSTR path)
  26. : object(pUnk), container(pUnk)
  27. {
  28. if (path)
  29. {
  30. while (!empty() && *path != L'\0')
  31. {
  32. getChild(path, *this);
  33. path += wcslen(path) + 1;
  34. }
  35. }
  36. }
  37. DataStoreObject::~DataStoreObject() throw ()
  38. {
  39. }
  40. void DataStoreObject::getValue(PCWSTR name, BSTR* value)
  41. {
  42. VARIANT v;
  43. getValue(name, VT_BSTR, &v, true);
  44. *value = V_BSTR(&v);
  45. }
  46. void DataStoreObject::getValue(PCWSTR name, BSTR* value, BSTR defaultValue)
  47. {
  48. VARIANT v;
  49. if (getValue(name, VT_BSTR, &v, false))
  50. {
  51. *value = V_BSTR(&v);
  52. }
  53. else
  54. {
  55. *value = SysAllocString(defaultValue);
  56. if (!*value && defaultValue) { _com_issue_error(E_OUTOFMEMORY); }
  57. }
  58. }
  59. void DataStoreObject::getValue(PCWSTR name, ULONG* value)
  60. {
  61. VARIANT v;
  62. getValue(name, VT_I4, &v, true);
  63. *value = V_UI4(&v);
  64. }
  65. void DataStoreObject::getValue(PCWSTR name, ULONG* value, ULONG defaultValue)
  66. {
  67. VARIANT v;
  68. if (getValue(name, VT_I4, &v, false))
  69. {
  70. *value = V_UI4(&v);
  71. }
  72. else
  73. {
  74. *value = defaultValue;
  75. }
  76. }
  77. void DataStoreObject::getValue(PCWSTR name, bool* value)
  78. {
  79. VARIANT v;
  80. getValue(name, VT_BOOL, &v, true);
  81. *value = V_BOOL(&v) != 0;
  82. }
  83. void DataStoreObject::getValue(PCWSTR name, bool* value, bool defaultValue)
  84. {
  85. VARIANT v;
  86. if (getValue(name, VT_BOOL, &v, false))
  87. {
  88. *value = V_BOOL(&v) != 0;
  89. }
  90. else
  91. {
  92. *value = defaultValue;
  93. }
  94. }
  95. LONG DataStoreObject::numChildren()
  96. {
  97. LONG value;
  98. CheckError(container->get_Count(&value));
  99. return value;
  100. }
  101. bool DataStoreObject::nextChild(DataStoreObject& obj)
  102. {
  103. if (!hasChildren()) { return false; }
  104. _variant_t element;
  105. ULONG fetched;
  106. HRESULT hr = children->Next(1, &element, &fetched);
  107. CheckError(hr);
  108. if (hr != S_OK || !fetched) { return false; }
  109. obj.attach(IDataStoreObjectPtr(element));
  110. return true;
  111. }
  112. bool DataStoreObject::empty() const throw ()
  113. {
  114. return object == NULL;
  115. }
  116. void DataStoreObject::attach(IDataStoreObject* obj)
  117. {
  118. object = obj;
  119. container = object;
  120. children = NULL;
  121. }
  122. bool DataStoreObject::getChild(PCWSTR name, DataStoreObject& obj)
  123. {
  124. // Convert name to a BSTR.
  125. CComBSTR bstrName(name);
  126. if (!bstrName) { _com_issue_error(E_OUTOFMEMORY); }
  127. IDataStoreObjectPtr child;
  128. HRESULT hr = container->Item(bstrName, &child);
  129. if (FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
  130. {
  131. _com_issue_error(hr);
  132. }
  133. obj.attach(child);
  134. return !obj.empty();
  135. }
  136. bool DataStoreObject::hasChildren()
  137. {
  138. if (children == NULL)
  139. {
  140. IUnknownPtr unk;
  141. CheckError(container->get__NewEnum(&unk));
  142. children = unk;
  143. }
  144. return children != NULL;
  145. }
  146. bool DataStoreObject::getValue(
  147. PCWSTR name,
  148. VARTYPE vt,
  149. VARIANT* value,
  150. bool mandatory
  151. )
  152. {
  153. bool retval = false;
  154. if (object == NULL)
  155. {
  156. if (mandatory) { _com_issue_error(DISP_E_MEMBERNOTFOUND); }
  157. }
  158. else
  159. {
  160. // Convert name to a BSTR.
  161. CComBSTR bstrName(name);
  162. if (!bstrName) { _com_issue_error(E_OUTOFMEMORY); }
  163. HRESULT hr = object->GetValue(bstrName, value);
  164. if (SUCCEEDED(hr))
  165. {
  166. if (V_VT(value) != vt)
  167. {
  168. VariantClear(value);
  169. _com_issue_error(DISP_E_TYPEMISMATCH);
  170. }
  171. retval = true;
  172. }
  173. else if (hr == DISP_E_MEMBERNOTFOUND)
  174. {
  175. if (mandatory)
  176. {
  177. _com_issue_error(DISP_E_MEMBERNOTFOUND);
  178. }
  179. }
  180. else
  181. {
  182. _com_issue_error(hr);
  183. }
  184. }
  185. return retval;
  186. }