home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / Start / Search / vc / Search.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-19  |  2.1 KB  |  85 lines

  1. // Search.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "stdio.h"
  6. #include "activeds.h"
  7.  
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11.     HRESULT hr;
  12.     IDirectorySearch *pSearch;
  13.     CoInitialize(NULL);
  14.  
  15.  
  16.     ///////////////////////////////////////////////
  17.     // Bind to Object, it serves as a base search
  18.     ///////////////////////////////////////////////
  19.     hr = ADsGetObject(L"LDAP://DC=windows2000,DC=nttest,DC=microsoft,DC=com",
  20.                       IID_IDirectorySearch,
  21.                       (void**) &pSearch );
  22.  
  23.     if ( !SUCCEEDED(hr) )
  24.     {
  25.         return hr;
  26.     }
  27.     
  28.  
  29.     ///////////////////////////////////////
  30.     // We want a subtree search
  31.     /////////////////////////////////////////
  32.     ADS_SEARCHPREF_INFO prefInfo[1];
  33.     prefInfo[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  34.     prefInfo[0].vValue.dwType = ADSTYPE_INTEGER;
  35.     prefInfo[0].vValue.Integer = ADS_SCOPE_SUBTREE;
  36.     hr = pSearch->SetSearchPreference( prefInfo, 1);
  37.  
  38.     ////////////////////////////////////
  39.     // Prepared for attributed returned
  40.     ////////////////////////////////////
  41.     LPWSTR pszAttr[] = { L"Name"};
  42.     ADS_SEARCH_HANDLE hSearch;
  43.     DWORD dwCount= sizeof(pszAttr)/sizeof(LPWSTR);
  44.  
  45.  
  46.     //////////////////////////////////////////
  47.     // Search for all groups in a domain
  48.     /////////////////////////////////////////////
  49.     hr = pSearch->ExecuteSearch(L"(objectCategory=Group)", pszAttr, dwCount, &hSearch );
  50.  
  51.     if ( !SUCCEEDED(hr) )
  52.     {
  53.         pSearch->Release();
  54.         return hr;
  55.     }
  56.  
  57.     //////////////////////////////////////////
  58.     // Now enumerate the result
  59.     /////////////////////////////////////////////
  60.     ADS_SEARCH_COLUMN col;
  61.     while(  pSearch->GetNextRow(hSearch)  != S_ADS_NOMORE_ROWS )
  62.     {
  63.         // Get 'Name' attribute
  64.         hr = pSearch->GetColumn( hSearch, pszAttr[0], &col );
  65.         if ( SUCCEEDED(hr) )
  66.         {
  67.             printf("%S\n", col.pADsValues->CaseIgnoreString);
  68.             pSearch->FreeColumn( &col ); // You need to FreeColum after use.
  69.         }
  70.  
  71.       
  72.     }
  73.  
  74.     ////////////////////
  75.     // Clean-up
  76.     ////////////////////////
  77.     pSearch->CloseSearchHandle(hSearch);
  78.     pSearch->Release();
  79.  
  80.  
  81.  
  82.     CoUninitialize();
  83.     return 0;
  84. }
  85.