Export (0) Print
Expand All

ICommonQuery::OpenQueryWindow method

The ICommonQuery::OpenQueryWindow method displays the directory service query dialog. This method does not return until the dialog box has been closed by the user.

Syntax


HRESULT OpenQueryWindow(
  [in]   HWND hwdnParent,
  [in]   LPOPENQUERYWINDOW *pQueryWnd,
  [out]  IDataObject **ppDataObj
);

Parameters

hwdnParent [in]

Contains the handle of the window to use as the parent to the query dialog box. This parameter can be NULL if no parent is specified.

pQueryWnd [in]

Address of an OPENQUERYWINDOW structure that defines the query to perform and the characteristics of the query dialog.

ppDataObj [out]

Address of an IDataObject interface pointer that receives the results of the query. This parameter only receives valid data if this method returns S_OK. This IDataObject supports the following clipboard formats.

ValueMeaning
CFSTR_DSOBJECTNAMES

Contains data about objects selected in the directory service query dialog box.

CFSTR_DSQUERYPARAMS

Contains data about the query performed by the directory service query dialog box.

CFSTR_DSQUERYSCOPE

Contains data about the scope of the query performed by the directory service query dialog box.

 

Return value

Returns a standard HRESULT value including the following.

Return codeDescription
S_OK

The method was successful and the user clicked OK. ppDataObj receives a valid IDataObject pointer.

S_FALSE

The method was successful, but the user canceled the dialog box and did not click OK. ppDataObj receives NULL.

E_FAIL

An unspecified error occurred.

E_INVALIDARG

One or more parameters are invalid.

E_OUTOFMEMORY

A memory allocation failure occurred.

 

Examples

The following code example shows how to invoke the directory service query dialog box to search for printers in Active Directory Domain Services.


//  Add dsquery.dll to your project
//  Add uuid.lib to your project
//  Add activeds.lib to your project

#include "stdafx.h"
#include "activeds.h"
#include "cmnquery.h"
#include "dsquery.h"
#include "shlobj.h"
#include "dsclient.h"
#include "windows.h"


//  The DisplayDSQueryParams() function gets the query parameters
HRESULT DisplayDSQueryParams(IDataObject *pdo)
{
    if(NULL == pdo)
    {
        return E_INVALIDARG;
    }

    HRESULT hr;
    FORMATETC feQueryParams;
    STGMEDIUM stmQueryParams;

    //  Get the CFSTR_DSQUERYPARAMS data from the data object.
    feQueryParams.cfFormat = RegisterClipboardFormat(CFSTR_DSQUERYPARAMS);
    feQueryParams.ptd = NULL;
    feQueryParams.dwAspect = DVASPECT_CONTENT;
    feQueryParams.lindex = -1;
    feQueryParams.tymed = TYMED_HGLOBAL;
    hr = pdo->GetData(&feQueryParams, &stmQueryParams);
    if(SUCCEEDED(hr))
    {
        //  Lock the DSQUERYPARAMS structure.
        LPDSQUERYPARAMS pqp = (LPDSQUERYPARAMS)GlobalLock(stmQueryParams.hGlobal);
        if(pqp)
        {
            //  Get the query string.
            LPWSTR pwszQuery = (LPWSTR)((LPBYTE)pqp + pqp->offsetQuery);
            OutputDebugStringW(pwszQuery);
            OutputDebugString(TEXT("\n"));

            //  Get the column headers.
            OutputDebugString(TEXT("Column Headers:\n"));
            int i;
            for(i = 0; i < pqp->iColumns; i++)
            {
                TCHAR szHeader[MAX_PATH];
                LoadString(pqp->hInstance, pqp->aColumns[i].idsName, szHeader, MAX_PATH);
                OutputDebugString(TEXT("\t"));
                OutputDebugString(szHeader);
                OutputDebugString(TEXT("\n"));
            }

            //  Get the attribute names.
            OutputDebugString(TEXT("Attribute Names:\n"));
            for(i = 0; i < pqp->iColumns; i++)
            {
                switch(pqp->aColumns[i].offsetProperty)
                {
                case DSCOLUMNPROP_ADSPATH:
                    OutputDebugString(TEXT("\tDSCOLUMNPROP_ADSPATH\n"));
                    break;

                case DSCOLUMNPROP_OBJECTCLASS:
                    OutputDebugString(TEXT("\tDSCOLUMNPROP_OBJECTCLASS\n"));
                    break;

                default:
                    LPWSTR pwszAttr = (LPWSTR)((LPBYTE)pqp + pqp->aColumns[i].offsetProperty);
                    OutputDebugString(TEXT("\t"));
                    OutputDebugStringW(pwszAttr);
                    OutputDebugString(TEXT("\n"));
                    break;
                }
            }

            //  Unlock the DSQUERYPARAMS structure.
            GlobalUnlock(stmQueryParams.hGlobal);
        }

        //  Release the DSQUERYPARAMS memory.
        ReleaseStgMedium(&stmQueryParams);
    }

    return hr;
}


