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.

230 lines
4.2 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Active Directory 1.0 Sample Code
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996
  6. //
  7. // File: enum.cxx
  8. //
  9. // Contents: Active Drectory container enumeration
  10. //
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "main.hxx"
  14. //
  15. // Private defines
  16. //
  17. #define MAX_ADS_FILTERS 10
  18. #define MAX_ADS_ENUM 100 // number of entries to read each time
  19. //
  20. // Local functions
  21. //
  22. HRESULT
  23. PrintLongFormat(
  24. IADs * pObject
  25. );
  26. //
  27. // List contents of a container identified by the ADsPath
  28. //
  29. int
  30. DoList(char * AnsiADsPath)
  31. {
  32. HRESULT hr;
  33. int i = 0 ;
  34. LPWSTR pszADsPath, apszTypes[MAX_ADS_FILTERS] ;
  35. if (!(pszADsPath = AllocateUnicodeString(AnsiADsPath))) {
  36. return(1) ;
  37. }
  38. apszTypes[0] = NULL ;
  39. //
  40. // Filter may be set as follows. For example, to get users and group:
  41. //
  42. // apszTypes[0] = L"User" ;
  43. // apszTypes[1] = L"Group" ;
  44. // apszTypes[2] = NULL ;
  45. //
  46. hr = EnumObject(
  47. pszADsPath,
  48. apszTypes,
  49. i
  50. );
  51. return (FAILED(hr) ? 1 : 0) ;
  52. }
  53. //
  54. // Enumerates the contents of a container object.
  55. //
  56. HRESULT
  57. EnumObject(
  58. LPWSTR pszADsPath,
  59. LPWSTR * lppClassNames,
  60. DWORD dwClassNames
  61. )
  62. {
  63. ULONG cElementFetched = 0L;
  64. IEnumVARIANT * pEnumVariant = NULL;
  65. VARIANT VarFilter, VariantArray[MAX_ADS_ENUM];
  66. HRESULT hr;
  67. IADsContainer * pADsContainer = NULL;
  68. DWORD dwObjects = 0, dwEnumCount = 0, i = 0;
  69. BOOL fContinue = TRUE;
  70. VariantInit(&VarFilter);
  71. hr = ADsGetObject(
  72. pszADsPath,
  73. IID_IADsContainer,
  74. (void **)&pADsContainer
  75. );
  76. if (FAILED(hr)) {
  77. printf("\"%S\" is not a valid container object.\n", pszADsPath) ;
  78. goto exitpoint ;
  79. }
  80. hr = ADsBuildVarArrayStr(
  81. lppClassNames,
  82. dwClassNames,
  83. &VarFilter
  84. );
  85. BAIL_ON_FAILURE(hr);
  86. hr = pADsContainer->put_Filter(VarFilter);
  87. BAIL_ON_FAILURE(hr);
  88. hr = ADsBuildEnumerator(
  89. pADsContainer,
  90. &pEnumVariant
  91. );
  92. BAIL_ON_FAILURE(hr);
  93. while (fContinue) {
  94. IADs *pObject ;
  95. hr = ADsEnumerateNext(
  96. pEnumVariant,
  97. MAX_ADS_ENUM,
  98. VariantArray,
  99. &cElementFetched
  100. );
  101. if (hr == S_FALSE) {
  102. fContinue = FALSE;
  103. }
  104. dwEnumCount++;
  105. for (i = 0; i < cElementFetched; i++ ) {
  106. IDispatch *pDispatch = NULL;
  107. pDispatch = VariantArray[i].pdispVal;
  108. hr = pDispatch->QueryInterface(IID_IADs,
  109. (VOID **) &pObject) ;
  110. BAIL_ON_FAILURE(hr);
  111. PrintLongFormat(pObject);
  112. pObject->Release();
  113. pDispatch->Release();
  114. }
  115. memset(VariantArray, 0, sizeof(VARIANT)*MAX_ADS_ENUM);
  116. dwObjects += cElementFetched;
  117. }
  118. printf("Total Number of Objects enumerated is %d\n", dwObjects);
  119. if (pEnumVariant) {
  120. pEnumVariant->Release();
  121. }
  122. if (pADsContainer) {
  123. pADsContainer->Release();
  124. }
  125. return(S_OK);
  126. error:
  127. if (FAILED(hr)) {
  128. printf("Unable to list contents of: %S\n", pszADsPath) ;
  129. }
  130. exitpoint:
  131. if (pEnumVariant) {
  132. pEnumVariant->Release();
  133. }
  134. VariantClear(&VarFilter);
  135. if (pADsContainer) {
  136. pADsContainer->Release();
  137. }
  138. return(hr);
  139. }
  140. HRESULT
  141. PrintLongFormat(IADs * pObject)
  142. {
  143. HRESULT hr = S_OK;
  144. BSTR bstrName = NULL;
  145. BSTR bstrClass = NULL;
  146. BSTR bstrSchema = NULL;
  147. hr = pObject->get_Name(&bstrName) ;
  148. BAIL_ON_FAILURE(hr);
  149. hr = pObject->get_Class(&bstrClass);
  150. BAIL_ON_FAILURE(hr);
  151. // hr = pObject->get_Schema(&bstrSchema);
  152. printf(" %S(%S)\n", bstrName, bstrClass) ;
  153. error:
  154. if (bstrClass) {
  155. SysFreeString(bstrClass);
  156. }
  157. if (bstrName) {
  158. SysFreeString(bstrName);
  159. }
  160. if (bstrSchema) {
  161. SysFreeString(bstrSchema);
  162. }
  163. return(hr);
  164. }