Platform SDK: Active Directory, ADSI, and Directory Services |
The IADsGroup::IsMember method determines if an ADSI object is a member of the group representing the object implementing this interface.
HRESULT IADsGroup::IsMember( BSTR bstMember, VARIANT_BOOL * bMember );
This method supports the standard return values, including S_OK. For other return values, see ADSI Error Codes.
Although you can add/remove a security principal to/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.
The following Visual Basic code adds the "jsmith" 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 Set grp = GetObject("WinNT://Microsoft/Administrators") grp.Add ("WinNT://Microsoft/jsmith") Debug.Print grp.IsMember("WinNT://Microsoft/jsmith ") 'Should be TRUE
When supported, this method can be used for a recursive test of whether a group is allowed to have other groups as members.
The following C++ code snippet checks if a user belongs to a group before adding it to the group.
IADsGroup *pGroup; HRESULT hr; LPWSTR adsPath = L"WinNT://Fabrikam/Administrators"; hr = ADsGetObject(adsPath,IID_IADsGroup,(void**)&pGroup); if(FAILED(hr)) exit(hr); BSTR bstr; hr = pGroup->get_Description(&bstr); if(FAILED(hr)) exit(hr); printf("Descripton: %S\n",bstr); SysFreeString(bstr); VARIANT_BOOL inG=false; hr = pGroup->IsMember(L"WinNT//Microsoft/SecUser", &inG); if (inG ) { printf("already in the group.\n"); } else { hr = pGroup->Add(L"WinNT://Microsoft/SecUser"); printf("user added.\n"); } pGroup->Release();
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, IADsGroup, IADsGroup Property Methods, ADSI Error Codes