使用NetUserAdd API添加用户的的问题

elxf99 2003-09-08 02:09:09
我看了msdn里的资料,使用NetUserAdd API添加用户,加是加上去了,但加上去的用户名却是乱码,请各位打下指点。还有,我要是设置这个用户的一些相关信息,比如说密码永不过期,要怎么弄。在线等待。
...全文
171 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
elxf99 2003-09-10
  • 打赏
  • 举报
回复
那么,大哥,要是使用netgroupuseradd api要怎么弄?
bager 2003-09-10
  • 打赏
  • 举报
回复
实现代码如下:
#include "Lm.h"
链接Netapi32.lib


USER_INFO_1 ui;
NET_API_STATUS ret;
CString strUserName ="TestUser";
CString strUserPassword ="Abc123";

ui.usri1_name = strUserName.AllocSysString();
ui.usri1_password = strUserPassword.AllocSysString();
ui.usri1_priv = USER_PRIV_USER;
ui.usri1_home_dir = NULL;
ui.usri1_comment = NULL;
ui.usri1_flags= UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
ui.usri1_script_path = NULL;

ret=NetUserAdd(NULL,1,(LPBYTE)&ui,NULL);

SysFreeString(ui.usri1_name);
SysFreeString(ui.usri1_password);
if(ret!=NERR_Success && ret!=NERR_UserExists)
{
AfxMessageBox("Add Failed!");
}

elxf99 2003-09-08
  • 打赏
  • 举报
回复
哥哥,我用的是vc,您有vc的例子么?
xiaohedou 2003-09-08
  • 打赏
  • 举报
回复
Public Type USER_INFO_3
ptrName As Long 'LPWSTR in SDK
ptrPassword As Long 'LPWSTR in SDK
dwPasswordAge As Long 'DWORD in SDK
dwPriv As Long 'DWORD in SDK
ptrHomeDir As Long 'LPWSTR in SDK
ptrComment As Long 'LPWSTR in SDK
dwFlags As Long 'DWORD in SDK
ptrScriptPath As Long 'LPWSTR in SDK
dwAuthFlags As Long 'DWORD in SDK
ptrFullName As Long 'LPWSTR in SDK
ptrUserComment As Long 'LPWSTR in SDK
ptrParms As Long 'LPWSTR in SDK
ptrWorkstations As Long 'LPWSTR in SDK
dwLastLogon As Long 'DWORD in SDK
dwLastLogoff As Long 'DWORD in SDK
dwAcctExpires As Long 'DWORD in SDK
dwMaxStorage As Long 'DWORD in SDK
dwUnitsPerWeek As Long 'DWORD in SDK
pbLogonHours As Long 'PBYTE in SDK
dwBadPwCount As Long 'DWORD in SDK
dwNumLogons As Long 'DWORD in SDK
ptrLogonServer As Long 'LPWSTR in SDK
dwCountryCode As Long 'DWORD in SDK
dwCodePage As Long 'DWORD in SDK
dwUserId As Long 'DWORD in SDK
dwPrimaryGroupId As Long 'DWORD in SDK
ptrProfile As Long 'LPWSTR in SDK
ptrHomeDirDrive As Long 'LPWSTR in SDK
dwPasswordExpired As Long 'DWORD in SDK
End Type

Function AddUser(ByVal sName As String, ByVal UName As String, _
ByVal PWD As String, ByVal FullName As String) As Long

Dim result As Long
Dim UNPtr As Long
Dim PWDPtr As Long
Dim FNPtr As Long
Dim CMTPtr As Long
Dim SCRPtr As Long
Dim ParmError As Long
Dim sBackupDir As String

Dim SNArray() As Byte
Dim UNArray() As Byte
Dim FNArray() As Byte
Dim PWDArray() As Byte
Dim SCRArray() As Byte
Dim CMTArray() As Byte

Dim UserStruct As USER_INFO_3

