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.

361 lines
7.0 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: object.cxx
  7. //
  8. // Contents: Windows NT 3.5 Enumerator Code
  9. //
  10. // History:
  11. //----------------------------------------------------------------------------
  12. #include "nds.hxx"
  13. #pragma hdrstop
  14. ObjectTypeList::ObjectTypeList()
  15. {
  16. _pObjList = NULL;
  17. _dwCurrentIndex = 0;
  18. _dwMaxElements = 0;
  19. _dwUBound = 0;
  20. _dwLBound = 0;
  21. }
  22. HRESULT
  23. ObjectTypeList::CreateObjectTypeList(
  24. VARIANT vFilter,
  25. ObjectTypeList ** ppObjectTypeList
  26. )
  27. {
  28. ObjectTypeList * pObjectTypeList = NULL;
  29. HRESULT hr = S_OK;
  30. pObjectTypeList = new ObjectTypeList;
  31. if (!pObjectTypeList) {
  32. hr = E_OUTOFMEMORY;
  33. BAIL_ON_FAILURE(hr);
  34. }
  35. hr = BuildObjectArray(
  36. vFilter,
  37. &pObjectTypeList->_pObjList,
  38. &pObjectTypeList->_dwMaxElements
  39. );
  40. if (FAILED(hr)) {
  41. hr = BuildDefaultObjectArray(
  42. gpFilters,
  43. gdwMaxFilters,
  44. &pObjectTypeList->_pObjList,
  45. &pObjectTypeList->_dwMaxElements
  46. );
  47. BAIL_ON_FAILURE(hr);
  48. }
  49. hr = SafeArrayGetUBound(
  50. pObjectTypeList->_pObjList,
  51. 1,
  52. (long FAR *)&pObjectTypeList->_dwUBound
  53. );
  54. BAIL_ON_FAILURE(hr);
  55. hr = SafeArrayGetLBound(
  56. pObjectTypeList->_pObjList,
  57. 1,
  58. (long FAR *)&pObjectTypeList->_dwLBound
  59. );
  60. BAIL_ON_FAILURE(hr);
  61. pObjectTypeList->_dwCurrentIndex = pObjectTypeList->_dwLBound;
  62. *ppObjectTypeList = pObjectTypeList;
  63. RRETURN(S_OK);
  64. error:
  65. if (pObjectTypeList) {
  66. delete pObjectTypeList;
  67. }
  68. RRETURN(hr);
  69. }
  70. ObjectTypeList::~ObjectTypeList()
  71. {
  72. HRESULT hr = S_OK;
  73. if (_pObjList) {
  74. hr = SafeArrayDestroy(_pObjList);
  75. }
  76. }
  77. HRESULT
  78. ObjectTypeList::GetCurrentObject(
  79. PDWORD pdwObject
  80. )
  81. {
  82. HRESULT hr = S_OK;
  83. if (_dwCurrentIndex > _dwUBound) {
  84. return(E_FAIL);
  85. }
  86. hr = SafeArrayGetElement(
  87. _pObjList,
  88. (long FAR *)&_dwCurrentIndex,
  89. (void *)pdwObject
  90. );
  91. RRETURN(hr);
  92. }
  93. HRESULT
  94. ObjectTypeList::Next()
  95. {
  96. HRESULT hr = S_OK;
  97. _dwCurrentIndex++;
  98. if (_dwCurrentIndex > _dwUBound) {
  99. return(E_FAIL);
  100. }
  101. return(hr);
  102. }
  103. HRESULT
  104. ObjectTypeList::Reset()
  105. {
  106. HRESULT hr = S_OK;
  107. _dwCurrentIndex = _dwLBound;
  108. return(hr);
  109. }
  110. HRESULT
  111. IsValidFilter(
  112. LPWSTR ObjectName,
  113. DWORD *pdwFilterId,
  114. PFILTERS pFilters,
  115. DWORD dwMaxFilters
  116. )
  117. {
  118. DWORD i = 0;
  119. for (i = 0; i < dwMaxFilters; i++) {
  120. if (!_wcsicmp(ObjectName, (pFilters + i)->szObjectName)) {
  121. *pdwFilterId = (pFilters + i)->dwFilterId;
  122. RRETURN(S_OK);
  123. }
  124. }
  125. *pdwFilterId = 0;
  126. RRETURN(E_FAIL);
  127. }
  128. HRESULT
  129. BuildDefaultObjectArray(
  130. PFILTERS pFilters,
  131. DWORD dwMaxFilters,
  132. SAFEARRAY ** ppFilter,
  133. DWORD * pdwNumElements
  134. )
  135. {
  136. DWORD i;
  137. HRESULT hr = S_OK;
  138. SAFEARRAYBOUND sabNewArray;
  139. SAFEARRAY * pFilter = NULL;
  140. sabNewArray.cElements = dwMaxFilters;
  141. sabNewArray.lLbound = 0;
  142. pFilter = SafeArrayCreate(
  143. VT_I4,
  144. 1,
  145. &sabNewArray
  146. );
  147. if (!pFilter){
  148. hr = E_OUTOFMEMORY;
  149. BAIL_ON_FAILURE(hr);
  150. }
  151. for (i = 0; i < dwMaxFilters; i++) {
  152. hr = SafeArrayPutElement(
  153. pFilter,
  154. (long *)&i,
  155. (void *)&((pFilters + i)->dwFilterId)
  156. );
  157. BAIL_ON_FAILURE(hr);
  158. }
  159. *ppFilter = pFilter;
  160. *pdwNumElements = dwMaxFilters;
  161. RRETURN(S_OK);
  162. error:
  163. if (pFilter) {
  164. SafeArrayDestroy(pFilter);
  165. }
  166. *ppFilter = NULL;
  167. *pdwNumElements = 0;
  168. RRETURN(hr);
  169. }
  170. HRESULT
  171. BuildObjectArray(
  172. VARIANT var,
  173. SAFEARRAY ** ppFilter,
  174. DWORD * pdwNumElements
  175. )
  176. {
  177. LONG uDestCount = 0;
  178. LONG dwSLBound = 0;
  179. LONG dwSUBound = 0;
  180. VARIANT v;
  181. VARIANT varDest;
  182. LONG i;
  183. HRESULT hr = S_OK;
  184. SAFEARRAYBOUND sabNewArray;
  185. DWORD dwFilterId;
  186. SAFEARRAY * pFilter = NULL;
  187. if(!((V_VT(&var) & VT_VARIANT) && V_ISARRAY(&var))) {
  188. RRETURN(E_FAIL);
  189. }
  190. //
  191. // Check that there is only one dimension in this array
  192. //
  193. if ((V_ARRAY(&var))->cDims != 1) {
  194. hr = E_FAIL;
  195. BAIL_ON_FAILURE(hr);
  196. }
  197. //
  198. // Check that there is atleast one element in this array
  199. //
  200. if ((V_ARRAY(&var))->rgsabound[0].cElements == 0){
  201. hr = E_FAIL;
  202. BAIL_ON_FAILURE(hr);
  203. }
  204. //
  205. // We know that this is a valid single dimension array
  206. //
  207. hr = SafeArrayGetLBound(V_ARRAY(&var),
  208. 1,
  209. (long FAR *)&dwSLBound
  210. );
  211. BAIL_ON_FAILURE(hr);
  212. hr = SafeArrayGetUBound(V_ARRAY(&var),
  213. 1,
  214. (long FAR *)&dwSUBound
  215. );
  216. BAIL_ON_FAILURE(hr);
  217. sabNewArray.cElements = dwSUBound - dwSLBound + 1;
  218. sabNewArray.lLbound = dwSLBound;
  219. pFilter = SafeArrayCreate(
  220. VT_I4,
  221. 1,
  222. &sabNewArray
  223. );
  224. if (!pFilter) {
  225. hr = E_OUTOFMEMORY;
  226. BAIL_ON_FAILURE(hr);
  227. }
  228. for (i = dwSLBound; i <= dwSUBound; i++) {
  229. VariantInit(&v);
  230. hr = SafeArrayGetElement(V_ARRAY(&var),
  231. (long FAR *)&i,
  232. &v
  233. );
  234. if (FAILED(hr)) {
  235. continue;
  236. }
  237. hr = IsValidFilter(
  238. V_BSTR(&v),
  239. &dwFilterId,
  240. gpFilters,
  241. gdwMaxFilters
  242. );
  243. if (FAILED(hr)) {
  244. VariantClear(&v);
  245. continue;
  246. }
  247. VariantClear(&v);
  248. hr = SafeArrayPutElement(
  249. pFilter,
  250. (long*)&uDestCount,
  251. (void *)&dwFilterId
  252. );
  253. if(FAILED(hr)){
  254. continue;
  255. }
  256. uDestCount++;
  257. }
  258. //
  259. // There was nothing of value that could be retrieved from the
  260. // filter.
  261. //
  262. if (!uDestCount ) {
  263. hr = E_FAIL;
  264. BAIL_ON_FAILURE(hr);
  265. }
  266. *pdwNumElements = uDestCount;
  267. *ppFilter = pFilter;
  268. RRETURN(S_OK);
  269. error:
  270. if (pFilter) {
  271. SafeArrayDestroy(pFilter);
  272. }
  273. *ppFilter = NULL;
  274. *pdwNumElements = 0;
  275. RRETURN(hr);
  276. }