//  The DisplayDSObjectNames() function gets the DS object information
HRESULT DisplayDSObjectNames(IDataObject *pdo)
{
    if(NULL == pdo)
    {
        return E_INVALIDARG;
    }

    HRESULT hr;
    FORMATETC feObjectNames;
    STGMEDIUM stmObjectNames;

    //  Get the CFSTR_DSOBJECTNAMES data from the data object. 
    feObjectNames.cfFormat = RegisterClipboardFormat(CFSTR_DSOBJECTNAMES);
    feObjectNames.ptd = NULL;
    feObjectNames.dwAspect = DVASPECT_CONTENT;
    feObjectNames.lindex = -1;
    feObjectNames.tymed = TYMED_HGLOBAL;
    hr = pdo->GetData(&feObjectNames, &stmObjectNames);
    if(SUCCEEDED(hr))
    {
        //  Lock the DSOBJECTNAMES structure.
        LPDSOBJECTNAMES pon = (LPDSOBJECTNAMES)GlobalLock(stmObjectNames.hGlobal);
        if(pon)
        {
            UINT i;

            //  Get the ADsPaths for the objects.
            OutputDebugString(TEXT("ADsPaths:\n"));
            for(i = 0; i < pon->cItems; i++)
            {
                LPWSTR pwszADsPath = (LPWSTR)((LPBYTE)pon + pon->aObjects[i].offsetName);
                OutputDebugString(TEXT("\t"));
                OutputDebugStringW(pwszADsPath);
                OutputDebugString(TEXT("\n"));
            }

            //  Get the class names for the objects.
            OutputDebugString(TEXT("Class Names:\n"));
            for(i = 0; i < pon->cItems; i++)
            {
                LPWSTR pwszClass = (LPWSTR)((LPBYTE)pon + pon->aObjects[i].offsetClass);
                OutputDebugString(TEXT("\t"));
                OutputDebugStringW(pwszClass);
                OutputDebugString(TEXT("\n"));
            }

            //  Unlock the DSOBJECTNAMES structure.
            GlobalUnlock(stmObjectNames.hGlobal);
        }

        //  Release the DSOBJECTNAMES memory.
        ReleaseStgMedium(&stmObjectNames);
    }

    return hr;
}

//  The DisplayQueryScope function gets the query scope
HRESULT DisplayQueryScope(IDataObject *pdo)
{
    if(NULL == pdo)
    {
        return E_INVALIDARG;
    }

    HRESULT hr;
    FORMATETC feScope;
    STGMEDIUM stmScope;

    //  Get the CFSTR_DSQUERYSCOPE data from the data object.
    feScope.cfFormat = RegisterClipboardFormat(CFSTR_DSQUERYSCOPE);
    feScope.ptd = NULL;
    feScope.dwAspect = DVASPECT_CONTENT;
    feScope.lindex = -1;
    feScope.tymed = TYMED_HGLOBAL;
    hr = pdo->GetData(&feScope, &stmScope);
    if(SUCCEEDED(hr))
    {
        //  Lock the scope string.
        LPWSTR pwszScope = (LPWSTR)GlobalLock(stmScope.hGlobal);
        if(pwszScope)
        {
            OutputDebugString(TEXT("Query Scope: "));
            OutputDebugStringW(pwszScope);
            OutputDebugString(TEXT("\n"));

            //  Unlock the scope string
            GlobalUnlock(stmScope.hGlobal);
        }

        //  Release the scope memory
        ReleaseStgMedium(&stmScope);
    }

    return hr;
}