' ' Move to byte arrays '
sBackupDir = Trim(sPDC)
sBackupDir = Left(sBackupDir, InStr(sBackupDir, vbNullChar) - 1)

sBackupDir = sBackupDir & sFilePath & UName & vbNullChar

result = CreateDirectory(sBackupDir, 0&)


SNArray = sName & vbNullChar
UNArray = UName & vbNullChar
FNArray = FullName & vbNullChar
PWDArray = PWD & vbNullChar
SCRArray = sScript & vbNullChar
CMTArray = sComment & vbNullChar

' ' Allocate buffer space '
result = NetAPIBufferAllocate(UBound(UNArray) + 1, UNPtr)
result = NetAPIBufferAllocate(UBound(FNArray) + 1, FNPtr)
result = NetAPIBufferAllocate(UBound(PWDArray) + 1, PWDPtr)
result = NetAPIBufferAllocate(UBound(SCRArray) + 1, SCRPtr)
result = NetAPIBufferAllocate(UBound(CMTArray) + 1, CMTPtr)
' ' Copy arrays to the buffer '
result = StrToPtr(UNPtr, UNArray(0))
result = StrToPtr(FNPtr, FNArray(0))
result = StrToPtr(PWDPtr, PWDArray(0))
result = StrToPtr(SCRPtr, SCRArray(0))
result = StrToPtr(CMTPtr, CMTArray(0))
' ' Fill the structure '
With UserStruct
.ptrName = UNPtr
.ptrPassword = PWDPtr
.dwPasswordAge = 0
.dwPriv = USER_PRIV_USER
.ptrHomeDir = 0
.ptrComment = CMTPtr
.dwFlags = lFlags 'UF_NORMAL_ACCOUNT Or UF_SCRIPT Or UF_DONT_EXPIRE_PASSWD
.ptrScriptPath = SCRPtr
.dwAuthFlags = 0
.ptrFullName = FNPtr
.ptrUserComment = 0
.ptrParms = 0
.ptrWorkstations = 0
.dwLastLogon = 0
.dwLastLogoff = 0
.dwAcctExpires = TIMEQ_FOREVER
.dwMaxStorage = lMaxDiskSpace 'USER_MAXSTORAGE_UNLIMITED
.dwUnitsPerWeek = 0
.pbLogonHours = 0
.dwBadPwCount = 0
.dwNumLogons = 0
.ptrLogonServer = 0
.dwCountryCode = 0
.dwCodePage = 0
.dwUserId = 0
.dwPrimaryGroupId = DOMAIN_GROUP_RID_USERS
.ptrProfile = 0
.ptrHomeDir = 0
.dwPasswordExpired = 0
End With
' ' Add the user '
result = NetUserAdd3(SNArray(0), 3, UserStruct, ParmError)
AddUser = result
If result <> 0 Then
Debug.Print "Error " & result & " in parameter " & ParmError _
& " when adding user " & UName
Else

End If
' ' Release buffers from memory '
result = NetAPIBufferFree(UNPtr)
result = NetAPIBufferFree(PWDPtr)

End Function


Public Function MakePassword() As String

'Dim sPassword As String

Dim iRnd As Integer
Dim X As Integer
Dim sPassword As String

Do While X < 8
Randomize
iRnd = Int((42 * Rnd) + 48)
If iRnd < 58 Or iRnd > 64 Then
X = X + 1
sPassword = sPassword & Chr$(iRnd)

End If

Loop
MakePassword = LCase(sPassword)

End Function
xiaohedou 2003-09-08
  • 打赏
  • 举报
回复
Function AddUser(ByVal sName As String, ByVal UName As String, _
ByVal PWD As String, ByVal FullName As String) As Long

Dim result As Long
Dim UNPtr As Long
Dim PWDPtr As Long
Dim FNPtr As Long
Dim CMTPtr As Long
Dim SCRPtr As Long
Dim ParmError As Long
Dim sBackupDir As String

