Platform SDK: Active Directory, ADSI, and Directory Services

Displaying the Object Picker Dialog Box

The sample code in this topic shows the main function of a process that uses the object picker dialog box. The main function performs the following steps:

Note  RegisterClipboardFormat call that registers the CFSTR_DSOP_DS_SELECTION_LIST clipboard format. RegisterClipboardFormat returns an identifier used in the IDataObject::GetData call made by the subroutine that processes the user's selections.

#include <objsel.h>
 
UINT g_cfDsObjectPicker = 
    RegisterClipboardFormat(CFSTR_DSOP_DS_SELECTION_LIST);
 
void _cdecl
main(int argc, char * argv[]) {
 
HRESULT hr = S_OK;
IDsObjectPicker *pDsObjectPicker = NULL;
IDataObject *pdo = NULL;
HWND hwndParent = NULL; // Supply a window handle to your application.
 
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr)) return;
 
do {
 
    // Create an instance of the object picker. 
 
    hr = CoCreateInstance(CLSID_DsObjectPicker,
                  NULL,
                  CLSCTX_INPROC_SERVER,
                  IID_IDsObjectPicker,
                  (void **) &pDsObjectPicker);
    if (FAILED(hr)) break;
 
    // Initialize the object picker instance. 
 
    hr = InitObjectPicker(pDsObjectPicker);
    if (FAILED(hr)) break;
 
    // Invoke the modal dialog.
 
    hr = pDsObjectPicker->InvokeDialog(hwndParent, &pdo);
    if (FAILED(hr)) break;
 
    if (hr == S_OK) {
        ProcessSelectedObjects(pdo);
        pdo->Release();
    }
    else if (hr == S_FALSE)
        printf("User canceled object picker dialog\n");
 
} while (0);
 
pDsObjectPicker->Release();
CoUninitialize();
}