FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Dobbs M-Dev
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
May 01, 2001

Using the Windows 2000 Object Picker

(Page 3 of 3)
May 2001/Using the Windows 2000 Object Picker/Listing 1

Listing 1: op.cpp — Sample object picker usage

// OP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

///////////////////////////////////////////////////////////////////
// PrintSelection - Print to the command-line the list of all the
// selected objects in the Object Picker dialog-box
///////////////////////////////////////////////////////////////////
HRESULT PrintSelection(IDataObject* pSelectionDataObject)
{
    STGMEDIUM stg = { 0 };          // Storage used to get the data
    FORMATETC ftc = { 0 };          // Picker's clipboard format
    PDS_SELECTION_LIST pDsSelList;  // Selection list structure
    UINT iSel;                      // Current analyzed item
    int cAttributes;                // Number of fetched attributes
    int iAttrib;                    // Printed attribute index
    VARIANT vAttrib;                // Current printed attribute
    
    // Prepare the STGMEDIUM and FORMATETC to get the data
    // through the HGLOBAL managed by the dataobject
    stg.tymed = ftc.tymed = TYMED_HGLOBAL;
    ftc.cfFormat = RegisterClipboardFormat(
                                     CFSTR_DSOP_DS_SELECTION_LIST);
    ftc.dwAspect = DVASPECT_CONTENT;
    ftc.lindex = -1;

    // Get the data and cast it to a DS_SELECTION_LIST pointer
    HRESULT hr;
    if (SUCCEEDED(hr = pSelectionDataObject->GetData(&ftc, &stg)))
    {
        pDsSelList = (PDS_SELECTION_LIST)GlobalLock(stg.hGlobal);
        cAttributes = pDsSelList->cFetchedAttributes;
        
        // Enumerate all of the selected items and print them
        wprintf(L"%i selected items\n", pDsSelList->cItems);
        for (iSel = 0; iSel < pDsSelList->cItems; iSel++)
        {
            // Print the object name
            wprintf(L"%s", pDsSelList->aDsSelection[iSel].pwzName);
            
            // Show fetched attributes
            for (iAttrib = 0; iAttrib < cAttributes; iAttrib++)
            {
                // Convert any VARIANT to a BSTR
                VariantInit(&vAttrib);
                if (SUCCEEDED(VariantChangeType(&vAttrib,
                    &pDsSelList->aDsSelection[iSel].
                    pvarFetchedAttributes[iAttrib], 
                    0, VT_BSTR)))
                {
                    wprintf(L"%s%s", 
                        iAttrib == cAttributes ? L"" : L";", 
                        vAttrib.bstrVal);

                    VariantClear(&vAttrib); // Free memory
                }

                VariantClear(&pDsSelList->aDsSelection[iSel].
                    pvarFetchedAttributes[iAttrib]);    // Free
            }

            wprintf(L"\n");
        }

           ReleaseStgMedium(&stg);    // Free memory
    }

    return S_OK;
}

///////////////////////////////////////////////////////////////////
// SelectUsersOrComputers - Show the Object Picker dialog and
// return a Data Object containing the user selection
///////////////////////////////////////////////////////////////////
HRESULT SelectUsersOrComputers(HWND hWndParent, 
                               bool bUsers, 
                               IDataObject** ppSelectionDataObject)
{  
    // Create the Object Picker COM component
    LPCWSTR attribs[] = { L"mail", L"company" };
    IDsObjectPicker* pObjectPicker;
    HRESULT hr;
    if (SUCCEEDED(hr = CoCreateInstance(CLSID_DsObjectPicker, NULL, 
                        CLSCTX_INPROC_SERVER, IID_IDsObjectPicker, 
                        (void**)&pObjectPicker)))
    {
        // Specify the scopes to browse for: up/down level domains
        DSOP_SCOPE_INIT_INFO scopeInit = { 0 };
        scopeInit.cbSize = sizeof(DSOP_SCOPE_INIT_INFO);
        scopeInit.flType = DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN | 
                           DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN;

        // Set which Active Directory objects we want to filter
        if (bUsers)
        {
            scopeInit.FilterFlags.Uplevel.flBothModes = 
                DSOP_FILTER_USERS;
            scopeInit.FilterFlags.flDownlevel = 
                DSOP_DOWNLEVEL_FILTER_USERS;
        }
        else
        {
            scopeInit.FilterFlags.Uplevel.flBothModes = 
                DSOP_FILTER_COMPUTERS;
            scopeInit.FilterFlags.flDownlevel = 
                DSOP_DOWNLEVEL_FILTER_COMPUTERS;
        }

        // Set the initialization structure for the Dialog-Box
        DSOP_INIT_INFO initInfo = { 0 };
        initInfo.cbSize = sizeof(DSOP_INIT_INFO);
        initInfo.cDsScopeInfos = 1;
        initInfo.flOptions = DSOP_FLAG_MULTISELECT; //Multi-select.
        initInfo.aDsScopeInfos = &scopeInit;

        // Fetch the e-mail and Company attributes for users
        if (bUsers)
        {
            initInfo.cAttributesToFetch = 2;
            initInfo.apwzAttributeNames = attribs;
        }

        // Initialize and display the modal dialog-box
        if (SUCCEEDED(hr = pObjectPicker->Initialize(&initInfo)))
            hr = pObjectPicker->InvokeDialog(hWndParent, 
                                            ppSelectionDataObject);

        // Release the component and return the HRESULT
        pObjectPicker->Release();
    }

    return hr;
}

int wmain(int argc, WCHAR* argv[])
{
    bool bUsers;                 // true if we are browsing users
    IDataObject* pSelection = 0; // Selection returned by the OP

    // Syntax analysis
    if (argc != 2)
    {
        wprintf(L"Syntax: %s [users | computers]", argv[0]);
        return 1;
    }
    else bUsers = !lstrcmp(argv[1], L"users");

    // COM STA Initialization
    CoInitialize(NULL);

    // Show the Object Picker dialog-box and get the data object
    if (S_OK == SelectUsersOrComputers(NULL, bUsers, &pSelection))
    {
        PrintSelection(pSelection);
        pSelection->Release();
    }

    // COM Uninit.
    CoUninitialize();

    return 0;
}

//End of File
Previous Page | 1 | 2 | 3
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK