Directory Services

IADsUser::Groups

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

Parameters

ppGroups
[out] Pointer to a pointer to the IADsMembers interface on a members object that can be enumerated using IEnumVARIANT to determine the groups to which this end-user belongs.

Return Values

This method supports the standard return values, including S_OK. For other return values, see ADSI Error Codes.

Example Code [Visual Basic]

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

Example Code [C++]

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

Requirements

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.

See Also

IADsMembers, IADsUser, IADsUser Property Methods, IEnumVARIANT, ADSI Error Codes