home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / Start / Read / vc / Read.cpp next >
Encoding:
C/C++ Source or Header  |  1999-01-18  |  3.1 KB  |  136 lines

  1. // Read.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.     IADs *pUsr=NULL;
  13.  
  14.  
  15.     CoInitialize(NULL);
  16.  
  17.     ////////////////////////////////////
  18.     // Bind to a directory object
  19.     /////////////////////////////////////
  20.     hr = ADsGetObject(L"WinNT://INDEPENDENCE/Administrator,user", IID_IADs, (void**) &pUsr );
  21.     if ( !SUCCEEDED(hr) )
  22.     {
  23.         return 0;
  24.     }
  25.  
  26.  
  27.     ///////////////////////////////////
  28.     // Get a single value attribute
  29.     ////////////////////////////////////
  30.     VARIANT var;
  31.     VariantInit(&var);
  32.  
  33.  
  34.     hr = pUsr->Get(L"FullName", &var );
  35.     if ( SUCCEEDED(hr) )
  36.     {
  37.         printf("FullName: %S\n", V_BSTR(&var) );
  38.         VariantClear(&var);
  39.     }
  40.  
  41.     if ( pUsr )
  42.     {
  43.         pUsr->Release();
  44.     }
  45.  
  46.  
  47.  
  48.     ///////////////////////////////////////////////////////
  49.     // Get a multi value attribute from a service object
  50.     /////////////////////////////////////////////////////////
  51.     IADs *pSvc = NULL;
  52.  
  53.     hr = ADsGetObject(L"WinNT://INDEPENDENCE/ANDYHAR11/Browser,service", IID_IADs, (void**) &pSvc );
  54.     if ( !SUCCEEDED(hr) )
  55.     {
  56.         return hr;
  57.     }
  58.  
  59.     hr = pSvc->Get(L"Dependencies", &var );
  60.     if ( SUCCEEDED(hr) )
  61.     {
  62.         LONG lstart, lend;
  63.         SAFEARRAY  *sa = V_ARRAY( &var );
  64.         VARIANT varItem;
  65.  
  66.         // Get the lower and upper bound
  67.         hr = SafeArrayGetLBound( sa, 1, &lstart );
  68.         hr = SafeArrayGetUBound( sa, 1, &lend );
  69.         
  70.         // Now iterate and print the content
  71.         VariantInit(&varItem);
  72.         printf("Getting service dependencies using IADs :\n");
  73.         for ( long idx=lstart; idx < lend; idx++ )
  74.         {
  75.             hr = SafeArrayGetElement( sa, &idx, &varItem );
  76.             printf("%S ", V_BSTR(&varItem));
  77.             VariantClear(&varItem);
  78.         }
  79.         printf("\n");
  80.         
  81.         VariantClear(&var);
  82.     }
  83.     
  84.     // Clean-up
  85.     if ( pSvc )
  86.     {
  87.         pSvc->Release();
  88.     }
  89.  
  90.  
  91.  
  92.  
  93.     ///////////////////////////////////////////////////////////
  94.     // Using IDirectoryObject to get a multivalue attribute
  95.     // Note: NOT all providers support this interface
  96.     ////////////////////////////////////////////////////////////
  97.     IDirectoryObject    *pDirObject=NULL;
  98.     ADS_ATTR_INFO        *pAttrInfo=NULL;
  99.     DWORD                dwReturn;
  100.     LPWSTR                pAttrNames[]={L"objectClass" };
  101.     DWORD                dwNumAttr=sizeof(pAttrNames)/sizeof(LPWSTR);
  102.  
  103.     hr = ADsGetObject(L"LDAP://CN=Administrator,CN=Users,DC=windows2000,DC=nttest,DC=microsoft,DC=com",
  104.                       IID_IDirectoryObject, 
  105.                       (void**) &pDirObject );
  106.  
  107.     if ( !SUCCEEDED(hr) )
  108.     {
  109.         return 0;
  110.     }
  111.  
  112.  
  113.     
  114.     // Now get the attribute
  115.     hr = pDirObject->GetObjectAttributes( pAttrNames, 
  116.                                           dwNumAttr, 
  117.                                           &pAttrInfo, 
  118.                                           &dwReturn );
  119.     if ( SUCCEEDED(hr) )
  120.     {
  121.         printf("Getting the objectClass multivalue attribute using IDirectoryObject :\n");
  122.         for (DWORD val=0; val < pAttrInfo->dwNumValues; val++, pAttrInfo->pADsValues++) 
  123.         {
  124.                 printf("  %S\n", pAttrInfo->pADsValues->CaseIgnoreString);
  125.         }
  126.         FreeADsMem(pAttrInfo);
  127.     }
  128.     
  129.     //Clean up
  130.     pDirObject->Release();
  131.  
  132.  
  133.     CoUninitialize();
  134.     return 0;
  135. }
  136.