How do I use the NetLocalGroupAddMembers function in VB?

dumbo2569
dumbo2569 used Ask the Experts™
on
Can anyone give me a quick code snippet of a how to use the NetLocalGroupAddMembers function in VB. I know how to use it in C++, but I'm fairly new to VB and I'm not quite sure how to handle pointers in VB. Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
MSDN article on using the LanMan functions from VB

http://support.microsoft.com/default.aspx?scid=KB;en-us;q159498
Module Code


Option Explicit

Declare Function NetLocalGroupAddMembers Lib "netapi32.dll" _
   (ByVal servername As String, _
    ByVal groupname As String, _
    ByVal level As Long, _
    buf As Any, _
    ByVal totalentries As Long) As Long


Declare Function NetApiBufferFree Lib "netapi32.dll" _
   (ByVal Buffer As Long) As Long

Declare Function NetUserEnum Lib "netapi32.dll" _
   (ByVal servername As String, _
    ByVal level As Long, _
    ByVal filter As Long, _
    bufptr As Any, _
    ByVal prefmaxlen As Long, _
    entriesread As Long, _
    totalentries As Long, _
    resume_handle As Long) As Long


Declare Function NetLocalGroupEnum Lib "netapi32.dll" _
   (ByVal servername As String, _
    ByVal level As Long, _
    bufptr As Any, _
    ByVal prefmaxlen As Long, _
    entriesread As Long, _
    totalentries As Long, _
    resumehandle As Long) As Long


Declare Function NetUserGetLocalGroups Lib "netapi32.dll" _
   (ByVal servername As String, _
    ByVal username As String, _
    ByVal level As Long, _
    ByVal flag As Long, _
    bufptr As Any, _
    ByVal prefmaxlen As Long, _
    entriesread As Long, _
    totalentries As Long) As Long


Declare Sub RtlMoveMemory Lib "Kernel32.dll" _
   (Destination As Any, _
    Source As Any, _
    ByVal Length As Long)


Declare Function lstrcpy Lib "Kernel32.dll" _
    Alias "lstrcpyW" _
   (lpszString1 As Any, _
    lpszString2 As Any) As Long


Declare Function lstrlen Lib "Kernel32.dll" _
    Alias "lstrlenW" _
   (ByVal lpszString As Long) As Long

Type USER_INFO_0
    usri0_name As Long
End Type


Type LOCALGROUP_INFO_0
    lgrpi0_name As Long
End Type


Type LOCALGROUP_USER_INFO_0
    lgrui0_name As Long
End Type


Type LOCALGROUP_MEMBERS_INFO_3
    lgrmi3_domainandname As Long
End Type

Public Const LG_INCLUDE_INDIRECT = &H1
Public Const MAX_PREFERRED_LENGTH = -1&
Public Const ERROR_MORE_DATA = 234&
Public Const NERR_Success = 0

Function PointerToString(lngPointer As Long) As String
    Dim bytBuffer(255) As Byte
   
    lstrcpy bytBuffer(0), ByVal lngPointer
   
    PointerToString = Left(bytBuffer, lstrlen(lngPointer))
End Function




Form Code


Option Explicit

Private Sub Combo1_Click()
   
    EnumLocalGroups List2
   
    EnumUserLocalGroups List1, List2
End Sub

Private Sub Command1_Click()
    Dim lngWin32apiResultCode As Long
    Dim strServerName         As String
    Dim strLocalGroupName     As String
    Dim lngBufPtr             As Long
    Dim udtLGMemInfo          As LOCALGROUP_MEMBERS_INFO_3
    Dim strName               As String
   
    strServerName = _
        StrConv("", _
                vbUnicode)
   
    If List2.ListIndex >= 0 Then
       
        strLocalGroupName = _
            StrConv(List2.Text, _
                    vbUnicode)
       
        If Combo1.ListIndex >= 0 Then
           
            strName = Combo1.Text
            udtLGMemInfo.lgrmi3_domainandname = StrPtr(strName)
            '
            lngWin32apiResultCode = _
                NetLocalGroupAddMembers(strServerName, _
                                        strLocalGroupName, _
                                        3, _
                                        udtLGMemInfo, _
                                        1)
           
            NetApiBufferFree lngBufPtr
        End If
    End If
   
    EnumLocalGroups List2
    '
    EnumUserLocalGroups List1, List2
End Sub

Private Sub Form_Load()
   
    With Label1
        .Alignment = vbRightJustify
        .Caption = "[U[<F"
    End With
    Label2.Caption = "
