Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Binding to the Users Container and Displaying the ADsPath

The following C++ program binds to the users container of the current user's domain and displays the users container's ADsPath (showing a live example of a WKGUID binding string). It also contains a function that binds to the specified well-known object (WKO) based on its WKO GUID in the current user's domain.

You can use similar code to bind to other wellknown containers. To bind to the Deleted Objects container using the ADsOpenObject function, you must specify the ADS_FAST_BIND option.

#include <wchar.h>
#include <objbase.h>
 
//For ADSI
#include <activeds.h>
 
//Make sure you define UNICODE
//Need to define version 5 for Windows 2000
#define _WIN32_WINNT 0x0500
#include <ntdsapi.h>
 
HRESULT GetWKDomainObject(LPOLESTR szBindableWKGUID, //IN. Bindable string GUID of well-known object.
                          IADs **ppObject //OUT. Return a pointer to the specified well-known object.
                          );
 
 
void wmain( int argc, wchar_t *argv[ ])
{
wprintf(L"This program finds the user's container in the current Window 2000 domain\n");
//Intialize COM
CoInitialize(NULL);
HRESULT hr = S_OK;
//Get rootDSE and the domain container's DN.
IADs *pObject = NULL;
hr = GetWKDomainObject(GUID_USERS_CONTAINER_W, //IN. Bindable string GUID of well-known object.
                          &pObject //OUT. Return a pointer to the specified well-known object.
                          );
if (FAILED(hr))
{
   wprintf(L"Not Found. Could not bind to the user container.\n");
   if (pObject)
     pObject->Release();
   return;
}
 
BSTR bstr;
pObject->get_ADsPath(&bstr);
wprintf (L"ADsPath of users Container: %s\n", bstr);
FreeADsStr(bstr);
if (pObject)
    pObject->Release();
 
//uninitialize COM
CoUninitialize();
return;
 
}
 
// This function gets the specified well-known object for the current user's domain.
 
HRESULT GetWKDomainObject(LPOLESTR szBindableWKGUID, //IN. Bindable string GUID of well-known object.
                          IADs **ppObject //OUT. Return a pointer to the specified well-known object.
                          )
{
HRESULT hr = E_FAIL;
//Get rootDSE and the domain container's DN.
IADs *pObject = NULL;
LPOLESTR szPath = new OLECHAR[MAX_PATH];
VARIANT var;
hr = ADsOpenObject(L"LDAP://rootDSE",
                 NULL,
                 NULL,
                 ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
                 IID_IADs,
                 (void**)&pObject);
 
//Get current domain DN.
if (SUCCEEDED(hr))
{
    hr = pObject->Get(L"defaultNamingContext",&var);
    if (SUCCEEDED(hr))
    {
        //Build the WKGUID binding string.
        wcscpy(szPath,L"LDAP://");
        wcscat(szPath,L"<WKGUID=");
        wcscat(szPath,szBindableWKGUID);
        wcscat(szPath,L",");
        wcscat(szPath,var.bstrVal);
        wcscat(szPath,L">");
        //Print the binding string.
        //wprintf(L"WKGUID binding string: %s\n",szPath);
        VariantClear(&var);
        //Bind to the well-known object.
        hr = ADsOpenObject(szPath,
                         NULL,
                         NULL,
                         ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
                         IID_IADs,
                         (void**)ppObject);
        if (FAILED(hr))
        {
            if (*ppObject)
            {
              (*ppObject)->Release();
              (*ppObject) = NULL;
            }
        }
    }
}
if (pObject)
  pObject->Release();
 
return hr;
}