//  The FindADPrinters function creates a query dialog box
//  for finding printers.
HRESULT FindADPrinters(HWND hwndParent)
{
    HRESULT hr;
    ICommonQuery *pcq;

    //  Create the query dialog box object. 
    //  Call CoInitialize before making
    //  this call.
    hr = CoCreateInstance(CLSID_CommonQuery, 
                          NULL, 
                          CLSCTX_INPROC_SERVER, 
                          IID_ICommonQuery, 
                          (LPVOID*)&pcq);
    if(SUCCEEDED(hr))
    {
        OPENQUERYWINDOW openQueryWindow;
        DSQUERYINITPARAMS dqip;
        IDataObject *pdo;

        //  Initialize the OPENQUERYWINDOW structure.
        ZeroMemory(&openQueryWindow, sizeof(openQueryWindow));
        openQueryWindow.cbStruct = sizeof(openQueryWindow);
        openQueryWindow.dwFlags =   OQWF_OKCANCEL |     //  Display the OK and Cancel buttons
            OQWF_DEFAULTFORM |  //  Display the form specified in the clsidDefaultForm member
            OQWF_ISSUEONOPEN;   //  Run the query when the dialog is created
        openQueryWindow.pHandlerParameters = &dqip;
        openQueryWindow.clsidHandler = CLSID_DsQuery; //search the directory service
        openQueryWindow.clsidDefaultForm = CLSID_DsFindPrinter; //search for printers

        //  Initialize the query handler parameters.
        //  In this case, the File/Save menu item is removed.
        ZeroMemory(&dqip, sizeof(dqip));
        dqip.cbStruct = sizeof(dqip);
        dqip.dwFlags = DSQPF_NOSAVE;    //  Remove the Save Search option from the File menu.

        //  Call OpenQueryWindow. This call will block until the query dialog box 
        //  is dismissed.
        hr = pcq->OpenQueryWindow(hwndParent, &openQueryWindow, &pdo);
        if(S_OK == hr)
        {
            //  Display the DSQUERYPARAMS data.
            hr = DisplayDSQueryParams(pdo);

            //  Get the DSOBJECTNAMES data for the selected items.
            hr = DisplayDSObjectNames(pdo);

            //  Get the query scope data.
            hr = DisplayQueryScope(pdo);

            //  Release the data object.
            pdo->Release();
        }

        //  Release the query dialog box object.
        pcq->Release();
    }

    return hr;
}

//  Entry point for application
int main(int argc, _TCHAR* argv[])
{
    HRESULT hr;
    CoInitialize(NULL);
    hr = FindADPrinters(NULL);
    CoUninitialize();
    return 0;
}


Requirements

Minimum supported client

Windows Vista

Minimum supported server

Windows Server 2003

Header

Cmnquery.h

Library

Uuid.lib

DLL

Dsquery.dll

IID

IID_ICommonQuery is defined as ab50dec0-6f1d-11d0-a1c4-00aa00c16e65

See also

DSQUERYINITPARAMS
OPENQUERYWINDOW
IDataObject
CFSTR_DSOBJECTNAMES
CFSTR_DSQUERYPARAMS
CFSTR_DSQUERYSCOPE
DSQUERYPARAMS
ICommonQuery
Display Interfaces in Active Directory Domain Services

 

 

Show:
© 2014 Microsoft