Click here to Skip to main content
15,566,394 members
Articles / Desktop Programming / ATL
Posted 12 Jun 2002


24 bookmarked

How to use IDirectoryObject

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Jun 20022 min read
How to use the IDirectoryObject interface instead of using the IADs(IDispatch) objects


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

Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Generallinking error Pin
Kingwulf27-Sep-03 13:59
Kingwulf27-Sep-03 13:59 
GeneralRe: linking error Pin
Wictor Wilén28-Sep-03 21:31
Wictor Wilén28-Sep-03 21:31 
GeneralRe: linking error Pin
Anonymous29-Sep-03 8:37
Anonymous29-Sep-03 8:37 
After link it to the libraries I still get this one linking error:

error LNK2001: unresolved external symbol _IID_IDirectoryObject
m:\siebel\bin\w32ud\SiebelAx_Calendar_4c5c6192-b2b9-4f9e-b4f8-00f687b2a77e.dll : fatal erro
r LNK1120: 2 unresolved externals

I only link it in object compilation linking and I do not import any lib in my idl file
GeneralRe: linking error Pin
Wictor Wilén29-Sep-03 22:07
Wictor Wilén29-Sep-03 22: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.