Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Getting the Distinguished Name of the Domain

The following code fragments use serverless binding to get a binding string for the default naming context on the domain associated with the current security context of the calling thread.

[Visual Basic]
Dim rootDSE as IADs
Dim sADsPath as String
 
On Error GoTo NonAD
 
Set rootDSE = GetObject("LDAP://rootDSE")
SADsPath = "LDAP://" & rootDSE.Get("defaultNamingContext")
'Clean up
Set rootDSE = Nothing
NonAD:
    'Notify couldn't bind to rootDSE
[C++]
IADs *pRootDSE;
IADs *pObject;
HRESULT hr;
VARIANT var;
LPOLESTR szDSPath = new OLECHAR[MAX_PATH];
 
hr = ADsGetObject(L"LDAP://rootDSE",
                  IID_IADs,
                  (void**)&pRootDSE);
if (SUCCEEDED(hr))
{
    hr = pRootDSE->Get(L"defaultNamingContext",&var);
    wcscpy(szDSPath,L"LDAP://");
    wcscat(szDSPath,var.bstrVal);
    hr = ADsGetObject(szDSPath, IID_IADs, (void**) &pObject);
}
    // Clean up
    pRootDSE->Release();
    pObject->Release();
    VariantClear(&var);