Click here to Skip to main content
Click here to Skip to main content

How to use IDirectoryObject

By , 13 Jun 2002
Rate this:
Please Sign up or sign in to vote.

Introduction

<!-- Add the rest of your HTML here -->

This article will briefly describe how to use the IDirectoryObject in your C++ ADSI application, instead of using the IADs objects intended for scripting clients

Why IDirectoryObject instead of IADs

ADSI (Active Directory Services Interface) is a nice collection of COM objects and gives you easy access to objects and attributes in Active Directory or other LDAP directories. Most programmers use the IADs, IDispatch inherited, interface when programming but using the non-scripting interface IDirectoryObject can improve performance and coding for C++ programmers (if you use scripting languages and/or Visual Basic you have no choice but to use the IADs interface). All ADSI providers must implement the IADs and IDirectoryInterface, so why not take advantage of the features of the IDirectoryObject interface

The major differences between IADs and IDirectoryObject is that the IDirectoryObject will give you direct net access to the directory. IADs uses a property cache from which you read and write attributes using the Get(), get_* and put_* methods and you have to put up with that all the data is delivered using the VARIANT data type. The IDirectoryObject lets you read and write directly through a single request to the object.

Example

Here are three code snippets doing almost the same thing; retrieving the distinguishedName property of a user in Active Directory with a given GUID. The examples can be downloaded above.

IADs

The code in VBScript and in VC++

This first example shows a message box with the distinguishedName and is written in VBScript.

' VBScript (script_example.vbs)
Dim oObject As IADs
Dim vValue As Variant

Set oObject = GetObject("GC://<GUID=b74488fda7dc9c488de9b4bbcdde341f>")
vValue = oObject.Get("distinguishedName")

This example in C++ prints the distinguishedName in a console.

// C++
  IADs* pObject;
  HRESULT hr;
  VARIANT vValue;

  hr = ADsGetObject(CComBSTR(
    L"GC://<GUID=b74488fda7dc9c488de9b4bbcdde341f>"), 
          IID_IADs, 
          (void**)&pObject);
  if(SUCCEEDED(hr)) {
    hr = pObject->Get(CComBSTR(L"distinguishedName"), &vValue);
    if(SUCCEEDED(hr)) {
      
      USES_CONVERSION;
      printf("distinguishedName is %s\n", W2A(vValue.bstrVal));
      VariantClear(&vValue);
    }
    pObject->Release();
  }

IDirectoryObject

This is the same as above but now using the IDirectoryObject. More coding but slightly faster!

  IDirectoryObject* pObject;
  HRESULT hr;
  PADS_ATTR_INFO pAttributeEntries;
  LPWSTR ppAttributeNames[] = {L"distinguishedName"};
  DWORD dwAttributesReturned = 0; 
  hr = ADsGetObject(CComBSTR(
      L"GC://<GUID=b74488fda7dc9c488de9b4bbcdde341f>"), 
          IID_IDirectoryObject, 
          (void**)&pObject);
  if(SUCCEEDED(hr)) {
    hr = pObject->GetObjectAttributes(ppAttributeNames, 
            1, // only one attribute to fetch
            &pAttributeEntries, 
            &dwAttributesReturned );
    if(SUCCEEDED(hr)) {
      if(dwAttributesReturned >0 ) {
        printf("distinguishedName is %S\n", 
            pAttributeEntries->pADsValues[0].CaseIgnoreString);          
      }
      FreeADsMem(pAttributeEntries); // Free up ADSI memory resources
    }
    pObject->Release();
  }

Summary

Use IDirectoryObject when programming in C++ (or plain C), especially if you are reading small or very large amount of attributes of the objects or when reading attributes that the IADs property cache does not cache by default.

Notes

The GUID used in the examples points to a user object in Active Directory, to retrieve the GUID get the objectGUID property of the ADSI object.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Wictor Wilén
Web Developer
Sweden Sweden
No Biography provided

Comments and Discussions

 
Generallinking error PinmemberKingwulf27-Sep-03 12:59 
GeneralRe: linking error PinsussWictor Wilén28-Sep-03 20:31 
GeneralRe: linking error PinsussAnonymous29-Sep-03 7:37 
GeneralRe: linking error PinmemberWictor Wilén29-Sep-03 21:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 14 Jun 2002
Article Copyright 2002 by Wictor Wilén
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid