Click here to Skip to main content
12,550,111 members (45,012 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked

How to use IDirectoryObject

, 13 Jun 2002
Rate this:
Please Sign up or sign in to vote.
How to use the IDirectoryObject interface instead of using the IADs(IDispatch) objects


<!-- 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.


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.


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;
  VARIANT vValue;

  hr = ADsGetObject(CComBSTR(
  if(SUCCEEDED(hr)) {
    hr = pObject->Get(CComBSTR(L"distinguishedName"), &vValue);
    if(SUCCEEDED(hr)) {
      printf("distinguishedName is %s\n", W2A(vValue.bstrVal));


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

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


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.


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.


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

You may also be interested in...


Comments and Discussions

Generallinking error Pin
Kingwulf27-Sep-03 12:59
memberKingwulf27-Sep-03 12:59 
GeneralRe: linking error Pin
Wictor Wilén28-Sep-03 20:31
sussWictor Wilén28-Sep-03 20:31 
GeneralRe: linking error Pin
Anonymous29-Sep-03 7:37
sussAnonymous29-Sep-03 7:37 
GeneralRe: linking error Pin
Wictor Wilén29-Sep-03 21:07
memberWictor Wilén29-Sep-03 21:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 14 Jun 2002
Article Copyright 2002 by Wictor Wilén
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid