c语言中如何添加系统用户

c语言中如何添加系统用户,第1张

给段程序你参考:

#include <windows.h>

#include <lm.h>

BOOL AddMachineAccount(

LPWSTR wTargetComputer,

LPWSTR MachineAccount,

DWORD AccountType

)

{

LPWSTR wAccount

LPWSTR wPassword

USER_INFO_1 ui

DWORD cbAccount

DWORD cbLength

DWORD dwError

//

// Ensure a valid computer account type was passed.

//

if (AccountType != UF_WORKSTATION_TRUST_ACCOUNT &&

AccountType != UF_SERVER_TRUST_ACCOUNT &&

AccountType != UF_INTERDOMAIN_TRUST_ACCOUNT

)

{

SetLastError(ERROR_INVALID_PARAMETER)

return FALSE

}

//

// Obtain number of chars in computer account name.

//

cbLength = cbAccount = lstrlenW(MachineAccount)

//

// Ensure computer name doesn't exceed maximum length.

//

if(cbLength >MAX_COMPUTERNAME_LENGTH) {

SetLastError(ERROR_INVALID_ACCOUNT_NAME)

return FALSE

}

//

// Allocate storage to contain Unicode representation of

// computer account name + trailing $ + NULL.

//

wAccount=(LPWSTR)HeapAlloc(GetProcessHeap(), 0,

(cbAccount + 1 + 1) * sizeof(WCHAR) // Account + '$' + NULL

)

if(wAccount == NULL) return FALSE

//

// Password is the computer account name converted to lowercase

// you will convert the passed MachineAccount in place.

//

wPassword = MachineAccount

//

// Copy MachineAccount to the wAccount buffer allocated while

// converting computer account name to uppercase.

// Convert password (in place) to lowercase.

//

while(cbAccount--) {

wAccount[cbAccount] = towupper( MachineAccount[cbAccount] )

wPassword[cbAccount] = towlower( wPassword[cbAccount] )

}

//

// Computer account names have a trailing Unicode '$'.

//

wAccount[cbLength] = L'$'

wAccount[cbLength + 1] = L'\0'// terminate the string

//

// If the password is greater than the max allowed, truncate.

//

if(cbLength >LM20_PWLEN) wPassword[LM20_PWLEN] = L'\0'

//

// Initialize the USER_INFO_1 structure.

//

ZeroMemory(&ui, sizeof(ui))

ui.usri1_name = wAccount

ui.usri1_password = wPassword

ui.usri1_flags = AccountType | UF_SCRIPT

ui.usri1_priv = USER_PRIV_USER

dwError=NetUserAdd(

wTargetComputer,// target computer name

1, // info level

(LPBYTE) &ui, // buffer

NULL

)

//

// Free allocated memory.

//

if(wAccount) HeapFree(GetProcessHeap(), 0, wAccount)

//

// Indicate whether the function was successful.

//

if(dwError == NO_ERROR)

return TRUE

else {

SetLastError(dwError)

return FALSE

}

}

可以用system()函数间接调用net命令实现,例如:

system("net user abcd 1234 /add")

/*创建一个用户abcd,密码是1234*/

需要包含stdlib.h文件

******************************************************

关于修改密码,可以查询一下NetUserChangePassword()函数的用法


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11895280.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存