首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

NetLocalGroupAddMembers API 调用出玩124异常,是什么原因

2012-03-21 
NetLocalGroupAddMembers API 调用出玩124错误,是什么原因?我在VB中调用了NetLocalGroupAddMembers这个函

NetLocalGroupAddMembers API 调用出玩124错误,是什么原因?
我在VB中调用了NetLocalGroupAddMembers这个函数,但在运行时出玩了124错误,请问下是什么原因造成的?应该如何解决?
我的原码下如:
Public   Type   LOCALGROUP_MEMBERS_INFO_3
        Name   As   String
End   Type
Public   Declare   Function   NetLocalGroupAddMembers   Lib   "netapi32.dll "   (serverName   As   Byte,   groupName   As   Byte,   _
        level   As   Long,   buf   As   Any,   TotalEntries   As   Long)   As   Long


Public   Function   AddUserToLocalGroup(ByVal   serverName   As   String,   ByVal   groupName   As   String,   ByVal   userName   As   String,   ByRef   returnString   As   String)   As   Boolean
On   Error   GoTo   AddUserToLocalGroupErr
        Dim   serverArray()   As   Byte,   groupArray()   As   Byte,   Result       As   Long
        Dim   userInfo   As   LOCALGROUP_MEMBERS_INFO_3
       
        AddUserToLocalGroup   =   False
       
        serverArray   =   serverName   &   vbNullChar
        groupArray   =   groupName   &   vbNullChar
        userInfo.Name   =   StrConv(userName,   vbUnicode)
       
        Result   =   0
        Result   =   NetLocalGroupAddMembers(serverArray(0),   groupArray(0),   3,   userInfo,   1)

        If   Result   <>   0   Then   GoTo   AddUserToLocalGroupErr
       
        returnString   =   Format(Now,   "yyyy-mm-dd   hh:mm:ss ")   +   ":     成功添加用户 "   +   userName   +   "到组 "   +   groupName   +   "中!   "   +   Chr(13)   +   Chr(10)
        AddUserToLocalGroup   =   True
        Exit   Function
AddUserToLocalGroupErr:
        returnString   =   Format(Now,   "yyyy-mm-dd   hh:mm:ss ")   +   ":     添加用户 "   +   userName   +   "到组 "   +   groupName   +   "时出错!   "   +   Err.Description   +   Chr(13)   +   Chr(10)
End   Function

[解决办法]
整理了下,下面的代码可以直接用了

Option Explicit
Private Const NERR_Success = 0
Private Const ERROR_MORE_DATA = 234&
Private Const MAX_PREFERRED_LENGTH = -1&
Private Const LG_INCLUDE_INDIRECT = &H1
Private Const User_Priv_User = &H1
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const NERR_BASE = 2100
Private Const MAX_NERR = NERR_BASE + 899
Private Const LOAD_LIBRARY_AS_DATAFILE = &H2
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800

Private Type TUser1 ' Level 1
ptrName As Long
ptrPassword As Long
dwPasswordAge As Long
dwPriv As Long
ptrHomeDir As Long
ptrComment As Long


dwFlags As Long
ptrScriptPath As Long
End Type

Private Type USER_INFO_0
usri0_name As Long
End Type

Private Type LOCALGROUP_INFO_0
lgrpi0_name As Long
End Type

Private Type LOCALGROUP_USER_INFO_0
lgrui0_name As Long
End Type

Private Type UserInfo_1
Username As String
Password As String
PasswordAge As Long
Privilege As Long
HomeDir As String
Comment As Long
Flags As Long
ScriptPath As String
End Type

Private Type LOCALGROUP_MEMBERS_INFO_3
lgrmi3_domainandname As Long
End Type

Private Type USER_INFO_1003
usri1003_password As Long
End Type

Private Usr1 As UserInfo_1

