Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for IDirectoryObject

[C++]

// IDirObj.cpp : Demonstrates the use of IDirectoryObject.
//
 
#define _WIN32_DCOM
#define UNICODE
#define _UNICODE
 
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "activeds.h"
#include "tchar.h"
 
void main(void)
{
    IDirectoryObject    *pDirectoryObj;
    DWORD                dwNumAttrToGet;
    DWORD                dwNumAttrGot;
    LPWSTR               AttribNames[10];
    ADS_ATTR_INFO       *pPropEntries;
    HRESULT              hr;
 
    // Call to initialize COM Library
    CoInitialize(NULL);
 
    // Bind to a known object asking for the IDirectoryObject Interface.
    hr = ADsOpenObject( L"LDAP://MyMachine/CN=Administrator,CN=Users,DC=MyDomain,DC=Fabrikam,DC=COM",
                L"Administrator",
                L"",
                ADS_SECURE_AUTHENTICATION,
                IID_IDirectoryObject,
                (void**)&pDirectoryObj);
 
    if(FAILED(hr)) {
        // Error code goes here.
        printf("ADsOpenObject Failed\n");
        exit(1);
    }
 
    // Create a list of attributes to get.
    AttribNames[0] = L"distinguishedName";
    dwNumAttrToGet = 1;
 
    // Call GetObjectAttributes with the 
    // list of attributes and a pointer to 
    // an array of ADS_ATTRIB_INFO structs.
    hr = pDirectoryObj->GetObjectAttributes( AttribNames,
                                             dwNumAttrToGet,
                                             &pPropEntries,
                                             &dwNumAttrGot);
    if(FAILED(hr)) {
        // Error code goes here.
        pDirectoryObj->Release();
        printf("IDirectoryObject::GetObjectAttributes Failed\n");
        exit(1);
    }
 
    printf("distinguishedName: %S\n", 
            pPropEntries[0].pADsValues->PrintableString);
 
    // Cleanup.
    pDirectoryObj->Release();
    FreeADsMem(pPropEntries);
 
    // Call to uninitialize COM Library
    CoUninitialize();
 
}