Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Displaying Members of a Group

[C++]

The following code fragment contains a function that displays the members of a group using IADsGroup and IADsMembers:

//////////////////////////////
/*  PrintGroupObjectMembers()    
- Prints the members of the group that the pADsGroup passes.
    Parameters
        IADsGroup * pADsGroup       - Group from which to list members
*/
HRESULT PrintGroupObjectMembers(IADsGroup * pADsGroup)
{
    HRESULT         hr                = S_OK;     // COM Result Code
    IADsMembers *   pADsMembers       = NULL;     // Ptr to Members of the IADsGroup
    BOOL            fContinue         = TRUE;     // Looping Variable
    IEnumVARIANT *  pEnumVariant      = NULL;     // Ptr to the Enum variant
    IUnknown *      pUnknown          = NULL;     // IUnknown for getting the ENUM initially
    VARIANT         VariantArray[FETCH_NUM];      // Variant array for temp holding returned data
    ULONG           ulElementsFetched = NULL;     // Number of elements fetched
 
    // Get an interface pointer to the IADsCollection of members
    hr = pADsGroup->Members(&pADsMembers);
 
    if (SUCCEEDED(hr))
    {
 
        // Ask the IADsCollection of members for a new ENUM Interface
        // Note the enum comes back as an IUnknown *
        hr = pADsMembers->get__NewEnum(&pUnknown);
 
        if (SUCCEEDED(hr))
        {
 
            // QI the IUnknown * for a IEnumVARIANT interface
            hr = pUnknown->QueryInterface(IID_IEnumVARIANT, (void **)&pEnumVariant);
 
            if (SUCCEEDED(hr))
            {
 
                // While have not hit errors or end of data....
                while (fContinue) 
                {
                   ulElementsFetched = 0;
 
                    // Get a "batch" number of group members- number of rows that FETCH_NUM specifies
                    hr = ADsEnumerateNext(pEnumVariant, FETCH_NUM, VariantArray, &ulElementsFetched);
 
                    if (ulElementsFetched )//SUCCEEDED(hr) && hr != S_FALSE)
                    {
 
                        // Loop through the current batch, printing 
                        // the path for each member
                        for (ULONG i = 0; i < ulElementsFetched; i++ ) 
                        {
                            IDispatch * pDispatch         = NULL; 
                            // ptr for holding dispath of element
                            IADs      * pIADsGroupMember  = NULL; 
                            // IADs ptr to group member
                            BSTR        bstrPath          = NULL; 
                            // Holds path of object
 
                            // Get the dispatch ptr for the variant
                            pDispatch = VariantArray[i].pdispVal;
                            assert(HAS_BIT_STYLE(VariantArray[i].vt,VT_DISPATCH));
 
                            // Get the IADs interface for the "member" of this group
                            hr = pDispatch->QueryInterface(IID_IADs,
                                                           (VOID **) &pIADsGroupMember) ;
 
                            if (SUCCEEDED(hr))
                            {
 
                            // Get the ADsPath property for 
                            //this member
                                hr = pIADsGroupMember->get_ADsPath(&bstrPath) ;
 
                                if (SUCCEEDED(hr))
                                {
 
                                    // Print the ADsPath of the 
                                    //group member
                                    wprintf(L"\tMember Object: %ws\n", bstrPath);
                                    SysFreeString(bstrPath);
                                }
                                pIADsGroupMember->Release();
                                pIADsGroupMember   = NULL;
                            }
                         }
 
                        // Clear the variant array
                        memset(VariantArray, 0, sizeof(VARIANT)*FETCH_NUM);
                    }
                    else
                        fContinue = FALSE;
                }
                pEnumVariant->Release();
                pEnumVariant = NULL;
            }
            pUnknown->Release();
            pUnknown = NULL;
        }
        pADsMembers ->Release();
        pADsMembers  = NULL;
    }
 
    // If everything WENT ok, all the data
    // was printed,  and an S_FALSE (indicating 
    // no more data) was received. If that is the case,
    // you REALLY want to return an S_OK
    if (hr == S_FALSE)
        hr = S_OK;
 
    return hr;
}
[Visual Basic]

The following code fragment contains a function that displays the members of a group:

'////////////////////////////////////////////////////////////////////////////////////////////////////
'    PrintGroupObjectMembers()    - Prints the members of passed IADsGroup ptr
'
'    Parameters
'
'        oADsGroup As IADsGroup       - Group from which to list members
'
'HRESULT PrintGroupObjectMembers(IADsGroup * pADsGroup)
Sub PrintGroupObjectMembers(oADsGroup As IADsGroup)
 
    Dim child As IADs
    For Each child In oADsGroup.Members
        PrintIADSObject child
    Next child
 
End Sub