Platform SDK: Active Directory, ADSI, and Directory Services

Processing the Selected Objects

This topic describes a subroutine that retrieves and processes information about the objects the user selected from the object picker dialog box.

The ProcessSelectedObjects subroutine calls the GetData method of the IDataObject instance returned by the object picker dialog box. GetData retrieves a STGMEDIUM structure containing a handle to the global memory block. Calling the GlobalLock function on this handle returns a pointer to a DS_SELECTION_LIST structure containing data describing the user's selections.

void ProcessSelectedObjects(
    IDataObject *pdo  // Data object containing user's selections
) {
 
HRESULT hr = S_OK;
BOOL fGotStgMedium = FALSE;
PDS_SELECTION_LIST pDsSelList = NULL;
ULONG i;
STGMEDIUM stgmedium = {
    TYMED_HGLOBAL, 
    NULL, 
    NULL
};
FORMATETC formatetc = {
    g_cfDsObjectPicker,
    NULL,
    DVASPECT_CONTENT,
    -1,
    TYMED_HGLOBAL
};
 
do {
    // Get the global memory block containing the user's selections.
 
    hr = pdo->GetData(&formatetc, &stgmedium);
    if (FAILED(hr)) break;
    fGotStgMedium = TRUE;
 
    // Retrieve pointer to DS_SELECTION_LIST structure.
 
    pDsSelList = (PDS_SELECTION_LIST) GlobalLock(stgmedium.hGlobal);
    if (!pDsSelList) break;
 
    // Loop through DS_SELECTION array of selected objects.
 
    for (i = 0; i < pDsSelList->cItems; i++) {
        printf("Object %u'\n", i);
        printf("  Name '%ws'\n", 
                pDsSelList->aDsSelection[i].pwzName);
        printf("  Class '%ws'\n", 
                pDsSelList->aDsSelection[i].pwzClass);
        printf("  Path '%ws'\n", 
                pDsSelList->aDsSelection[i].pwzADsPath);
        printf("  UPN '%ws'\n", 
                pDsSelList->aDsSelection[i].pwzUPN);
    }
 
    GlobalUnlock(stgmedium.hGlobal);
} while (0);
 
if (fGotStgMedium) 
    ReleaseStgMedium(&stgmedium);
 
}