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.

407 lines
8.3 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. WCHAR szADsPathName[MAX_PATH];
  116. WCHAR szNDSTreeName[MAX_PATH];
  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 = BuildNDSTreeNameFromADsPath(
  129. _bstrUserName,
  130. szNDSTreeName
  131. );
  132. BAIL_ON_FAILURE(hr);
  133. // Make the first two characters "//" instead of "\\"
  134. szNDSTreeName[0] = (WCHAR)'/';
  135. szNDSTreeName[1] = (WCHAR)'/';
  136. hr = BuildADsPathFromNDSPath(
  137. szNDSTreeName,
  138. v.bstrVal,
  139. szADsPathName
  140. );
  141. BAIL_ON_FAILURE(hr);
  142. hr = CNDSGenObject::CreateGenericObject(
  143. szADsPathName,
  144. L"group",
  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. RRETURN(hr);
  158. }
  159. HRESULT
  160. CNDSUserCollectionEnum::GetUserSingleMemberObject(
  161. IDispatch ** ppDispatch
  162. )
  163. {
  164. HRESULT hr = S_OK;
  165. WCHAR szADsPathName[MAX_PATH];
  166. WCHAR szNDSTreeName[MAX_PATH];
  167. *ppDispatch = NULL;
  168. if (_dwIndex == 1) {
  169. RRETURN(S_FALSE);
  170. }
  171. hr = BuildNDSTreeNameFromADsPath(
  172. _bstrUserName,
  173. szNDSTreeName
  174. );
  175. BAIL_ON_FAILURE(hr);
  176. // Make the first two characters "//" instead of "\\"
  177. szNDSTreeName[0] = (WCHAR)'/';
  178. szNDSTreeName[1] = (WCHAR)'/';
  179. hr = BuildADsPathFromNDSPath(
  180. szNDSTreeName,
  181. _vMembers.bstrVal,
  182. szADsPathName
  183. );
  184. BAIL_ON_FAILURE(hr);
  185. hr = CNDSGenObject::CreateGenericObject(
  186. szADsPathName,
  187. L"group",
  188. _Credentials,
  189. ADS_OBJECT_BOUND,
  190. IID_IDispatch,
  191. (void **)ppDispatch
  192. );
  193. BAIL_ON_FAILURE(hr);
  194. _dwIndex++;
  195. error:
  196. if (FAILED(hr)) {
  197. hr = S_FALSE;
  198. }
  199. RRETURN(hr);
  200. }
  201. //+---------------------------------------------------------------------------
  202. //
  203. // Function: CNDSUserCollectionEnum::Next
  204. //
  205. // Synopsis: Returns cElements number of requested NetOle objects in the
  206. // array supplied in pvar.
  207. //
  208. // Arguments: [cElements] -- The number of elements requested by client
  209. // [pvar] -- ptr to array of VARIANTs to for return objects
  210. // [pcElementFetched] -- if non-NULL, then number of elements
  211. // -- actually returned is placed here
  212. //
  213. // Returns: HRESULT -- S_OK if number of elements requested are returned
  214. // -- S_FALSE if number of elements is < requested
  215. //
  216. // Modifies:
  217. //
  218. // History: 11-3-95 krishnag Created.
  219. //
  220. //----------------------------------------------------------------------------
  221. STDMETHODIMP
  222. CNDSUserCollectionEnum::Next(
  223. ULONG cElements,
  224. VARIANT FAR* pvar,
  225. ULONG FAR* pcElementFetched
  226. )
  227. {
  228. ULONG cElementFetched = 0;
  229. HRESULT hr = S_OK;
  230. hr = EnumUserMembers(
  231. cElements,
  232. pvar,
  233. &cElementFetched
  234. );
  235. if (pcElementFetched) {
  236. *pcElementFetched = cElementFetched;
  237. }
  238. RRETURN(hr);
  239. }
  240. HRESULT
  241. CNDSUserCollectionEnum::ValidateVariant(
  242. VARIANT var
  243. )
  244. {
  245. if (V_VT(&var) == (VT_VARIANT|VT_ARRAY)) {
  246. _dwMultiple = MULTIPLE;
  247. RRETURN(ValidateMultipleVariant(var));
  248. }else if (V_VT(&var) == VT_BSTR){
  249. _dwMultiple = SINGLE;
  250. RRETURN(ValidateSingleVariant(var));
  251. }else if (V_VT(&var) == VT_EMPTY){
  252. _dwMultiple = EMPTY;
  253. RRETURN(S_OK);
  254. }
  255. RRETURN(E_FAIL);
  256. }
  257. HRESULT
  258. CNDSUserCollectionEnum::ValidateMultipleVariant(
  259. VARIANT var
  260. )
  261. {
  262. HRESULT hr = S_OK;
  263. DWORD dwSLBound = 0;
  264. DWORD dwSUBound = 0;
  265. if (!(V_VT(&var) == (VT_VARIANT|VT_ARRAY))) {
  266. return(E_FAIL);
  267. }
  268. //
  269. // Check that there is only one dimension in this array
  270. //
  271. if ((V_ARRAY(&var))->cDims != 1) {
  272. hr = E_FAIL;
  273. BAIL_ON_FAILURE(hr);
  274. }
  275. //
  276. // Check that there is atleast one element in this array
  277. //
  278. if ((V_ARRAY(&var))->rgsabound[0].cElements == 0){
  279. hr = E_FAIL;
  280. BAIL_ON_FAILURE(hr);
  281. }
  282. //
  283. // We know that this is a valid single dimension array
  284. //
  285. hr = SafeArrayGetLBound(
  286. V_ARRAY(&var),
  287. 1,
  288. (long FAR *)&dwSLBound
  289. );
  290. BAIL_ON_FAILURE(hr);
  291. hr = SafeArrayGetUBound(
  292. V_ARRAY(&var),
  293. 1,
  294. (long FAR *)&dwSUBound
  295. );
  296. BAIL_ON_FAILURE(hr);
  297. hr = VariantCopy(&_vMembers, &var);
  298. BAIL_ON_FAILURE(hr);
  299. _dwSUBound = dwSUBound;
  300. _dwSLBound = dwSLBound;
  301. _dwIndex = dwSLBound;
  302. error:
  303. RRETURN(hr);
  304. }
  305. HRESULT
  306. CNDSUserCollectionEnum::ValidateSingleVariant(
  307. VARIANT var
  308. )
  309. {
  310. HRESULT hr = S_OK;
  311. if(!( V_VT(&var) == VT_BSTR)){
  312. return(E_FAIL);
  313. }
  314. hr = VariantCopy(&_vMembers, &var);
  315. BAIL_ON_FAILURE(hr);
  316. _dwIndex = 0;
  317. error:
  318. RRETURN(hr);
  319. }