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.

425 lines
8.5 KiB

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