home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / General / ADsCmd / enum.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-16  |  4.4 KB  |  230 lines

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