Platform SDK: Active Directory, ADSI, and Directory Services

Container Browser

Using the DsBrowseForContainer function, an application can display a container browser dialog box and get back the path to the container selected by the user (ADS_FORMAT_X500_NO_SERVER is the default—see the ADS_FORMAT_ENUM enumeration for the list of all formats) and optionally the ldapDisplayName of the container's object class. The function takes a pointer to a DSBROWSEINFO structure that contains the caption and dialog box text, ADsPath to the root of the browser tree, credentials (user and password), and flags to control the appearance of the dialog box.

The following function displays the container browser dialog box where the user selects a container. The function returns the ADsPath and class of the container:

//Must include DSCLIENT.H
 
HRESULT BrowseForContainer(HWND hWnd,//Handle to window that should own the browse dialog.
                    LPOLESTR szRootPath, //Root of the browse tree. NULL for entire forest.
                    LPOLESTR *ppContainerADsPath, //Return the ADsPath of the selected container.
                    LPOLESTR *ppContainerClass //Return the ldapDisplayName of the container's class.
                    )
{
HRESULT hr = E_FAIL;
DSBROWSEINFO dsbi;
OLECHAR szPath[MAX_PATH*2];
OLECHAR szClass[MAX_PATH];
DWORD result;
 
if (!ppContainerADsPath)
  return E_POINTER;
 
::ZeroMemory( &dsbi, sizeof(dsbi) );
dsbi.hwndOwner = hWnd;
dsbi.cbStruct = sizeof (DSBROWSEINFO);
dsbi.pszCaption = "Browse for Container"; // The caption (titlebar text)
dsbi.pszTitle = "Select a container."; //Text for the dialog.
dsbi.pszRoot = szRootPath; //ADsPath for the root of the tree to display in the browser.
                //Specify NULL with DSBI_ENTIREDIRECTORY flag for entire forest.
                //NULL without DSBI_ENTIREDIRECTORY flag displays current domain rooted at LDAP.
dsbi.pszPath = szPath; //Pointer to a unicode string buffer.
dsbi.cchPath = sizeof(szPath)/sizeof(OLECHAR);//count of characters for buffer.
dsbi.dwFlags = DSBI_INCLUDEHIDDEN | //Include hidden objects
            DSBI_IGNORETREATASLEAF| //Ignore the treat as leaf flag on the object for display purposes.
            DSBI_RETURN_FORMAT | //Return the path to object in format specified in dwReturnFormat
            DSBI_RETURNOBJECTCLASS; //Return the object class
dsbi.pfnCallback = NULL;
dsbi.lParam = 0;
dsbi.dwReturnFormat = ADS_FORMAT_X500; //Specify the format.
                    //This one returns an ADsPath. See ADS_FORMAT_ENUM enumeration in IADS.H
dsbi.pszObjectClass = szClass; //Pointer to a unicode string buffer.
dsbi.cchObjectClass = sizeof(szClass)/sizeof(OLECHAR);//count of characters for buffer.
 
//if root path is NULL, make the forest the root.
if (!szRootPath)
  dsbi.dwFlags |= DSBI_ENTIREDIRECTORY;
 
 
 
//Display browse dialog box.
result = DsBrowseForContainer( &dsbi ); // returns -1, 0, IDOK or IDCANCEL
if (result == IDOK)
{
    //Allocate memory for string
    *ppContainerADsPath = (OLECHAR *)CoTaskMemAlloc (sizeof(OLECHAR)*(wcslen(szPath)+1));
    if (*ppContainerADsPath)
    {
        wcscpy(*ppContainerADsPath, szPath);
        //Caller must free using CoTaskMemFree
        hr = S_OK;
    }
    else
        hr=E_FAIL;
    if (ppContainerClass)
    {
        //Allocate memory for string
        *ppContainerClass = (OLECHAR *)CoTaskMemAlloc (sizeof(OLECHAR)*(wcslen(szClass)+1));
    if (*ppContainerClass)
    {
        wcscpy(*ppContainerClass, szClass);
        //Call must free using CoTaskMemFree
        hr = S_OK;
    }
    else
        hr=E_FAIL;
    }
}
else
    hr = E_FAIL;
 
return hr;
 
}