Directory Services |
The IADsUser::Groups method obtains a collection of the ADSI group objects to which this user belongs. The method returns an IADsMembers interface pointer through which you can enumerate all the groups in the collection.
HRESULT Groups( IADsMembers** ppGroups );
The following code example examines the group membership of a user.
Dim usr As IADsUser On Error GoTo Cleanup Set usr = GetObject("WinNT://Fabrikam/JeffSmith,user") For Each grp In usr.Groups Debug.Print grp.Name & " (" & grp.Class & ")" Next Cleanup: If(Err.Number<>0) Then MsgBox("An error has occurred. " & Err.Number) End If Set usr = Nothing
The following code example examines the group memberships of a user.
HRESULT CheckUserGroups(IADsUser *pUser) { IADsMembers *pGroups; HRESULT hr = S_OK; hr = pUser->Groups(&pGroups); pUser->Release(); if (FAILED(hr)) return hr; IUnknown *pUnk; hr = pGroups->get__NewEnum(&pUnk); if (FAILED(hr)) return hr; pGroups->Release(); IEnumVARIANT *pEnum; hr = pUnk->QueryInterface(IID_IEnumVARIANT,(void**)&pEnum); if (FAILED(hr)) return hr; pUnk->Release(); // Enumerate. BSTR bstr; VARIANT var; IADs *pADs; ULONG lFetch; IDispatch *pDisp; VariantInit(&var); hr = pEnum->Next(1, &var, &lFetch); while(hr == S_OK) { if (lFetch == 1) { pDisp = V_DISPATCH(&var); pDisp->QueryInterface(IID_IADs, (void**)&pADs); pADs->get_Name(&bstr); printf("Group belonged: %S\n",bstr); SysFreeString(bstr); pADs->Release(); } VariantClear(&var); pDisp=NULL; hr = pEnum->Next(1, &var, &lFetch); }; hr = pEnum->Release(); return S_OK; }
Client: Included in Windows XP and
Windows 2000 Professional.
Server: Included in Windows Server 2003 and
Windows 2000 Server.
Redistributable: Requires Active Directory Client Extension
on Windows NT 4.0 SP6a and Windows 95/98/Me.
Header: Declared in Iads.h.
IADsMembers, IADsUser, IADsUser Property Methods, IEnumVARIANT, ADSI Error Codes