Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: NT
Expertise: Beginner
Feb 18, 1999



Application Security Testing: An Integral Part of DevOps

Adding local groups to a NT Workstation

I am trying to write an app that can add local groups to the NT security database on an NT workstation. When I use the netlocalgroupadd() function, I get an error saying that I do not have permission.

According to the MSDN, "Only members of the Administrators or Account Operators local group can successfully execute NetLocalGroupAdd." But given than you actually have the correct rights, then the following code should work.

Option Explicit

Private Const NERR_Success             As Long = 0&
Private Const LocalGroupMembersInfo3   As Long = 3&

   DomainAndName                       As Long
End Type

Private Declare Function NetLocalGroupAddMembers _
   Lib "netapi32" _
   (ByVal psServer As Long, _
    ByVal psLocalGroupName As Long, _
    ByVal level As Long, _
    pPtrBuffer As Long, _
    ByVal membercount As Long) As Long
Public Sub Main()
   AddUserToLocal "xxx_group", _
                  "myuser", _
End Sub

Function AddUserToLocal(ByVal xi_strGroupName As String, _
                        ByVal xi_strUserName As String, _
                        ByVal xi_strServerName As String) As Boolean
   Dim p_lngPtrGroupName   As Long
   Dim p_lngPtrUserName    As Long
   Dim p_lngPtrServerName  As Long
   Dim p_lngMemberCount    As Long
   Dim p_lngRtn            As Long
   ' Convert the server name to a pointer
   If Len(Trim$(xi_strServerName)) = 0 Then
      p_lngPtrServerName = 0&
      p_lngPtrServerName = StrPtr(xi_strServerName)
   End If

   ' Convert the group name to a pointer
   p_lngPtrGroupName = StrPtr(xi_strGroupName)
   ' Convert the user name to a pointer
   p_lngPtrUserName = StrPtr(xi_strUserName)
   ' Add the user
   p_lngMemberCount = 1
   p_lngRtn = NetLocalGroupAddMembers(p_lngPtrServerName, _
                                      p_lngPtrGroupName, _
                                      LocalGroupMembersInfo3, _
                                      p_lngPtrUserName, _
   If p_lngRtn = NERR_Success Then
      AddUserToLocal = True
      AddUserToLocal = False
   End If
End Function
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date