devxlogo

Win NT Domain Management from VB5

Win NT Domain Management from VB5

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 SubOption ExplicitDeclare Function NetGroupAddUser _   Lib "netapi32.dll" _   (ServerName As Byte, _    GroupName As Byte, _    UserName As Byte) As LongPrivate Const NERR_Success          As Long = 0&Private Const NERR_Base             As Long = 2100Private Const NERR_InvalidComputer  As Long = NERR_Base + 251Private Const NERR_NotPrimary       As Long = NERR_Base + 126Private Const NERR_GroupExists      As Long = NERR_Base + 123Private Const NERR_UserNotFound     As Long = NERR_Base + 121Private Const NERR_GroupNotFound    As Long = NERR_Base + 120Private 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
See also  Comparing different methods of testing your Infrastructure-as-Code
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist