Condividi tramite


Codice di esempio per la ricerca di una foresta

Questo argomento contiene codice di esempio che esegue la ricerca in una foresta.

Nell'esempio di codice C/C++ seguente viene associato alla radice del Catalogo globale ed enumera il singolo oggetto, che è la radice della foresta, in modo che possa essere usato per eseguire ricerche nell'intera foresta.

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

L'esempio di codice C/C++ seguente contiene una funzione che restituisce un puntatore IDirectorySearch usato per eseguire ricerche nell'intera foresta.

La funzione esegue un'associazione serverless alla radice del Catalogo globale, enumera il singolo elemento, che è la radice della foresta e può essere usato per eseguire ricerche nell'intera foresta, chiama QueryInterface per ottenere un puntatore IDirectorySearch all'oggetto e restituisce tale puntatore per l'uso da parte del chiamante per la ricerca nella foresta.

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