Directory Services

Example Code for Using a VLV Search

The following code example shows how to perform a search using VLV.

#define SECURITY_WIN32

#include <windows.h>
#include <stdio.h>

#include "winldap.h"
#include <rpc.h>
#include <rpcdce.h>

#include <security.h>

#include <objbase.h>
#include "iads.h"
#include <activeds.h>

//#include <lmaccess.h>

void VLVsearch(IDirectorySearch *pSearchRoot, 
			 ADS_VLV *vlvPref, 
			 WCHAR *attrVal, 
			 int offset, 
			 int beforecount, 
			 int aftercount);

void __cdecl main (int argc, char **argv) 
{
   CHAR szCmd[1024];
   int offset = 1;
   char *attrVal = NULL;
   IDirectorySearch *pSearchRoot = NULL;
   ADS_VLV vlvPref;
   HRESULT hr = S_OK;

   CoInitialize(0);

   hr = ADsOpenObject(L"LDAP://Fabrikam/OU=vlvOU,DC=AB1,DC=mytest,DC=fabrikam,DC=com",
					NULL,
					NULL,
					ADS_SECURE_AUTHENTICATION,
					IID_IDirectorySearch,
					(void **) &pSearchRoot);

   if (FAILED(hr))
   {
	PrintError(hr);
	goto cleanup;
   }

   vlvPref.dwContentCount = 0;
   vlvPref.dwContextIDLength = 0;
   vlvPref.lpContextID = NULL;

   VLVsearch(pSearchRoot, &vlvPref, NULL, 2, 1, 1);
   printf("\n");
   VLVsearch(pSearchRoot, &vlvPref, NULL, 10, 3, 3);
   printf("\n");
   VLVsearch(pSearchRoot, &vlvPref, NULL, 0, 3, 10);


cleanup:
   if(pSearchRoot)
   {
	 pSearchRoot->Release();
   }
   CoUninitialize();

}

void VLVsearch(IDirectorySearch *pSearchRoot, 
			 ADS_VLV *vlvPref, 
			 WCHAR *attrVal, 
			 int offset, 
			 int beforeCount, 
			 int afterCount)
{

   HRESULT hr;
   ADS_SEARCHPREF_INFO prefInfo[3];
   ADS_SEARCH_HANDLE hSearchHandle;
   ADS_SEARCH_COLUMN pColumn;
   ADS_SORTKEY sortKey;
   int iRowCnt=0;

   vlvPref->dwBeforeCount = beforeCount;
   vlvPref->dwAfterCount = afterCount;
   vlvPref->dwOffset = offset;
   vlvPref->pszTarget = attrVal;

   prefInfo[0].dwSearchPref = ADS_SEARCHPREF_VLV;
   prefInfo[0].vValue.dwType = ADSTYPE_PROV_SPECIFIC;
   prefInfo[0].vValue.ProviderSpecific.dwLength = sizeof(ADS_VLV);
   prefInfo[0].vValue.ProviderSpecific.lpValue = (LPBYTE) vlvPref;

   
   sortKey.pszAttrType = L"cn";
   sortKey.pszReserved = NULL;
   sortKey.fReverseorder = 0;

   prefInfo[1].dwSearchPref	= ADS_SEARCHPREF_SORT_ON;
   prefInfo[1].vValue.dwType = ADSTYPE_PROV_SPECIFIC;
   prefInfo[1].vValue.ProviderSpecific.dwLength = sizeof(ADS_SORTKEY);
   prefInfo[1].vValue.ProviderSpecific.lpValue = (LPBYTE) &sortKey;

   hr = pSearchRoot->SetSearchPreference( prefInfo, 2);

   hr = pSearchRoot->ExecuteSearch(L"(objectClass=*)", NULL, -1, &hSearchHandle);
   if (FAILED(hr))
   {
	PrintError(hr);
	return;
   }
   while (TRUE)
   {
	hr = pSearchRoot->GetNextRow(hSearchHandle);
	if( hr == S_ADS_NOMORE_ROWS)
	{
		// End of the rowset
		hr = S_OK;
		break;
}
	else if (FAILED(hr))
	{
		 PrintError(hr);
		 break;
}
	iRowCnt++;

	// Get the Name and display it in the list.
	hr = pSearchRoot->GetColumn( hSearchHandle, L"ADsPath", &pColumn );
	if ( SUCCEEDED(hr) )
	{
		 printf("%S\n", pColumn.pADsValues->CaseIgnoreString);
		 pSearchRoot->FreeColumn( &pColumn );
}
   }
   if (SUCCEEDED(hr))
   {
	ADSVALUE vlvValue;
	ADS_VLV *pVlv;
	hr = pSearchRoot->GetColumn(hSearchHandle, ADS_VLV_RESPONSE, &pColumn);

	if (ADSTYPE_PROV_SPECIFIC != pColumn.dwADsType)
	{
		 printf("Error: ADs type is not provider specific for vlv response\n");
}
	if (1 != pColumn.dwNumValues)
	{
		 printf("Error: More than one ADs value for vlv response\n");
}
	vlvValue = pColumn.pADsValues[0];
	pVlv = (PADS_VLV) vlvValue.ProviderSpecific.lpValue;

	printf("context ID length = %d \n",pVlv->dwContextIDLength);
	printf("content count = %d \n",pVlv->dwContentCount);

	vlvPref->dwContentCount = pVlv->dwContentCount;
	vlvPref->dwContextIDLength = pVlv->dwContextIDLength;
	vlvPref->lpContextID = pVlv->lpContextID;

	pSearchRoot->FreeColumn(&pColumn);
   }

   if(pVlv)
   {
	pVlv = NULL;
   }
   hr = pSearchRoot->CloseSearchHandle(hSearchHandle);  

}