'用户所在组
Private 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
'本地组
Private 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
Private Declare Function lstrlen Lib "Kernel32.dll " Alias "lstrlenW " (ByVal lpszString As Long) As Long
Private Declare Function lstrcpy Lib "Kernel32.dll " Alias "lstrcpyW " (lpszString1 As Any, lpszString2 As Any) As Long
Private Declare Function NetApiBufferFree Lib "netapi32.dll " (ByVal Buffer As Long) As Long
Declare Sub RtlMoveMemory Lib "Kernel32.dll " (Destination As Any, Source As Any, ByVal Length As Long)
'添加用户
Private Declare Function NetUserAdd Lib "Netapi32 " (ByVal ServerName As String, ByVal Level As Long, Buffer As Any, ParamErr As Long) As Long
'用户列表
Private 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
'添加到本地组
Private 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
'删除用户
Private Declare Function NetUserDel Lib "netapi32.dll " (ServerName 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 NetUserChangePassword Lib "netapi32.dll " (ByVal domainname As String, ByVal Username As String, ByVal OldPassword As String, ByVal NewPassword As String) As Long
Private Declare Function NetGetDCName Lib "netapi32.dll " (ServerName As Long, domainname As Byte, bufptr As Long) As Long
Private Declare Function LoadLibraryEx Lib "kernel32 " Alias "LoadLibraryExA " (ByVal lpLibFileName As String, ByVal hFile As Long, ByVal dwFlags As Long) As Long
Private Declare Function NetUserSetInfo Lib "netapi32.dll " (ByVal ServerName As String, ByVal Username As String, ByVal Level As Long, UserInfo As Any, ParmError As Long) As Long
Private Declare Sub lstrcpyW Lib "kernel32 " (dest As Any, ByVal src As Any)
Private Declare Function FormatMessage Lib "kernel32 " Alias "FormatMessageA " (ByVal dwFlags As Long, ByVal lpSource As Long, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Any) As Long


Private Declare Function FreeLibrary Lib "kernel32 " (ByVal hLibModule As Long) As Long

'函数部分

'修改密码

Public Function ChangePassword(ByVal ServerName As String, ByVal Username As String, ByVal OldPassword As String, ByVal NewPassword As String)
Dim strServer As String, strUserName As String
Dim strNewPassword As String, strOldPassword As String
Dim UI1003 As USER_INFO_1003
Dim dwLevel As Long
Dim lRet As String
Dim sNew As String

'strServer = StrConv(ServerName, vbUnicode)
strUserName = StrConv(Username, vbUnicode)
'strOldPassword = StrConv(OldPassword, vbUnicode)
strNewPassword = StrConv(NewPassword, vbUnicode)
If Left(ServerName, 2) = "\\ " Then
strServer = StrConv(ServerName, vbUnicode)
Else
' Domain was referenced, get the Primary Domain Controller
strServer = StrConv(GetPrimaryDCName(ServerName), vbUnicode)
End If
If OldPassword = " " Then
' Administrative over-ride of existing password.
' Does not require old password

dwLevel = 1003
sNew = NewPassword
UI1003.usri1003_password = StrPtr(sNew)
lRet = NetUserSetInfo(strServer, strUserName, dwLevel, UI1003, 0&)
Else
' Set the Old Password and attempt to change the user 's password
strOldPassword = StrConv(OldPassword, vbUnicode)
lRet = NetUserChangePassword(strServer, strUserName, strOldPassword, strNewPassword)
End If
If lRet <> 0 Then
DisplayError lRet
Else
MsgBox "Password Change was Successful "
End If
End Function

Private Sub DisplayError(ByVal lCode As Long)
Dim sMsg As String
Dim sRtrnCode As String
Dim lFlags As Long
Dim hModule As Long
Dim lRet As Long

hModule = 0
sRtrnCode = Space$(256)
lFlags = FORMAT_MESSAGE_FROM_SYSTEM

' if lRet is in the network range, load the message source

If (lCode > = NERR_BASE And lCode <= MAX_NERR) Then
hModule = LoadLibraryEx( "netmsg.dll ", 0&, LOAD_LIBRARY_AS_DATAFILE)

If (hModule <> 0) Then
lFlags = lFlags Or FORMAT_MESSAGE_FROM_HMODULE
End If

End If

' Call FormatMessage() to allow for message text to be acquired
' from the system or the supplied module handle.
'

lRet = FormatMessage(lFlags, hModule, lCode, 0&, _
sRtrnCode, 256&, 0&)
If lRet = 0 Then
MsgBox "FormatMessage Error : " & Err.LastDllError
End If

' if you loaded a message source, unload it.
'
If (hModule <> 0) Then
FreeLibrary (hModule)
End If

'//... now display this string
sMsg = "ERROR: " & lCode & " - " & sRtrnCode

MsgBox sMsg

End Sub

热点排行