The following example adds users to a group.
Example 1: Adding Users to a Group (Visual Basic)
Dim Group as IADsGroup
' Bind to a known group object.
Set Group = GetObject("WinNT://MSFT/Groups/Administrators")
' Add Jane and John to the Administrators group.
Group.Add("WinNT://MSFT/Users/John")
Group.Add("WinNT://MSFT/Users/Jane")
Example 1: Adding Users to a Group (C/C++)
IADsGroup *pGroup;
//
// Bind to a known group object.
//
ADsGetObject(TEXT("WinNT://MSFT/Groups/Administrators"),
IID_IADsGroup,
(void**)&pGroup);
//
// Add Jane and John to the Administrators group.
//
pGroup->Add(TEXT("WinNT://MSFT/Users/John"));
pGroup->Add(TEXT("WinNT://MSFT/Users/Jane"));
//
// Cleanup.
//
pGroup->Release();
The following example removes users from a group.
Example 2: Removing Users from a Group (Visual Basic)
Dim Group as IADsGroup
' Bind to a known group object.
Set Group = GetObject("@WinNT!MSFT\Groups\Administrators")
' Remove Jane and John from the Administrators group.
Group.Remove("WinNT://MSFT/Users/John")
Group.Remove("WinNT://MSFT/Users/Jane")
Example 2: Removing Users from a Group (C/C++)
IADsGroup *pGroup;
//
// Bind to a known group object.
//
ADsGetObject(TEXT("WinNT://MSFT/Groups/Administrators"),
IID_IADsGroup,
(void**)&pGroup);
//
// Remove Jane and John from the Administrators group.
//
pGroup->Remove(TEXT("WinNT://MSFT/Users/John"));
pGroup->Remove(TEXT("WinNT://MSFT/Users/Jane"));
//
// Cleanup.
//
pGroup->Release();
The following example enumerates the users in a group.
Example 3: Enumerating Users in a Group (Visual Basic)
Dim Group as IADsGroup
Dim Member as IADs
' Bind to a known group object.
Set Group = GetObject("WinNT://MyDomain/Administrators")
' Enumerate the members in the group.
For Each Member in Group.Members
Debug.Print Member.Name
Next Member
Example 3: Enumerating Users in a Group (C/C++)
IADsGroup *pGroup;
IADsMembers *pMembers;
IEnumVARIANT *pEnum;
IADs *pMember;
BSTR bstrMemberName;
//
// Bind to a known group object.
//
ADsGetObject(TEXT("WinNT://MSFT/Groups/Administrators"),
IID_IADsGroup,
(void**)&pGroup);
//
// Get the Members collection.
//
pGroup->Members(&pMembers);
//
// Get an enumerator. ADsBuildEnumerator is a helper
// function supplied by Active Directory. See Appendix B for more
// details.
//
ADsBuildEnumerator((IADsContainer *)pMembers,
&pEnum);
//
// Enumerate through all the children of the container.
// ADsEnumerateNext is a helper function supplied by Active Directory.
// See Appendix B for more details.
//
do
{
hr = ADsEnumerateNext(pEnum,
1,
(void**)&pMember,
NULL);
if (SUCCEEDED(hr))
{
pMember->get_Name(&bstrMemberName);
printf("%s\n", bstrMemberName);
SysFreeString(bstrMemberName);
pMember->Release();
} // if
} while (SUCCEEDED(hr)); // do
//
// Cleanup.
//
pEnum->Release();
pMembers->Release();
pGroup->Release();