.7iO [v"
    Label3.Caption = "
.5H"O [v"
   
    Command1.Caption = ")"
   
    EnumUsers Combo1
    Combo1.ListIndex = 0
End Sub

Private Sub EnumUsers(cboUsers As ComboBox)
    Dim lngWin32apiResultCode As Long
    Dim strServerName         As String
    Dim lngBufPtr             As Long
    Dim lngMaxLen             As Long
    Dim lngEntriesRead        As Long
    Dim lngTotalEntries       As Long
    Dim lngResumeHandle       As Long
    Dim udtUserInfo0          As USER_INFO_0
    Dim lngEntry              As Long
   
   
    strServerName = _
        StrConv("", _
                vbUnicode)
    Do
       
        lngWin32apiResultCode = _
            NetUserEnum(strServerName, _
                        0, _
                        0, _
                        lngBufPtr, _
                        lngMaxLen, _
                        lngEntriesRead, _
                        lngTotalEntries, _
                        lngResumeHandle)
       
        If (lngWin32apiResultCode = NERR_Success) Or _
           (lngWin32apiResultCode = ERROR_MORE_DATA) Then
            For lngEntry = 0 To lngEntriesRead - 1
               
                RtlMoveMemory udtUserInfo0, _
                              ByVal lngBufPtr + _
                                    Len(udtUserInfo0) * _
                                    lngEntry, _
                              Len(udtUserInfo0)
               
                cboUsers.AddItem _
                    PointerToString(udtUserInfo0.usri0_name)
            Next
        End If
       
        If lngBufPtr <> 0 Then
            NetApiBufferFree lngBufPtr
        End If
    Loop Until lngEntriesRead = lngTotalEntries
End Sub

Private Sub EnumUserLocalGroups(lstUserLocalGroups As ListBox, _
                                lstLocalGroups As ListBox)
    Dim lngWin32apiResultCode As Long
    Dim strServerName   As String
    Dim strUserName     As String
    Dim lngBufPtr       As Long
    Dim lngEntriesRead  As Long
    Dim lngTotalEntries As Long
    Dim lngResumeHandle As Long
    Dim udtLGInfo0      As LOCALGROUP_USER_INFO_0
    Dim lngEntry        As Long
    Dim strLocalGroup   As String
    Dim lngListCounter  As Long
   
   
    lstUserLocalGroups.Clear
   
    strServerName = _
        StrConv("", _
                vbUnicode)
   
    strUserName = _
        StrConv(Combo1.Text, _
                vbUnicode)
    Do
       
        lngWin32apiResultCode = _
            NetUserGetLocalGroups(strServerName, _
                                  strUserName, _
                                  0, _
                                  LG_INCLUDE_INDIRECT, _
                                  lngBufPtr, _
                                  MAX_PREFERRED_LENGTH, _
                                  lngEntriesRead, _
                                  lngTotalEntries)
       
        If (lngWin32apiResultCode = NERR_Success) Or _
           (lngWin32apiResultCode = ERROR_MORE_DATA) Then
            For lngEntry = 0 To lngEntriesRead - 1
               
                RtlMoveMemory udtLGInfo0, _
                              ByVal lngBufPtr + _
                                    Len(udtLGInfo0) * _
                                    lngEntry, _
                              Len(udtLGInfo0)
                strLocalGroup = _
                    PointerToString(udtLGInfo0.lgrui0_name)
               
                lstUserLocalGroups.AddItem strLocalGroup
                With lstLocalGroups
                    For lngListCounter = 0 To .ListCount - 1
                        If strLocalGroup = .List(lngListCounter) Then
                            .RemoveItem (lngListCounter)
                        End If
                    Next
                End With
            Next
        End If
       
        If lngBufPtr <> 0 Then
            NetApiBufferFree lngBufPtr
        End If
    Loop Until lngEntriesRead = lngTotalEntries
End Sub

Private Sub EnumLocalGroups(lstLocalGroups As ListBox)
    Dim lngWin32apiResultCode As Long
    Dim strServerName         As String
    Dim lngBufPtr             As Long
    Dim lngEntriesRead        As Long
    Dim lngTotalEntries       As Long
    Dim lngResumeHandle       As Long
    Dim udtLGInfo0            As LOCALGROUP_INFO_0
    Dim lngEntry              As Long
   
   
    lstLocalGroups.Clear
   
    strServerName = _
        StrConv("", _
                vbUnicode)
    Do
       
        lngWin32apiResultCode = _
            NetLocalGroupEnum(strServerName, _
                              0, _
                              lngBufPtr, _
                              MAX_PREFERRED_LENGTH, _
                              lngEntriesRead, _
                              lngTotalEntries, _
                              lngResumeHandle)
       
        If (lngWin32apiResultCode = NERR_Success) Or _
           (lngWin32apiResultCode = ERROR_MORE_DATA) Then
            For lngEntry = 0 To lngEntriesRead - 1
               
                RtlMoveMemory udtLGInfo0, _
                              ByVal lngBufPtr + _
                                    Len(udtLGInfo0) * _
                                    lngEntry, _
                              Len(udtLGInfo0)
               
                lstLocalGroups.AddItem _
                    PointerToString(udtLGInfo0.lgrpi0_name)
            Next
        End If
       
        If lngBufPtr <> 0 Then
            NetApiBufferFree lngBufPtr
        End If
    Loop While lngWin32apiResultCode = ERROR_MORE_DATA
End Sub

Author

Commented:
you are da MAN~!. Thanks.. much appreciate it..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial