Platform SDK: Active Directory, ADSI, and 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 IADsUser::Groups( IADsMembers ** ppGroups );
This method supports the standard return values, including S_OK. For other return values, see ADSI Error Codes.
The following Visual Basic code snippet examines the group membership a user belongs to.
Dim usr As IADsUser Set usr = GetObject("WinNT://Microsoft/JSmith,user") For Each grp In usr.Groups Debug.Print grp.Name & " (" & grp.Class & ")" Next
The following C++ code snippet examines the group membership a user belongs to.
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(); // Now 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; } HRESULT GetUserObject(LPWSTR); // function listed elsewhere in // IADsUser Property Methods. int main(int argc, char* argv[]) { HRESULT hr = CoInitialize(NULL); IADsUser *pUser = GetUserObject( L"WinNT://Microsoft/JSmith,user"); pUser->AddRef(); hr = CheckUserGroups(pUser); if(pUser) pUser->Release(); CoUninitialize(); return 0; }
Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with DSClient).
Windows 95/98: Requires Windows 95 or later (with DSClient).
Header: Declared in Iads.h.
IADsMembers, IADsUser, IADsUser Property Methods, IEnumVARIANT, ADSI Error Codes