将域用户或组添加到本地组的示例代码
以下C++代码示例将域用户或组添加到成员服务器上的本地组或 Windows NT 工作站或 Windows 2000 Professional 上运行的计算机。
#include <stdio.h>
#include <atlbase.h>
#include <activeds.h>
#include <ntldap.h>
/*******************************************************************
AddDomainUserToLocalGroup()
Adds a user from a domain to a local group using the WinNT
provider.
Parameters:
pwszComputerName - Contains the name of the computer
that the group belongs to.
pwszGroupName - Contains the name of the group to add a member
to. This group must exist on the target computer.
pwszDomainName - Contains the name of domain that contains
the user to add to the group.
pwszUserToAdd - Contains the name of the user in the domain
to add to the group.
*******************************************************************/
HRESULT AddDomainUserToLocalGroup(LPCWSTR pwszComputerName,
LPCWSTR pwszGroupName,
LPCWSTR pwszDomainName,
LPCWSTR pwszUserToAdd)
{
HRESULT hr = E_FAIL;
CComPtr<IADsContainer> spComputer;
// Build the binding string.
CComBSTR sbstrBindingString;
sbstrBindingString = "WinNT://";
sbstrBindingString += pwszComputerName;
sbstrBindingString += ",computer";
// Bind to the computer that contains the group.
hr = ADsOpenObject( sbstrBindingString,
NULL,
NULL,
ADS_SECURE_AUTHENTICATION,
IID_IADsContainer,
(void**)&spComputer);
if(FAILED(hr))
{
return hr;
}
// Bind to the group object.
CComPtr<IDispatch> spDisp;
hr = spComputer->GetObject(CComBSTR("group"),
CComBSTR(pwszGroupName),
&spDisp);
if(FAILED(hr))
{
return hr;
}
CComPtr<IADsGroup> spGroup;
hr = spDisp->QueryInterface(IID_IADs, (LPVOID*)&spGroup);
if(FAILED(hr))
{
return hr;
}
// Bind to the member to add.
sbstrBindingString = "WinNT://";
sbstrBindingString += pwszDomainName;
sbstrBindingString += "/";
sbstrBindingString += pwszUserToAdd;
hr = spGroup->Add(sbstrBindingString);
return hr;
}
以下 Visual Basic 代码示例将域用户或组添加到成员服务器上的本地组或 Windows NT 工作站或 Windows 2000 Professional 上运行的计算机。
''''''''''''''''''''''''''''''''''''''''
'
' AddDomainUserToLocalGroup
'
' Adds a user from a domain to a local group using the WinNT
' provider.
'
' Parameters:
'
' ComputerName - Contains the name of the computer that the group
' belongs to.
'
' GroupName - Contains the name of the group to add a member to.
' This group must exist on the target computer.
'
' DomainName - Contains the name of domain that contains the user
' to add to the group.
'
' UserName - Contains the name of the user in the domain to add
' to the group.
'
''''''''''''''''''''''''''''''''''''''''
Public Sub AddDomainUserToLocalGroup(ComputerName As String, _
GroupName As String, _
DomainName As String, _
UserName As String)
Dim GroupComputer As IADsContainer
Dim Group As IADsGroup
' Bind to the computer that contains the group.
Set GroupComputer = GetObject("WinNT://" &
ComputerName &
",computer")
' Bind to the group object.
Set Group = GroupComputer.GetObject("group", GroupName)
' Add the user to the group.
Group.Add ("WinNT://" & DomainName & "/" & UserName)
End Sub