home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / ActiveDir / GCAllUsers / vc / gcallusers.cpp next >
Encoding:
C/C++ Source or Header  |  1999-04-05  |  4.0 KB  |  158 lines

  1. // gcallusers.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <wchar.h>
  5. #include <activeds.h>
  6.  
  7. HRESULT FindAllUsersInGC();
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11. //Initialize COM
  12. CoInitialize(NULL);
  13.  
  14. HRESULT hr = S_OK;
  15. wprintf(L"This program finds all users in the forest\nby searching the global catalog.\n");
  16. wprintf(L"------------------------------\n");
  17.  
  18. hr = FindAllUsersInGC();
  19. if (FAILED(hr))
  20.   wprintf(L"Search for all users failed with hr: %d\n", hr);
  21. // Uninitialize COM
  22. CoUninitialize();
  23. return 0;
  24. }
  25.  
  26. HRESULT FindAllUsersInGC()
  27. {
  28.     HRESULT hr = E_FAIL;
  29.     HRESULT hrGC = S_OK;
  30.  
  31.     VARIANT var;
  32.     ULONG lFetch;
  33.  
  34.     // Interface Pointers
  35.     IDirectorySearch *pGCSearch = NULL;
  36.     IADsContainer *pContainer = NULL;
  37.     IUnknown *pUnk = NULL;
  38.     IEnumVARIANT *pEnum = NULL;
  39.        IDispatch *pDisp = NULL;
  40.     IADs *pADs = NULL;
  41.     
  42.     //Bind to Global Catalog
  43.     hr = ADsOpenObject(L"GC:",  //NT 4.0, Win9.x client must include the servername, e.g GC://myServer
  44.                  NULL,
  45.                  NULL,
  46.                  ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  47.                  IID_IADsContainer,
  48.                  (void**)&pContainer);
  49.     
  50.     if (SUCCEEDED(hr))
  51.     {
  52.        hr = pContainer->get__NewEnum( &pUnk );
  53.          if (SUCCEEDED(hr))
  54.        {
  55.           hr = pUnk->QueryInterface( IID_IEnumVARIANT, (void**) &pEnum );
  56.           if (SUCCEEDED(hr))
  57.           {
  58.             // Now Enumerate--there should be only one item.
  59.             hr = pEnum->Next( 1, &var, &lFetch );
  60.             if (SUCCEEDED(hr))
  61.             {
  62.                 while( hr == S_OK )
  63.                 {
  64.                     if ( lFetch == 1 )
  65.                     {
  66.                         pDisp = V_DISPATCH(&var);
  67.                         hr = pDisp->QueryInterface( IID_IDirectorySearch, (void**)&pGCSearch); 
  68.                         hrGC = hr;
  69.                     }
  70.                     VariantClear(&var);
  71.                     hr = pEnum->Next( 1, &var, &lFetch );
  72.                 };
  73.             }
  74.           }
  75.           if (pEnum)
  76.             pEnum->Release();
  77.        }
  78.        if (pUnk)
  79.          pUnk->Release();
  80.      }
  81.     if (pContainer)
  82.       pContainer->Release();
  83.  
  84.  
  85.     if (FAILED(hrGC))
  86.     {
  87.         if (pGCSearch)
  88.             pGCSearch->Release();
  89.         return hrGC;
  90.     }
  91.  
  92.     //Create search filter
  93.     LPOLESTR pszSearchFilter = L"(&(objectCategory=person)(objectClass=user))";
  94.     //Search entire subtree from root.
  95.     ADS_SEARCHPREF_INFO SearchPrefs;
  96.     SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  97.     SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
  98.     SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;
  99.     DWORD dwNumPrefs = 1;
  100.  
  101.     // COL for iterations
  102.     ADS_SEARCH_COLUMN col;
  103.  
  104.     // Handle used for searching
  105.     ADS_SEARCH_HANDLE hSearch;
  106.     
  107.     // Set the search preference
  108.     hr = pGCSearch->SetSearchPreference( &SearchPrefs, dwNumPrefs);
  109.     if (FAILED(hr))
  110.         return hr;
  111.     // Set attributes to return
  112.     CONST DWORD dwAttrNameSize = 2;
  113.     LPOLESTR pszAttribute[dwAttrNameSize] = {L"cn",L"distinguishedName"};
  114.  
  115.     // Execute the search
  116.     hr = pGCSearch->ExecuteSearch(pszSearchFilter,
  117.                                   pszAttribute,
  118.                                   dwAttrNameSize,
  119.                                   &hSearch
  120.                                   );
  121.     if ( SUCCEEDED(hr) )
  122.     {    
  123.  
  124.     // Call IDirectorySearch::GetNextRow() to retrieve the next row 
  125.     //of data
  126.         while( pGCSearch->GetNextRow( hSearch) != S_ADS_NOMORE_ROWS )
  127.         {
  128.  
  129.             // loop through the array of passed column names,
  130.             // print the data for each column
  131.             for (DWORD x = 0; x < dwAttrNameSize; x++)
  132.             {
  133.  
  134.                 // Get the data for this column
  135.                 hr = pGCSearch->GetColumn( hSearch, pszAttribute[x], &col );
  136.  
  137.                 if ( SUCCEEDED(hr) )
  138.                 {
  139.                     // Print the data for the column and free the column
  140.                     // Note the attributes we asked for are type CaseIgnoreString.
  141.                     wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString); 
  142.                     pGCSearch->FreeColumn( &col );
  143.                 }
  144.                 else
  145.                     wprintf(L"<%s property is not a string>",pszAttribute[x]);
  146.             }
  147.             wprintf(L"------------------------------\n");
  148.         }
  149.  
  150.         // Close the search handle to clean up
  151.         pGCSearch->CloseSearchHandle(hSearch);
  152.     } 
  153.     if (pGCSearch)
  154.         pGCSearch->Release();
  155.     return hr;
  156. }
  157.  
  158.