Directory Services

IADsGroup::IsMember

The IADsGroup::IsMember method determines if an ADSI object is a member of the group that represents the object that implements this interface.

HRESULT IsMember( 
  BSTR bstrMember,
  VARIANT_BOOL* bMember
);

Parameters

bstrMember
The null-terminated Unicode string that specifies the ADsPath of the ADSI object.
bMember
[out] TRUE if this object is a member of this group.

Return Values

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

Remarks

Although you can add, or remove, a security principal to, or from, a group using the member's SID through the WinNT provider, the IADsGroup::IsMember method does not currently support SID for testing if a member belongs to a group through the WinNT provider.

Example Code [Visual Basic]

The following code example adds the "jeffsmith" user to the "Administrators" group on the "Microsoft" domain and then reports that the user is now a member of the group.

Dim grp As IADsGroup
On Error GoTo Cleanup

Set grp = GetObject("WinNT://Microsoft/Administrators")
grp.Add ("WinNT://Microsoft/jeffsmith")
Debug.Print grp.IsMember("WinNT://Microsoft/jeffsmith ") ' Should be TRUE.

Cleanup:
	If (Err.Number<>0) Then
		MsgBox("An error has occurred. " & Err.Number)
	End If
	Set grp = Nothing

The following code example, when supported, performs a recursive test of whether a group is allowed to have other groups as members.

Example Code [C++]

The following code example verifies that a user belongs to a group before adding it to the group.

IADsGroup *pGroup;
HRESULT hr;
LPWSTR adsPath = L"WinNT://Fabrikam/Administrators";
BSTR bstr;

hr = ADsGetObject(adsPath,IID_IADsGroup,(void**)&pGroup);

if(FAILED(hr)){goto Cleanup;}


hr = pGroup->get_Description(&bstr);
if(FAILED(hr)){goto Cleanup;}

printf("Descripton: %S\n",bstr);
SysFreeString(bstr);

VARIANT_BOOL inG=false;
hr = pGroup->IsMember(CComBSTR("WinNT//Microsoft/SecUser"), &inG);

if (inG ) {
	printf("already in the group.\n");
}
else {
	hr = pGroup->Add(CComBSTR("WinNT://Microsoft/SecUser"));
	if(FAILED(hr)){goto Cleanup;}
	printf("user added.\n");
}

Cleanup:
	if(pGroup)
		pGroup->Release();
	if(bstr)
		SysFreeString(bstr);
	return hr;

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, IADsGroup, IADsGroup Property Methods, ADSI Error Codes