home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / adsi / sampapp / adscmd / enum.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-29  |  4.4 KB  |  231 lines

  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.  
  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.     if (pEnumVariant) {
  167.         pEnumVariant->Release();
  168.     }
  169.  
  170.     if (pADsContainer) {
  171.         pADsContainer->Release();
  172.     }
  173.  
  174.     return(S_OK);
  175.  
  176. error:
  177.     if (FAILED(hr)) {
  178.  
  179.         printf("Unable to list contents of: %S\n", pszADsPath) ;
  180.     }
  181.  
  182. exitpoint:
  183.  
  184.     if (pEnumVariant) {
  185.         pEnumVariant->Release();
  186.     }
  187.  
  188.     VariantClear(&VarFilter);
  189.  
  190.  
  191.     if (pADsContainer) {
  192.         pADsContainer->Release();
  193.     }
  194.  
  195.     return(hr);
  196. }
  197.  
  198. HRESULT
  199. PrintLongFormat(IADs * pObject)
  200. {
  201.  
  202.     HRESULT hr = S_OK;
  203.     BSTR bstrName = NULL;
  204.     BSTR bstrClass = NULL;
  205.     BSTR bstrSchema = NULL;
  206.  
  207.     hr = pObject->get_Name(&bstrName) ;
  208.     BAIL_ON_FAILURE(hr);
  209.  
  210.     hr = pObject->get_Class(&bstrClass);
  211.     BAIL_ON_FAILURE(hr);
  212.  
  213.     // hr = pObject->get_Schema(&bstrSchema);
  214.  
  215.     printf("  %S(%S)\n", bstrName, bstrClass) ;
  216.  
  217. error:
  218.     if (bstrClass) {
  219.         SysFreeString(bstrClass);
  220.     }
  221.     if (bstrName) {
  222.         SysFreeString(bstrName);
  223.     }
  224.     if (bstrSchema) {
  225.         SysFreeString(bstrSchema);
  226.     }
  227.     return(hr);
  228. }
  229.  
  230.  
  231.