Funzioni helper di enumerazione

Esistono tre funzioni helper dell'enumeratore che possono essere usate da C/C++ per facilitare la navigazione degli oggetti di Active Directory. Sono ADsBuildEnumerator, ADsEnumerateNext e ADsFreeEnumerator.

ADsBuildEnumerator

La funzione helper ADsBuildEnumerator incapsula il codice necessario per creare un oggetto enumeratore. Chiama il metodo IADsContainer::get__NewEnum per creare un oggetto enumeratore e quindi chiama il metodo QueryInterface di IUnknown per ottenere un puntatore all'interfaccia IEnumVARIANT per tale oggetto. L'oggetto di enumerazione è il meccanismo di automazione da enumerare sui contenitori. Utilizzare la funzione ADsFreeEnumerator per rilasciare questo oggetto enumeratore.

ADsEnumerateNext

La funzione helper ADsEnumerateNext popola una matrice VARIANT con elementi recuperati da un oggetto enumeratore. Il numero di elementi recuperati può essere inferiore al numero richiesto.

ADsFreeEnumerator

Libera un oggetto enumeratore creato in precedenza tramite la funzione ADsBuildEnumerator.

L'esempio di codice seguente illustra una funzione che usa le funzioni helper dell'enumeratore in C++.

/*****************************************************************************
  Function:    TestEnumObject
  Arguments:   pszADsPath - ADsPath of the container to be enumerated (WCHAR*).
  Return:      S_OK if successful, an error code otherwise.
  Purpose:     Example using ADsBuildEnumerator, ADsEnumerateNext and
               ADsFreeEnumerator.
******************************************************************************/
#define MAX_ENUM      100  
 
HRESULT
TestEnumObject( LPWSTR pszADsPath )
{
    ULONG cElementFetched = 0L;
    IEnumVARIANT * pEnumVariant = NULL;
    VARIANT VariantArray[MAX_ENUM];
    HRESULT hr = S_OK;
    IADsContainer * pADsContainer =  NULL;
    DWORD dwObjects = 0, dwEnumCount = 0, i = 0;
    BOOL  fContinue = TRUE;
 
 
    hr = ADsGetObject(
                pszADsPath,
                IID_IADsContainer,
                (void **)&pADsContainer
                );
 
 
    if (FAILED(hr)) {
 
        printf("\"%S\" is not a valid container object.\n", pszADsPath) ;
        goto exitpoint ;
    }
 
    hr = ADsBuildEnumerator(
            pADsContainer,
            &pEnumVariant
            );
 
    if( FAILED( hr ) )
    {
      printf("ADsBuildEnumerator failed with %lx\n", hr ) ;
      goto exitpoint ;
    }
 
    fContinue  = TRUE;
    while (fContinue) {
 
        IADs *pObject ;
 
        hr = ADsEnumerateNext(
                    pEnumVariant,
                    MAX_ENUM,
                    VariantArray,
                    &cElementFetched
                    );

        if ( FAILED( hr ) )
        {
            printf("ADsEnumerateNext failed with %lx\n",hr);
            goto exitpoint;
        }
 
        if (hr == S_FALSE) {
            fContinue = FALSE;
        }
 
        dwEnumCount++;
 
        for (i = 0; i < cElementFetched; i++ ) {
 
            IDispatch *pDispatch    = NULL;
            BSTR        bstrADsPath = NULL;
 
            pDispatch = VariantArray[i].pdispVal;
 
            hr = V_DISPATCH( VariantArray + i )->QueryInterface(IID_IADs, (void **) &pObject) ;
 
            if( SUCCEEDED( hr ) )
            {
               pObject->get_ADsPath( &bstrADsPath );
               printf( "%S\n", bstrADsPath );
            }
            pObject->Release();
            VariantClear( VariantArray + i );
            SysFreeString( bstrADsPath );
        }
 
        dwObjects += cElementFetched;
    }
 
    printf("Total Number of Objects enumerated is %d\n", dwObjects);
 
exitpoint:
    if (pEnumVariant) {
        ADsFreeEnumerator( pEnumVariant );
    }
 
    if (pADsContainer) {
        pADsContainer->Release();
    }
 
    return(hr);
}