| Platform SDK: Active Directory, ADSI, and Directory Services |
The following code fragment contains a function that adds a member to a group:
////////////////////////////////////////////////////////////////////////////////////////////////////
/* AddMemberToGroup() - Adds the passed directory object as a member of passed group
Parameters
IADsGroup * pGroup - Group to hold the new IDirectoryObject
IADs* pIADsNewMember - Object which will become a member of the group.
Object can be a user, contact, or group.
*/
HRESULT AddMemberToGroup(IADsGroup * pGroup, IADs* pIADsNewMember)
{
HRESULT hr = E_INVALIDARG;
if ((!pGroup)||(!pIADsNewMember))
return hr;
// Use the IADs::get_ADsPath() member to get the ADsPath
// Once the ADsPath string is returned, all that is needed
// to add the new member to the group, is to call the
// IADsGroup::Add() member, passing in the ADsPath string.
// Ask the new member for it's AdsPath
// This is a fully qualified LDAP path to the object to add.
BSTR bsNewMemberPath;
hr = pIADsNewMember->get_ADsPath(&bsNewMemberPath);
if (SUCCEEDED(hr))
{
// Use the IADsGroup interface to add the new member.
// Pass the LDAP path to the
// new member to the IADsGroup::Add() member
hr = pGroup->Add(bsNewMemberPath);
// Free the string returned from IADs::get_ADsPath()
SysFreeString(bsNewMemberPath);
}
return hr;
}
The following subroutine adds a member to a group:
'////////////////////////////////////////////////////////////////////////////////////////////////////
' AddUsersToGroup () - Function for adding users to a group
'
' Parameters
'
' IDirectoryObject oDirObject - Parent Directory Object for the new group
String array UsersList - List of Users to add.
Sub AddUsersToGroup(oDirObject As IDirectoryObject, sUsersList() As String, ByVal iNumUsers As Integer)
Dim x As Integer
' Add the passed Users
' go through the list and pull out the ADsPath properties contained therein
' use this path to call IADsGroup::Add(), thereby adding the object to the group
For x = 0 To iNumUsers
Dim oIADsGroup_NewGroup As IADsGroup
Set oIADsGroup_NewGroup = oDirObjectRet
oIADsGroup_NewGroup.Add sUsersList(x)
Set oIADsGroup_NewGroup = Nothing
Next x
End Sub