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.

428 lines
8.6 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: cenumGroupCollection.cxx
  7. //
  8. // Contents: Windows NT 3.5 GroupCollection Enumeration Code
  9. //
  10. // CNDSGroupCollectionEnum::CNDSGroupCollectionEnum()
  11. // CNDSGroupCollectionEnum::CNDSGroupCollectionEnum
  12. // CNDSGroupCollectionEnum::EnumObjects
  13. // CNDSGroupCollectionEnum::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. CNDSGroupCollectionEnum::Create(
  37. BSTR bstrGroupName,
  38. CCredentials& Credentials,
  39. CNDSGroupCollectionEnum FAR* FAR* ppenumvariant,
  40. VARIANT var
  41. )
  42. {
  43. HRESULT hr = NOERROR;
  44. CNDSGroupCollectionEnum FAR* penumvariant = NULL;
  45. *ppenumvariant = NULL;
  46. penumvariant = new CNDSGroupCollectionEnum();
  47. if (!penumvariant) {
  48. hr = E_OUTOFMEMORY;
  49. BAIL_ON_FAILURE(hr);
  50. }
  51. hr = ADsAllocString(bstrGroupName, &(penumvariant->_bstrGroupName));
  52. BAIL_ON_FAILURE(hr);
  53. hr = penumvariant->ValidateVariant(
  54. var
  55. );
  56. BAIL_ON_FAILURE(hr);
  57. penumvariant->_Credentials = Credentials;
  58. *ppenumvariant = penumvariant;
  59. RRETURN(hr);
  60. error:
  61. delete penumvariant;
  62. RRETURN(hr);
  63. }
  64. CNDSGroupCollectionEnum::CNDSGroupCollectionEnum():
  65. _dwSLBound(0),
  66. _dwSUBound(0),
  67. _dwIndex(0),
  68. _dwMultiple(0),
  69. _bstrGroupName(0)
  70. {
  71. VariantInit(&_vMembers);
  72. }
  73. CNDSGroupCollectionEnum::~CNDSGroupCollectionEnum()
  74. {
  75. VariantClear(&_vMembers);
  76. if (_bstrGroupName) {
  77. ADsFreeString(_bstrGroupName);
  78. }
  79. }
  80. HRESULT
  81. CNDSGroupCollectionEnum::EnumGroupMembers(
  82. ULONG cElements,
  83. VARIANT FAR* pvar,
  84. ULONG FAR* pcElementFetched
  85. )
  86. {
  87. HRESULT hr = S_OK;
  88. IDispatch *pDispatch = NULL;
  89. DWORD i = 0;
  90. *pcElementFetched = 0;
  91. while (i < cElements) {
  92. if (_dwMultiple == MULTIPLE) {
  93. hr = GetGroupMultipleMemberObject(&pDispatch);
  94. }else if (_dwMultiple == SINGLE){
  95. hr = GetGroupSingleMemberObject(&pDispatch);
  96. }else {
  97. hr = S_FALSE;
  98. }
  99. if (hr == S_FALSE) {
  100. break;
  101. }
  102. VariantInit(&pvar[i]);
  103. pvar[i].vt = VT_DISPATCH;
  104. pvar[i].pdispVal = pDispatch;
  105. (*pcElementFetched)++;
  106. i++;
  107. }
  108. RRETURN(hr);
  109. }
  110. HRESULT
  111. CNDSGroupCollectionEnum::GetGroupMultipleMemberObject(
  112. IDispatch ** ppDispatch
  113. )
  114. {
  115. VARIANT v;
  116. HRESULT hr = S_OK;
  117. LPWSTR szADsPathName = NULL;
  118. LPWSTR pszNDSTreeName = NULL, pszNDSDn = NULL;
  119. *ppDispatch = NULL;
  120. if (_dwIndex > _dwSUBound) {
  121. RRETURN(S_FALSE);
  122. }
  123. VariantInit(&v);
  124. hr = SafeArrayGetElement(
  125. V_ARRAY(&_vMembers),
  126. (long FAR *)&_dwIndex,
  127. &v
  128. );
  129. BAIL_ON_FAILURE(hr);
  130. hr = BuildNDSPathFromADsPath2(
  131. _bstrGroupName,
  132. &pszNDSTreeName,
  133. &pszNDSDn
  134. );
  135. BAIL_ON_FAILURE(hr);
  136. hr = BuildADsPathFromNDSPath(
  137. pszNDSTreeName,
  138. v.bstrVal,
  139. &szADsPathName
  140. );
  141. BAIL_ON_FAILURE(hr);
  142. hr = CNDSGenObject::CreateGenericObject(
  143. szADsPathName,
  144. L"user",
  145. _Credentials,
  146. ADS_OBJECT_BOUND,
  147. IID_IDispatch,
  148. (void **)ppDispatch
  149. );
  150. BAIL_ON_FAILURE(hr);
  151. _dwIndex++;
  152. error:
  153. VariantClear(&v);
  154. if (FAILED(hr)) {
  155. hr = S_FALSE;
  156. }
  157. if (szADsPathName) {
  158. FreeADsMem(szADsPathName);
  159. }
  160. if (pszNDSDn) {
  161. FreeADsMem(pszNDSDn);
  162. }
  163. if (pszNDSTreeName) {
  164. FreeADsMem(pszNDSTreeName);
  165. }
  166. RRETURN(hr);
  167. }
  168. HRESULT
  169. CNDSGroupCollectionEnum::GetGroupSingleMemberObject(
  170. IDispatch ** ppDispatch
  171. )
  172. {
  173. HRESULT hr = S_OK;
  174. LPWSTR szADsPathName = NULL;
  175. LPWSTR pszNDSTreeName = NULL, pszNDSDn = NULL;
  176. *ppDispatch = NULL;
  177. if (_dwIndex == 1) {
  178. RRETURN(S_FALSE);
  179. }
  180. hr = BuildNDSPathFromADsPath2(
  181. _bstrGroupName,
  182. &pszNDSTreeName,
  183. &pszNDSDn
  184. );
  185. BAIL_ON_FAILURE(hr);
  186. hr = BuildADsPathFromNDSPath(
  187. pszNDSTreeName,
  188. _vMembers.bstrVal,
  189. &szADsPathName
  190. );
  191. BAIL_ON_FAILURE(hr);
  192. hr = CNDSGenObject::CreateGenericObject(
  193. szADsPathName,
  194. L"user",
  195. _Credentials,
  196. ADS_OBJECT_BOUND,
  197. IID_IDispatch,
  198. (void **)ppDispatch
  199. );
  200. BAIL_ON_FAILURE(hr);
  201. _dwIndex++;
  202. error:
  203. if (FAILED(hr)) {
  204. hr = S_FALSE;
  205. }
  206. if (szADsPathName) {
  207. FreeADsMem(szADsPathName);
  208. }
  209. if (pszNDSDn) {
  210. FreeADsMem(pszNDSDn);
  211. }
  212. if (pszNDSTreeName) {
  213. FreeADsMem(pszNDSTreeName);
  214. }
  215. RRETURN(hr);
  216. }
  217. //+---------------------------------------------------------------------------
  218. //
  219. // Function: CNDSGroupCollectionEnum::Next
  220. //
  221. // Synopsis: Returns cElements number of requested NetOle objects in the
  222. // array supplied in pvar.
  223. //
  224. // Arguments: [cElements] -- The number of elements requested by client
  225. // [pvar] -- ptr to array of VARIANTs to for return objects
  226. // [pcElementFetched] -- if non-NULL, then number of elements
  227. // -- actually returned is placed here
  228. //
  229. // Returns: HRESULT -- S_OK if number of elements requested are returned
  230. // -- S_FALSE if number of elements is < requested
  231. //
  232. // Modifies:
  233. //
  234. // History: 11-3-95 krishnag Created.
  235. //
  236. //----------------------------------------------------------------------------
  237. STDMETHODIMP
  238. CNDSGroupCollectionEnum::Next(
  239. ULONG cElements,
  240. VARIANT FAR* pvar,
  241. ULONG FAR* pcElementFetched
  242. )
  243. {
  244. ULONG cElementFetched = 0;
  245. HRESULT hr = S_OK;
  246. hr = EnumGroupMembers(
  247. cElements,
  248. pvar,
  249. &cElementFetched
  250. );
  251. if (pcElementFetched) {
  252. *pcElementFetched = cElementFetched;
  253. }
  254. RRETURN(hr);
  255. }
  256. HRESULT
  257. CNDSGroupCollectionEnum::ValidateVariant(
  258. VARIANT var
  259. )
  260. {
  261. if (V_VT(&var) == (VT_VARIANT|VT_ARRAY)) {
  262. _dwMultiple = MULTIPLE;
  263. RRETURN(ValidateMultipleVariant(var));
  264. }else if (V_VT(&var) == VT_BSTR){
  265. _dwMultiple = SINGLE;
  266. RRETURN(ValidateSingleVariant(var));
  267. }else if (V_VT(&var) == VT_EMPTY){
  268. _dwMultiple = EMPTY;
  269. RRETURN(S_OK);
  270. }
  271. RRETURN(E_FAIL);
  272. }
  273. HRESULT
  274. CNDSGroupCollectionEnum::ValidateMultipleVariant(
  275. VARIANT var
  276. )
  277. {
  278. HRESULT hr = S_OK;
  279. DWORD dwSLBound = 0;
  280. DWORD dwSUBound = 0;
  281. if (!(V_VT(&var) == (VT_VARIANT|VT_ARRAY))) {
  282. return(E_FAIL);
  283. }
  284. //
  285. // Check that there is only one dimension in this array
  286. //
  287. if ((V_ARRAY(&var))->cDims != 1) {
  288. hr = E_FAIL;
  289. BAIL_ON_FAILURE(hr);
  290. }
  291. //
  292. // Check that there is atleast one element in this array
  293. //
  294. if ((V_ARRAY(&var))->rgsabound[0].cElements == 0){
  295. hr = E_FAIL;
  296. BAIL_ON_FAILURE(hr);
  297. }
  298. //
  299. // We know that this is a valid single dimension array
  300. //
  301. hr = SafeArrayGetLBound(
  302. V_ARRAY(&var),
  303. 1,
  304. (long FAR *)&dwSLBound
  305. );
  306. BAIL_ON_FAILURE(hr);
  307. hr = SafeArrayGetUBound(
  308. V_ARRAY(&var),
  309. 1,
  310. (long FAR *)&dwSUBound
  311. );
  312. BAIL_ON_FAILURE(hr);
  313. hr = VariantCopy(&_vMembers, &var);
  314. BAIL_ON_FAILURE(hr);
  315. _dwSUBound = dwSUBound;
  316. _dwSLBound = dwSLBound;
  317. _dwIndex = dwSLBound;
  318. error:
  319. RRETURN(hr);
  320. }
  321. HRESULT
  322. CNDSGroupCollectionEnum::ValidateSingleVariant(
  323. VARIANT var
  324. )
  325. {
  326. HRESULT hr = S_OK;
  327. if(!( V_VT(&var) == VT_BSTR)){
  328. return(E_FAIL);
  329. }
  330. hr = VariantCopy(&_vMembers, &var);
  331. BAIL_ON_FAILURE(hr);
  332. _dwIndex = 0;
  333. error:
  334. RRETURN(hr);
  335. }