Question:
Are simple domain management tasks such as "Add User to a Group" or "Add a Computer to an NT domain" possible under VB5? Do you have sample codes?
Answer:
Adding a user to a global group is not very difficult. The code below will allow you to do this. Note that you should replace "\\LJ_Server" with the name of your PDC, "TestGroup" with the name of an already-created global group, and "xxx" with the name of a user already entered in on the PDC.
I was not able to find a way to add a computer to the NT domain. Apparently, that function is not exposed via the 32-bit API, which kinda makes sense, security-wise, but is rather annoying nonetheless.
Private Sub Command1_Click()
AddUserToGlobalGroup "\\LJ_Server", _
"TestGroup", _
"xxx"
End Sub
Option Explicit
Declare Function NetGroupAddUser _
Lib "netapi32.dll" _
(ServerName As Byte, _
GroupName As Byte, _
UserName As Byte) As Long
Private Const NERR_Success As Long = 0&
Private Const NERR_Base As Long = 2100
Private Const NERR_InvalidComputer As Long = NERR_Base + 251
Private Const NERR_NotPrimary As Long = NERR_Base + 126
Private Const NERR_GroupExists As Long = NERR_Base + 123
Private Const NERR_UserNotFound As Long = NERR_Base + 121
Private Const NERR_GroupNotFound As Long = NERR_Base + 120
Private Const NERR_UserNotInGroup As Long = NERR_Base + 137
' ------------------------------
' Add a user to the global group
' ------------------------------
Public Sub AddUserToGlobalGroup(ByVal xi_strServerName As String, _
ByVal xi_strGroupName As String, _
ByVal xi_strUserName As String)
Dim p_abytServerName() As Byte
Dim p_abytGroupName() As Byte
Dim p_abytUserName() As Byte
Dim p_lngRtn As Long
Dim p_strErr As String
' ------------------------------
' Convert strings to byte arrays
' ------------------------------
p_abytServerName = xi_strServerName & vbNullChar
p_abytGroupName = xi_strGroupName & vbNullChar
p_abytUserName = xi_strUserName & vbNullChar
' ------------------------------
' Make the call to add user
' ------------------------------
p_lngRtn = NetGroupAddUser(p_abytServerName(0), _
p_abytGroupName(0), _
p_abytUserName(0))
If p_lngRtn <> NERR_Success Then
' Need to raise an error
p_strErr = "Failed to add the user '" & _
xi_strUserName & _
"' to the global group, '" & _
xi_strGroupName & "'"
Select Case p_lngRtn
Case NERR_InvalidComputer
p_strErr = p_strErr & " -- This server name is invalid: '" & _
xi_strServerName & "'."
Case NERR_NotPrimary
p_strErr = p_strErr & " -- This operation is only allowed on the primary domain controller of the domain."
Case NERR_GroupExists
p_strErr = p_strErr & " -- The group already exists."
Case NERR_UserNotFound
p_strErr = p_strErr & " -- The user name could not be found."
Case NERR_GroupNotFound
p_strErr = p_strErr & " -- The group name could not be found."
Case NERR_UserNotInGroup
p_strErr = p_strErr & " -- The user does not belong to this group."
Case 53
p_strErr = p_strErr & " -- The network path was not found."
Case Else
' Just keep original error string
p_strErr = p_strErr & "."
End Select
Err.Raise Number:=p_lngRtn, _
Source:="AddUserToGlobalGroup", _
Description:=p_strErr
End If
End Sub