Directory Services

ICommonQuery::OpenQueryWindow

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.

HRESULT OpenQueryWindow( 
  HWND hwdnParent,
  LPOPENQUERYWINDOW* pQueryWnd,
  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.
Value Meaning
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 Values

Returns a standard HRESULT value including the following.
Return Code Description
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 cancelled 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.

Example Code [C++]

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

#include <cmnquery.h>
#include <dsquery.h>
#include <shlobj.h>
#include <dsclient.h>

//***************************************************************************
//
//  DisplayDSQueryParams()
//
//***************************************************************************

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;
}


//***************************************************************************
//
//  DisplayDSObjectNames()
//
//***************************************************************************

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;
}

//***************************************************************************
//
//  DisplayQueryScope()
//
//***************************************************************************

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;
}

//***************************************************************************
//
//  FindADPrinters()
//
//***************************************************************************

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 oqw;
		DSQUERYINITPARAMS dqip;
		IDataObject *pdo;

		// Initialize the OPENQUERYWINDOW structure.
		ZeroMemory(&oqw, sizeof(oqw));
		oqw.cbStruct = sizeof(oqw);
		oqw.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
		oqw.pHandlerParameters = &dqip;
		oqw.clsidHandler = CLSID_DsQuery; //search the directory service
		oqw.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, &oqw, &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;
}

//***************************************************************************
//
//  main()
//
//***************************************************************************

int main(int argc, _TCHAR* argv[])
{
	HRESULT hr;

	CoInitialize(NULL);

	hr = FindADPrinters(NULL);

	CoUninitialize();

	return 0;
}

Requirements

Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Header: Declared in Cmnquery.h.

See Also

DSQUERYINITPARAMS, OPENQUERYWINDOW, IDataObject, CFSTR_DSOBJECTNAMES, CFSTR_DSQUERYPARAMS, CFSTR_DSQUERYSCOPE, DSQUERYPARAMS, ICommonQuery, Active Directory Display Interfaces