Directory Services

Example Code for Searching a Forest

The following code example binds to the root of the Global Catalog (GC:) and enumerates the single object, which is the root of the forest, so that it can be used to search the entire forest.

Set gc = GetObject("GC:")
For each child in gc
	Set entpr = child
Next
' Now entpr is an object that can used
' to search the entire forest.

The following code example contains a function that returns an IDirectorySearch pointer used to search the entire forest.

The function performs a serverless bind to the root of GC:, enumerates the single item, which is the root of the forest and can be used to search the entire forest, calls QueryInterface to get an IDirectorySearch pointer to the object, and returns that pointer for use by the caller to search the forest.

HRESULT GetGC(IDirectorySearch **ppDS)
{
HRESULT hr;
IEnumVARIANT *pEnum = NULL;
IADsContainer *pCont = NULL;
VARIANT var;
IDispatch *pDisp = NULL;
ULONG lFetch;
 
// Set IDirectorySearch pointer to NULL.
*ppDS = NULL;
 
// First, bind to the GC: namespace container object. 
hr = ADsOpenObject(TEXT("GC:"),
				NULL,
				NULL,
				ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
				IID_IADsContainer,
				(void**)&pCont);
if (FAILED(hr)) {
	_tprintf(TEXT("ADsOpenObject failed: 0x%x\n"), hr);
	goto cleanup;
} 
 
// Get an enumeration interface for the GC container to enumerate the 
// contents. The actual GC is the only child of the GC container.
hr = ADsBuildEnumerator(pCont, &pEnum);
if (FAILED(hr)) {
	_tprintf(TEXT("ADsBuildEnumerator failed: 0x%x\n"), hr);
	goto cleanup;
} 
 
// Now enumerate. There is only one child of the GC: object.
hr = pEnum->Next(1, &var, &lFetch);
if (FAILED(hr)) {
	_tprintf(TEXT("ADsEnumerateNext failed: 0x%x\n"), hr);
	goto cleanup;
} 
 
// Get the IDirectorySearch pointer.
if (( hr == S_OK ) && ( lFetch == 1 ) )	 
{ 
	pDisp = V_DISPATCH(&var);
	hr = pDisp->QueryInterface( IID_IDirectorySearch, (void**)ppDS); 
}
 
cleanup:
 
if (pEnum)
	ADsFreeEnumerator(pEnum);
if (pCont)
	pCont->Release();
if (pDisp)
	(pDisp)->Release();
return hr;
}