Dim SNArray() As Byte
Dim UNArray() As Byte
Dim FNArray() As Byte
Dim PWDArray() As Byte
Dim SCRArray() As Byte
Dim CMTArray() As Byte

Dim UserStruct As USER_INFO_3

' ' Move to byte arrays '
sBackupDir = Trim(sPDC)
sBackupDir = Left(sBackupDir, InStr(sBackupDir, vbNullChar) - 1)

sBackupDir = sBackupDir & sFilePath & UName & vbNullChar

result = CreateDirectory(sBackupDir, 0&)


SNArray = sName & vbNullChar
UNArray = UName & vbNullChar
FNArray = FullName & vbNullChar
PWDArray = PWD & vbNullChar
SCRArray = sScript & vbNullChar
CMTArray = sComment & vbNullChar

' ' Allocate buffer space '
result = NetAPIBufferAllocate(UBound(UNArray) + 1, UNPtr)
result = NetAPIBufferAllocate(UBound(FNArray) + 1, FNPtr)
result = NetAPIBufferAllocate(UBound(PWDArray) + 1, PWDPtr)
result = NetAPIBufferAllocate(UBound(SCRArray) + 1, SCRPtr)
result = NetAPIBufferAllocate(UBound(CMTArray) + 1, CMTPtr)
' ' Copy arrays to the buffer '
result = StrToPtr(UNPtr, UNArray(0))
result = StrToPtr(FNPtr, FNArray(0))
result = StrToPtr(PWDPtr, PWDArray(0))
result = StrToPtr(SCRPtr, SCRArray(0))
result = StrToPtr(CMTPtr, CMTArray(0))
' ' Fill the structure '
With UserStruct
.ptrName = UNPtr
.ptrPassword = PWDPtr
.dwPasswordAge = 0
.dwPriv = USER_PRIV_USER
.ptrHomeDir = 0
.ptrComment = CMTPtr
.dwFlags = lFlags 'UF_NORMAL_ACCOUNT Or UF_SCRIPT Or UF_DONT_EXPIRE_PASSWD
.ptrScriptPath = SCRPtr
.dwAuthFlags = 0
.ptrFullName = FNPtr
.ptrUserComment = 0
.ptrParms = 0
.ptrWorkstations = 0
.dwLastLogon = 0
.dwLastLogoff = 0
.dwAcctExpires = TIMEQ_FOREVER
.dwMaxStorage = lMaxDiskSpace 'USER_MAXSTORAGE_UNLIMITED
.dwUnitsPerWeek = 0
.pbLogonHours = 0
.dwBadPwCount = 0
.dwNumLogons = 0
.ptrLogonServer = 0
.dwCountryCode = 0
.dwCodePage = 0
.dwUserId = 0
.dwPrimaryGroupId = DOMAIN_GROUP_RID_USERS
.ptrProfile = 0
.ptrHomeDir = 0
.dwPasswordExpired = 0
End With
' ' Add the user '
result = NetUserAdd3(SNArray(0), 3, UserStruct, ParmError)
AddUser = result
If result <> 0 Then
Debug.Print "Error " & result & " in parameter " & ParmError _
& " when adding user " & UName
Else

End If
' ' Release buffers from memory '
result = NetAPIBufferFree(UNPtr)
result = NetAPIBufferFree(PWDPtr)

End Function
xiaohedou 2003-09-08
  • 打赏
  • 举报
回复
几个VB的程序片断:
Function AddUserToGroup(ByVal sName As String, _
ByVal GName As String, ByVal UName As String) As Long
' ' This only adds users to global groups - not to local groups
Dim SNArray() As Byte
Dim GNArray() As Byte
Dim UNArray() As Byte
Dim result As Long

SNArray = sName & vbNullChar
GNArray = GName & vbNullChar
UNArray = UName & vbNullChar
result = NetGroupAddUser(SNArray(0), GNArray(0), UNArray(0))
If result = 2220 Then Debug.Print _
"There is no **GLOBAL** group '" & GName & "'"
AddUserToGroup = result
End Function

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