home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / ActiveDir / PropertyList / vc / main.cpp < prev   
Encoding:
C/C++ Source or Header  |  1999-01-08  |  5.8 KB  |  236 lines

  1.  
  2.  
  3. // ADsPropertyList Main.cpp
  4. // Active Directory Sample
  5. /*
  6.     Demonstrates use of the following 
  7.     ADSI COM interfaces from C++:
  8.  
  9.         IADs
  10.         IADsPropertyList
  11.         IADsPropertyEntry
  12.         IADsPropertyValue
  13.  
  14.     ADSI Functions:
  15.         ADsOpenObject()
  16.  
  17.  
  18.     This sample binds to a server using RootDSE, then IADsPropertyList to list all 
  19.     the properties on this object
  20.  
  21. */
  22.  
  23.  
  24. //
  25.  
  26.  
  27. #define INC_OLE2
  28. #define UNICODE 1
  29. #define _WIN32_DCOM
  30.  
  31. #include <windows.h>
  32. #include <winuser.h>
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <malloc.h>
  37. #include <winldap.h>
  38. #include <adsiid.h>
  39. #include <string.h>
  40. #include <activeds.h>
  41.  
  42. #include "ADSIhelpers.h"
  43.  
  44. void main()
  45. {
  46.     // COM result variable
  47.     HRESULT hr;
  48.  
  49.     // Interface Pointers
  50.     IDirectorySearch    *   pDSSearch=NULL;
  51.     IADs                *   pIADs = NULL;
  52.     IADsPropertyList    *   pIADpl = NULL;
  53.     VARIANT                 vRet;
  54.     LONG                    lCount;  
  55.     IADs                *   pIADsrootDSE = NULL; 
  56.     BSTR                    bsNamingContext;
  57.     VARIANT                 vResult;
  58.     WCHAR                   wszActualLDAPPath[1024];
  59.  
  60.     VariantInit(&vRet);
  61.     
  62.     // Initialize COM
  63.     CoInitialize(0);
  64.     
  65.     // Bind Through RootDSE
  66.     _putws(L"Binding to a server using LDAP://rootDSE");
  67.  
  68.     hr = ADsGetObject(  L"LDAP://rootDSE", IID_IADs,(void **)&pIADsrootDSE);
  69.  
  70.     if (FAILED(hr))
  71.         return;
  72.  
  73.      // Get the defaultNamingContext attribute and for binding
  74.     // to the actual DC
  75.     bsNamingContext = SysAllocString(L"defaultNamingContext");
  76.     VariantInit(&vResult);
  77.  
  78.     // Get the defaultNamingContext Attribute
  79.     hr = pIADsrootDSE->Get(bsNamingContext,&vResult);
  80.  
  81.     if (SUCCEEDED(hr))
  82.     {
  83.             // Make sure it's a BSTR
  84.             hr = VariantChangeType(&vResult,&vResult,NULL,VT_BSTR);
  85.  
  86.             if (SUCCEEDED(hr))
  87.             {
  88.                 // Build a binding string
  89.                 wsprintf(wszActualLDAPPath,L"LDAP://%s",vResult.bstrVal );
  90.                 wprintf(L"\nBinding to the path %s\n",wszActualLDAPPath);
  91.                 
  92.                 // Bind to the actual server
  93.                 hr = ADsGetObject(  wszActualLDAPPath, IID_IADs,(void **)&pIADs);
  94.             }
  95.             VariantClear(&vResult);
  96.     }  
  97.     SysFreeString(bsNamingContext);
  98.     
  99.     // Relase the RootDse binding...
  100.     pIADsrootDSE->Release();
  101.     pIADsrootDSE = NULL;
  102.  
  103.     if (FAILED(hr))
  104.         return;
  105.     
  106.     wprintf(L"\n\nSuccessfully bound to %s\n",wszActualLDAPPath);
  107.     
  108.     // Print some general info about the object we are bound to
  109.     PrintIADSObject(pIADs);
  110.     
  111.     if (FAILED(hr))
  112.         return;
  113.  
  114.     wprintf(L"\n\nEnumerating this object's properties using the IADsPropertyList interface\n\n");
  115.         
  116.     //Call GetInfo to load all the properties. Call GetInfoEx to load only specific ones.
  117.     hr = pIADs->GetInfo();
  118.  
  119.     if (FAILED(hr))
  120.         return;
  121.     
  122.     //QueryInterface() for IADsPropertyList ptr.            
  123.     hr = pIADs->QueryInterface( IID_IADsPropertyList,(void **)&pIADpl);
  124.  
  125.     if (FAILED(hr))
  126.         return;
  127.  
  128.     // Retrieve a COUNT of all the properties in the propertylist
  129.     hr = pIADpl->get_PropertyCount(  &lCount  );
  130.  
  131.     // Output the property count obtained from the IADsPropertyList interface
  132.     printf("\n The Object has %d properties\n",lCount);
  133.     
  134.     // Move to the FIRST property in the list
  135.     hr = pIADpl->Next(&vRet);
  136.     CheckHRESULT(hr,"pIADpl->Next(&vRet);");
  137.  
  138.      for (long lElement = 0; lElement < lCount; lElement++)
  139.     {
  140.         
  141.         IDispatch * pDisp = V_DISPATCH(&vRet);
  142.         IADsPropertyEntry * pAdsEntry = NULL;
  143.         
  144.         // QueryInterface for a IADsPropertyEntry interace on the current property
  145.         hr = pDisp->QueryInterface(IID_IADsPropertyEntry,(void**) &pAdsEntry);
  146.  
  147.         if (SUCCEEDED(hr))
  148.         {
  149.             BSTR  bsName ;
  150.             VARIANT vValue;
  151.             VariantInit(&vValue);
  152.  
  153.             // Get the NAME of the current property
  154.             hr = pAdsEntry->get_Name( &bsName);
  155.             
  156.             if (SUCCEEDED(hr))
  157.             {
  158.                 wprintf(L"\n NAME:%s \t\n",(LPOLESTR)bsName);
  159.                 SysFreeString(bsName);
  160.             }
  161.  
  162.             // Get the values
  163.             hr = pAdsEntry->get_Values(&vValue);
  164.  
  165.             puts("The Values Variant style is :");
  166.             puts(GetVariantStyle(vValue.vt));
  167.  
  168.             if (SUCCEEDED(hr))
  169.             {
  170.                 // We should now have a VARAIANT ARRAY of IDispath *'s
  171.                 // So, read the safe array, enumeraing each IDispatch * 
  172.                 // The IDispatch * is then QI'd for the IADsPropertyValue 
  173.                 // interface. This interface provides the property information
  174.                 if (HAS_BIT_STYLE(vValue.vt,VT_ARRAY))
  175.                 {
  176.                     LONG dwSLBound = 0;
  177.                     LONG dwSUBound = 0;
  178.                     LONG i;
  179.  
  180.                     hr = SafeArrayGetLBound(V_ARRAY(&vValue),1,(long FAR *)&dwSLBound);
  181.  
  182.                     hr = SafeArrayGetUBound(V_ARRAY(&vValue),1,(long FAR *)&dwSUBound);
  183.  
  184.                     for (i = dwSLBound; i <= dwSUBound; i++) 
  185.                     {
  186.                         VARIANT v;
  187.                         VariantInit(&v);
  188.                         hr = SafeArrayGetElement(V_ARRAY(&vValue),(long FAR *)&i,&v);
  189.                         
  190.                         if (SUCCEEDED(hr))
  191.                         {
  192.                             if (HAS_BIT_STYLE(v.vt,VT_DISPATCH))
  193.                             {
  194.                                 IDispatch * pDispEntry = V_DISPATCH(&v);
  195.                                 IADsPropertyValue * pAdsPV = NULL;
  196.                                 
  197.                                 hr = pDispEntry->QueryInterface(IID_IADsPropertyValue,(void **) &pAdsPV);
  198.  
  199.                                 if (SUCCEEDED(hr))
  200.                                 {    
  201.                                     BSTR bValue;
  202.  
  203.                                     // Get the value as a BSTR
  204.                                     hr = GetIADsPropertyValueAsBSTR(&bValue,pAdsEntry,pAdsPV);
  205.  
  206.                                     if (SUCCEEDED(hr))
  207.                                     {
  208.                                         wprintf(L"<%s>\n",(LPOLESTR)bValue);
  209.                                         SysFreeString(bValue);
  210.                                     }
  211.                                     pAdsPV->Release();
  212.                                     pAdsPV =NULL;
  213.                                 }
  214.                             }
  215.                             else
  216.                                 puts("!!NO DISPATCH ENTRY!!");
  217.                         
  218.                             VariantClear(&v);
  219.                         }
  220.                     }
  221.                     VariantClear(&vValue);
  222.                 }
  223.                 else
  224.                     wprintf(L" NAME:%s \t",(LPOLESTR)V_BSTR(&vValue));
  225.  
  226.             }
  227.             pAdsEntry->Release();
  228.             pAdsEntry = NULL;
  229.         }
  230.         hr = pIADpl->Next(&vRet);
  231.     }
  232.  
  233.     CoUninitialize();
  234.  
  235. }
  236.