WhileWindows2000supportsLanManAPIsforbackwardscompatibilityitisrecommendedthatADSIbeusedinsteadformostofthefunctionsbelow.FormoreinformationaboutADSIsearchforActiveDirectoryServicesInterfaceorADSIintheMicrosoftKnowledgeBaseorMSDN This article contains code examples of using the following LanMan APIs: |
NetGroupAddNetGroupDelNetGroupAddUserNetGroupDelUserNetLocalGroupAddNetLocalGroupDelNetLocalGroupAddMembersNetLocalGroupDelMembersNetAPIBufferFreeNetAPIBufferAllocateStrToPt Note: LAN Manager APIs use various level of structures. You can use any supported level depending on information required. For example in this article I have used NetGroupAdd API with level-1 structure GROUP_INFO_1 but you can also use GROUP_INFO_2 or GROUP_INFO_3 depending on information you want to specify when you add the group.
Although, Visual Basic stores strings internally as UNICODE, it converts them to ANSI when it calls APIs. Normally this isn't a problem because most 32-bit APIs have both an ANSI and a UNICODE version. However, LanMan APIs only support UNICODE. The code snippet below demonstrates how to use Byte arrays to pass UNICODE parameters.
Instead of using strings, for example: |
use Byte arrays, such the one below, because they aren't converted to ANSI: |
Now lets check the actual code which will first add sample user TestUser1 on local machine and then it will delete that account.
Step-By-Step Example
- Create a standard exe project - Add the following code in form1 |
Click here to copy the following block | Const NERR_BASE = 2100 Const ERROR_ACCESS_DENIED = 5& Const NERR_GroupNotFound As Long = (NERR_BASE + 120) Const ERROR_NO_SUCH_MEMBER As Long = 1387& Const ERROR_MEMBER_IN_ALIAS As Long = 1378& Const ERROR_INVALID_MEMBER As Long = 1388&
Private Type GROUP_INFO_1 ptrname As Long ptrcomment As Long End Type
Private Type LOCALGROUP_MEMBERS_INFO_3 lgrmi3_domainandname As Long End Type
Private Declare Function NetAPIBufferFree Lib "NETAPI32.dll" Alias _ "NetApiBufferFree" (ByVal Ptr As Long) As Long Private Declare Function NetAPIBufferAllocate Lib "NETAPI32.dll" Alias _ "NetApiBufferAllocate" (ByVal ByteCount As Long, Ptr As Long) As Long Private Declare Function StrToPtr Lib "Kernel32" Alias "lstrcpyW" _ (ByVal Ptr As Long, Source As Byte) As Long
Private Declare Function NetGroupAdd1 Lib "NETAPI32.dll" Alias "NetGroupAdd" _ (servername As Byte, ByVal Level As Long, Buffer As GROUP_INFO_1, ParmError As Long) As Long Private Declare Function NetGroupDel Lib "NETAPI32.dll" _ (servername As Byte, groupname As Byte) As Long Private Declare Function NetGroupAddUser Lib "NETAPI32.dll" _ (servername As Byte, groupname As Byte, UserName As Byte) As Long Private Declare Function NetGroupDelUser Lib "NETAPI32.dll" _ (servername As Byte, groupname As Byte, UserName As Byte) As Long
Private Declare Function NetLocalGroupAdd1 Lib "NETAPI32.dll" Alias "NetLocalGroupAdd" _ (servername As Byte, ByVal Level As Long, Buffer As GROUP_INFO_1, ParmError As Long) As Long Private Declare Function NetLocalGroupDel Lib "NETAPI32.dll" _ (servername As Byte, groupname As Byte) As Long Private Declare Function NetLocalGroupAddMembers Lib "NETAPI32.dll" _ (servername As Byte, groupname As Byte, _ ByVal Level As Long, buf As LOCALGROUP_MEMBERS_INFO_3, ByVal totalentries As Long) As Long Private Declare Function NetLocalGroupDelMembers Lib "NETAPI32.dll" _ (servername As Byte, groupname As Byte, _ ByVal Level As Long, buf As LOCALGROUP_MEMBERS_INFO_3, ByVal totalentries As Long) As Long
Function AddUsersToGroup(ByVal servername As String, ByVal groupname As String, _ Users() As String, Optional IsLocalGroup As Boolean = True) As Long
On Error GoTo errHandler
Dim UNPtr() As Long, nUsers As Long, i As Long Dim SNArray() As Byte, GNArray() As Byte, UNArray() As Byte Dim ULocalArray() As LOCALGROUP_MEMBERS_INFO_3
SNArray = IIf(Left(servername, 2) = "\\", servername & vbNullChar, "\\" & servername & vbNullChar) GNArray = groupname & vbNullChar
nUsers = UBound(Users) + 1
If IsLocalGroup = True Then ReDim ULocalArray(nUsers - 1) ReDim UNPtr(nUsers - 1)
For i = 0 To nUsers - 1 If InStr(1, Users(i), "\") >= 1 Then UNArray = Users(i) & vbNullChar Else UNArray = "\" & Users(i) & vbNullChar End If
result = NetAPIBufferAllocate(UBound(UNArray) + 1, UNPtr(i)) result = StrToPtr(UNPtr(i), UNArray(0))
ULocalArray(i).lgrmi3_domainandname = UNPtr(i) Next AddUsersToGroup = NetLocalGroupAddMembers(SNArray(0), GNArray(0), 3, ULocalArray(0), nUsers) For i = 0 To nUsers - 1 Call NetAPIBufferFree(UNPtr(i)) Next Else For i = 0 To nUsers - 1 If InStr(1, Users(i), "\") >= 1 Then UNArray = Users(i) & vbNullChar Else UNArray = "\" & Users(i) & vbNullChar End If
AddUsersToGroup = NetGroupAddUser(SNArray(0), GNArray(0), UNArray(0)) Debug.Print AddUsersToGroup Next End If
errHandler: End Function
Function DelUsersFromGroup(ByVal servername As String, ByVal groupname As String, _ Users() As String, Optional IsLocalGroup As Boolean = True) As Long
On Error GoTo errHandler
Dim UNPtr() As Long, nUsers As Long, i As Long Dim SNArray() As Byte, GNArray() As Byte, UNArray() As Byte Dim ULocalArray() As LOCALGROUP_MEMBERS_INFO_3
SNArray = IIf(Left(servername, 2) = "\\", servername & vbNullChar, "\\" & servername & vbNullChar) GNArray = groupname & vbNullChar
nUsers = UBound(Users) + 1
If IsLocalGroup = True Then ReDim ULocalArray(nUsers - 1) ReDim UNPtr(nUsers - 1)
For i = 0 To nUsers - 1 If InStr(1, Users(i), "\") >= 1 Then UNArray = Users(i) & vbNullChar Else UNArray = "\" & Users(i) & vbNullChar End If
result = NetAPIBufferAllocate(UBound(UNArray) + 1, UNPtr(i)) result = StrToPtr(UNPtr(i), UNArray(0))
ULocalArray(i).lgrmi3_domainandname = UNPtr(i) Next DelUsersFromGroup = NetLocalGroupDelMembers(SNArray(0), GNArray(0), 3, ULocalArray(0), nUsers)
For i = 0 To nUsers - 1 Call NetAPIBufferFree(UNPtr(i)) Next Else For i = 0 To nUsers - 1 If InStr(1, Users(i), "\") >= 1 Then UNArray = Users(i) & vbNullChar Else UNArray = "\" & Users(i) & vbNullChar End If
DelUsersFromGroup = NetGroupDelUser(SNArray(0), GNArray(0), UNArray(0)) Next End If
errHandler: End Function
Function AddGroup(ByVal servername As String, ByVal groupname As String, _ ByVal GroupComment As String, Optional IsLocalGroup As Boolean = True) As Long
Dim result As Long, GNPtr As Long, GCPtr As Long, ParmError As Long Dim SNArray() As Byte, GNArray() As Byte, GCArray() As Byte Dim GroupStruct As GROUP_INFO_1 SNArray = servername & vbNullChar GNArray = groupname & vbNullChar GCArray = GroupComment & vbNullChar
result = NetAPIBufferAllocate(UBound(GNArray) + 1, GNPtr) result = NetAPIBufferAllocate(UBound(GCArray) + 1, GCPtr) result = StrToPtr(GNPtr, GNArray(0)) result = StrToPtr(GCPtr, GCArray(0))
With GroupStruct .ptrname = GNPtr .ptrcomment = GCPtr End With If IsLocalGroup = False Then result = NetGroupAdd1(SNArray(0), 1, GroupStruct, ParmError) Else result = NetLocalGroupAdd1(SNArray(0), 1, GroupStruct, ParmError) End If
AddGroup = result If result <> 0 Then Debug.Print "Error " & result & " in parameter " & ParmError _ & " when adding group " & groupname End If result = NetAPIBufferFree(GNPtr) result = NetAPIBufferFree(GCPtr) End Function
Function DelGroup(ByVal servername As String, ByVal groupname As String, _ Optional IsLocalGroup As Boolean = True) As Long
Dim GNArray() As Byte, SNArray() As Byte GNArray = groupname & vbNullChar SNArray = servername & vbNullChar If IsLocalGroup = True Then DelGroup = NetLocalGroupDel(SNArray(0), GNArray(0)) Else DelGroup = NetGroupDel(SNArray(0), GNArray(0)) End If End Function
Function GetError(errCode As Long) As String Select Case errCode Case NERR_GroupNotFound GetError = "The local group specified by the groupname parameter does not exist." Case ERROR_INVALID_MEMBER GetError = "One or more of the members cannot be added because their " & _ "account type is invalid. No new members were added." Case ERROR_MEMBER_IN_ALIAS GetError = "One or more of the members specified were already members " & _ "of the local group. No new members were added." Case ERROR_NO_SUCH_MEMBER GetError = "One or more of the members specified do not exist. " & _ "Therefore, no new members were added." Case ERROR_ACCESS_DENIED GetError = "Access is denied" Case Else GetError = "Error#" & Err.LastDllError End Select End Function
Private Sub Form_Load() Dim ret As Long
ret = AddGroup(".", "TestGroup1", "This is jsut testing group", True)
If ret = 0 Then MsgBox "Group added", vbInformation Else MsgBox GetError(ret) End If
Dim UsersToAdd(1) As String UsersToAdd(0) = "guest" UsersToAdd(1) = "administrator"
ret = AddUsersToGroup(".", "TestGroup1", UsersToAdd, True)
If ret = 0 Then MsgBox "User added to group", vbInformation Else MsgBox GetError(ret) End If
ret = MsgBox("Do you want to remove user(s) from the group which you just added?", vbQuestion + vbYesNo) If ret = vbYes Then
Dim UsersToRemove(0) As String UsersToRemove(0) = "guest"
ret = DelUsersFromGroup(".", "TestGroup1", UsersToRemove, True)
If ret = 0 Then MsgBox "User removed from group", vbInformation Else MsgBox GetError(ret) End If End If
ret = MsgBox("Do you want to delete the Group which you just added?", vbQuestion + vbYesNo) If ret = vbYes Then ret = DelGroup(".", "TestGroup1", True) If ret = 0 Then MsgBox "Group Deleted", vbInformation Else MsgBox GetError(ret) End If End If End Sub |
|