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.

483 lines
10 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: cenumdom.cxx
  7. //
  8. // Contents: NDS Object Enumeration Code
  9. //
  10. // CNDSTreeEnum::CNDSTreeEnum()
  11. // CNDSTreeEnum::CNDSTreeEnum
  12. // CNDSTreeEnum::EnumObjects
  13. // CNDSTreeEnum::EnumObjects
  14. //
  15. // History:
  16. //----------------------------------------------------------------------------
  17. #include "NDS.hxx"
  18. #pragma hdrstop
  19. //+---------------------------------------------------------------------------
  20. //
  21. // Function: CNDSEnumVariant::Create
  22. //
  23. // Synopsis:
  24. //
  25. // Arguments: [pCollection]
  26. // [ppEnumVariant]
  27. //
  28. // Returns: HRESULT
  29. //
  30. // Modifies:
  31. //
  32. // History: 01-30-95 krishnag Created.
  33. //
  34. //----------------------------------------------------------------------------
  35. HRESULT
  36. CNDSTreeEnum::Create(
  37. CNDSTreeEnum FAR* FAR* ppenumvariant,
  38. BSTR ADsPath,
  39. VARIANT var,
  40. CCredentials& Credentials
  41. )
  42. {
  43. HRESULT hr = NOERROR;
  44. CNDSTreeEnum FAR* penumvariant = NULL;
  45. WCHAR szObjectFullName[MAX_PATH];
  46. WCHAR szObjectClassName[MAX_PATH];
  47. LPWSTR pszTreeName = NULL, pszDn = NULL;
  48. DWORD dwModificationTime = 0L;
  49. DWORD dwNumberOfEntries = 0L;
  50. DWORD dwStatus = 0L;
  51. *ppenumvariant = NULL;
  52. penumvariant = new CNDSTreeEnum();
  53. if (!penumvariant) {
  54. hr = E_OUTOFMEMORY;
  55. BAIL_ON_FAILURE(hr);
  56. }
  57. hr = ADsAllocString( ADsPath, &penumvariant->_ADsPath);
  58. BAIL_ON_FAILURE(hr);
  59. hr = BuildNDSFilterArray(
  60. var,
  61. (LPBYTE *)&penumvariant->_pNdsFilterList
  62. );
  63. if (FAILED(hr)) {
  64. penumvariant->_pNdsFilterList = NULL;
  65. }
  66. /*
  67. hr = ObjectTypeList::CreateObjectTypeList(
  68. var,
  69. &penumvariant->_pObjList
  70. );
  71. BAIL_ON_FAILURE(hr);
  72. */
  73. penumvariant->_Credentials = Credentials;
  74. *ppenumvariant = penumvariant;
  75. hr = BuildNDSPathFromADsPath2(
  76. ADsPath,
  77. &pszTreeName,
  78. &pszDn
  79. );
  80. BAIL_ON_FAILURE(hr);
  81. hr = ADsNdsOpenContext(
  82. pszTreeName,
  83. Credentials,
  84. &penumvariant->_hADsContext
  85. );
  86. if (pszTreeName) {
  87. FreeADsStr(pszTreeName);
  88. }
  89. if (pszDn) {
  90. FreeADsStr(pszDn);
  91. }
  92. RRETURN(hr);
  93. error:
  94. if (penumvariant) {
  95. }
  96. delete penumvariant;
  97. *ppenumvariant = NULL;
  98. if (pszTreeName) {
  99. FreeADsStr(pszTreeName);
  100. }
  101. if (pszDn) {
  102. FreeADsStr(pszDn);
  103. }
  104. RRETURN(hr);
  105. }
  106. CNDSTreeEnum::CNDSTreeEnum():
  107. _ADsPath(NULL)
  108. {
  109. _pObjList = NULL;
  110. _dwObjectReturned = 0;
  111. _dwObjectCurrentEntry = 0;
  112. _dwObjectTotal = 0;
  113. _hADsContext = NULL;
  114. _hOperationData = NULL;
  115. _lpObjects = NULL;
  116. _pNdsFilterList = NULL;
  117. _fSchemaReturned = NULL;
  118. _bNoMore = FALSE;
  119. }
  120. CNDSTreeEnum::~CNDSTreeEnum()
  121. {
  122. if (_ADsPath) {
  123. ADsFreeString(_ADsPath);
  124. }
  125. if (_pNdsFilterList) {
  126. FreeFilterList((LPBYTE)_pNdsFilterList);
  127. }
  128. if (_hADsContext) {
  129. ADsNdsCloseContext(_hADsContext);
  130. }
  131. ADsNdsFreeNdsObjInfoList(_lpObjects, _dwObjectReturned);
  132. }
  133. HRESULT
  134. CNDSTreeEnum::EnumObjects(
  135. DWORD ObjectType,
  136. ULONG cElements,
  137. VARIANT FAR * pvar,
  138. ULONG FAR * pcElementFetched
  139. )
  140. {
  141. //
  142. // Multi-level detection of Objects may not be necessary for NDS code
  143. //
  144. RRETURN(EnumGenericObjects(cElements, pvar, pcElementFetched));
  145. //
  146. // BugBug- commenting out this layer of code
  147. //
  148. /*
  149. switch (ObjectType) {
  150. default:
  151. RRETURN(EnumObjects(cElements, pvar, pcElementFetched));
  152. } */
  153. }
  154. HRESULT
  155. CNDSTreeEnum::EnumObjects(
  156. ULONG cElements,
  157. VARIANT FAR* pvar,
  158. ULONG FAR* pcElementFetched
  159. )
  160. {
  161. DWORD i;
  162. ULONG cRequested = 0;
  163. ULONG cFetchedByPath = 0;
  164. ULONG cTotalFetched = 0;
  165. VARIANT FAR* pPathvar = pvar;
  166. HRESULT hr;
  167. DWORD ObjectType;
  168. for (i = 0; i < cElements; i++) {
  169. VariantInit(&pvar[i]);
  170. }
  171. cRequested = cElements;
  172. while (SUCCEEDED(_pObjList->GetCurrentObject(&ObjectType)) &&
  173. ((hr = EnumObjects(ObjectType,
  174. cRequested,
  175. pPathvar,
  176. &cFetchedByPath)) == S_FALSE )) {
  177. pPathvar += cFetchedByPath;
  178. cRequested -= cFetchedByPath;
  179. cTotalFetched += cFetchedByPath;
  180. cFetchedByPath = 0;
  181. if (FAILED(_pObjList->Next())){
  182. if (pcElementFetched)
  183. *pcElementFetched = cTotalFetched;
  184. RRETURN(S_FALSE);
  185. }
  186. }
  187. if (pcElementFetched) {
  188. *pcElementFetched = cTotalFetched + cFetchedByPath;
  189. }
  190. RRETURN(hr);
  191. }
  192. HRESULT
  193. CNDSTreeEnum::EnumGenericObjects(
  194. ULONG cElements,
  195. VARIANT FAR* pvar,
  196. ULONG FAR* pcElementFetched
  197. )
  198. {
  199. HRESULT hr = S_OK;
  200. IDispatch *pDispatch = NULL;
  201. DWORD i = 0;
  202. while (i < cElements) {
  203. hr = GetGenObject(&pDispatch);
  204. if (hr == S_FALSE) {
  205. break;
  206. }
  207. VariantInit(&pvar[i]);
  208. pvar[i].vt = VT_DISPATCH;
  209. pvar[i].pdispVal = pDispatch;
  210. (*pcElementFetched)++;
  211. i++;
  212. }
  213. return(hr);
  214. }
  215. HRESULT
  216. CNDSTreeEnum::GetGenObject(
  217. IDispatch ** ppDispatch
  218. )
  219. {
  220. HRESULT hr = S_OK;
  221. DWORD dwStatus = 0L;
  222. PADSNDS_OBJECT_INFO lpCurrentObject = NULL;
  223. IADs * pADs = NULL;
  224. *ppDispatch = NULL;
  225. if (!_hOperationData || (_dwObjectCurrentEntry == _dwObjectReturned)) {
  226. if (_hOperationData) {
  227. ADsNdsFreeNdsObjInfoList(_lpObjects, _dwObjectReturned);
  228. _lpObjects = NULL;
  229. }
  230. _dwObjectCurrentEntry = 0;
  231. _dwObjectReturned = 0;
  232. if (_bNoMore) {
  233. hr = S_FALSE;
  234. goto error;
  235. }
  236. hr = ADsNdsListObjects(
  237. _hADsContext,
  238. L"[Root]",
  239. L"",
  240. L"",
  241. NULL,
  242. FALSE,
  243. &_hOperationData
  244. );
  245. BAIL_ON_FAILURE(hr);
  246. if (hr == S_ADS_NOMORE_ROWS) {
  247. _bNoMore = TRUE;
  248. }
  249. hr = ADsNdsGetObjectListFromBuffer(
  250. _hADsContext,
  251. _hOperationData,
  252. &_dwObjectReturned,
  253. &_lpObjects
  254. );
  255. BAIL_ON_FAILURE(hr);
  256. if (_dwObjectReturned == 0 ) {
  257. RRETURN (S_FALSE);
  258. goto error;
  259. }
  260. }
  261. //
  262. // Now send back the current object
  263. //
  264. lpCurrentObject = _lpObjects + _dwObjectCurrentEntry;
  265. //
  266. // Bump up the object count. The instantiation of this object
  267. // may fail; if we come into this function again, we do not want
  268. // to pick up the same object.
  269. //
  270. _dwObjectCurrentEntry++;
  271. hr = CNDSGenObject::CreateGenericObject(
  272. _ADsPath,
  273. lpCurrentObject->szObjectName,
  274. lpCurrentObject->szObjectClass,
  275. _Credentials,
  276. ADS_OBJECT_BOUND,
  277. IID_IADs,
  278. (void **)&pADs
  279. );
  280. BAIL_ON_FAILURE(hr);
  281. //
  282. // InstantiateDerivedObject should addref this pointer for us.
  283. //
  284. hr = InstantiateDerivedObject(
  285. pADs,
  286. _Credentials,
  287. IID_IDispatch,
  288. (void **)ppDispatch
  289. );
  290. if (FAILED(hr)) {
  291. hr = pADs->QueryInterface(
  292. IID_IDispatch,
  293. (void **)ppDispatch
  294. );
  295. BAIL_ON_FAILURE(hr);
  296. }
  297. error:
  298. if (hr == S_FALSE) {
  299. ADsNdsFreeBuffer(_hOperationData);
  300. _hOperationData = NULL;
  301. }
  302. //
  303. // GetGenObject returns only S_FALSE
  304. //
  305. if (FAILED(hr)) {
  306. hr = S_FALSE;
  307. }
  308. //
  309. // Free the intermediate pADs pointer.
  310. //
  311. if (pADs) {
  312. pADs->Release();
  313. }
  314. RRETURN(hr);
  315. }
  316. //+---------------------------------------------------------------------------
  317. //
  318. // Function: CNDSTreeEnum::Next
  319. //
  320. // Synopsis: Returns cElements number of requested NetOle objects in the
  321. // array supplied in pvar.
  322. //
  323. // Arguments: [cElements] -- The number of elements requested by client
  324. // [pvar] -- ptr to array of VARIANTs to for return objects
  325. // [pcElementFetched] -- if non-NULL, then number of elements
  326. // -- actually returned is placed here
  327. //
  328. // Returns: HRESULT -- S_OK if number of elements requested are returned
  329. // -- S_FALSE if number of elements is < requested
  330. //
  331. // Modifies:
  332. //
  333. // History: 11-3-95 krishnag Created.
  334. //
  335. //----------------------------------------------------------------------------
  336. STDMETHODIMP
  337. CNDSTreeEnum::Next(
  338. ULONG cElements,
  339. VARIANT FAR* pvar,
  340. ULONG FAR* pcElementFetched
  341. )
  342. {
  343. ULONG cElementFetched = 0;
  344. HRESULT hr = S_OK;
  345. hr = EnumGenericObjects(
  346. cElements,
  347. pvar,
  348. &cElementFetched
  349. );
  350. if (pcElementFetched) {
  351. *pcElementFetched = cElementFetched;
  352. }
  353. RRETURN(hr);
  354. }
  355. HRESULT
  356. CNDSTreeEnum::EnumSchema(
  357. ULONG cElements,
  358. VARIANT FAR* pvar,
  359. ULONG FAR* pcElementFetched
  360. )
  361. {
  362. HRESULT hr = S_OK;
  363. IDispatch *pDispatch = NULL;
  364. if ( _fSchemaReturned )
  365. RRETURN(S_FALSE);
  366. if ( cElements > 0 )
  367. {
  368. hr = CNDSSchema::CreateSchema(
  369. _ADsPath,
  370. TEXT("Schema"),
  371. _Credentials,
  372. ADS_OBJECT_BOUND,
  373. IID_IDispatch,
  374. (void **)&pDispatch
  375. );
  376. if ( hr == S_OK )
  377. {
  378. VariantInit(&pvar[0]);
  379. pvar[0].vt = VT_DISPATCH;
  380. pvar[0].pdispVal = pDispatch;
  381. (*pcElementFetched)++;
  382. _fSchemaReturned = TRUE;
  383. }
  384. }
  385. RRETURN(hr);
  386. }