Platform SDK: Active Directory, ADSI, and Directory Services

Domain Browser

Using the IDsBrowseDomainTree COM object, an application can display a domain browser dialog box and get back the DNS name of the domain selected by the user. The IDsBrowseDomainTree interface also has other methods to enumerate and retrieve information about all domain trees and domains within a forest.

The following function displays the domain browser dialog box where the user selects a domain. The function returns the DNS name of the domain:

// To compile this code, you must create static variables for the 
// GUIDs defined in dsclient.h. To do this, include initguid.h before
// including dsclient.h.
#include <initguid.h>
#include <dsclient.h>
 
HRESULT BrowseForDomain(HWND hWnd,//Handle to window that should own the browse dialog.
                LPOLESTR *ppDomainDNSName //Return the DNS name of selected domain.
                    )
{
HRESULT hr = E_FAIL;
IDsBrowseDomainTree *pDsDomains = NULL;
//CoCreate the domain tree browser.
hr = ::CoCreateInstance(CLSID_DsDomainTreeBrowser,
                NULL,
                CLSCTX_INPROC_SERVER,
                IID_IDsBrowseDomainTree,
                (void **)(&pDsDomains));
if (SUCCEEDED(hr))
{
hr = pDsDomains->BrowseTo(
                hWnd, //Handle to window that owns the dialog.
                ppDomainDNSName, //String to return DNS Name.
                0L //Flags
            );
//User must free string with CoTaskMemFree.
}
if (pDsDomains)
  pDsDomains->Release();
return hr;
}