简介
通过Windows API对Windows用户进行添加, 密码修改, 禁用等操作;
代码
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")
#include <stdio.h>
#include <windows.h>
#include <lm.h>
#include <atlstr.h>
int main()
{
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
char name[] = "LDH"; //用户名
char passwd[] = "!Hello123"; //用户密码
char new_passwd[] = "!Hello1234"; //新密码
LPWSTR user_name = CA2W(name);
LPWSTR user_passwd = CA2W(passwd);
LPWSTR new_user_passwd = CA2W(new_passwd);
ui.usri1_name = user_name; // argv[2]
ui.usri1_password = user_passwd;
ui.usri1_priv = USER_PRIV_USER;
ui.usri1_home_dir = NULL;
ui.usri1_comment = NULL;
ui.usri1_flags = UF_SCRIPT;
ui.usri1_script_path = NULL;
// 添加用户到admin
nStatus = NetUserAdd(NULL,
dwLevel,
(LPBYTE)&ui,
&dwError);
// If the call succeeds, inform the user.
if (nStatus == NERR_Success)
fwprintf(stderr, L"User %s has been successfully added on LOCAL\n",
user_name);
// Otherwise, print the system error.
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
// CHANGE PASSWD 修改用户密码
nStatus = NetUserChangePassword(NULL, user_name, user_passwd, new_user_passwd);
if (nStatus == NERR_Success)
fwprintf(stderr, L"User password has been changed successfully\n");
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
// Ban user 禁用用户
USER_INFO_1008 ui1;
ui1.usri1008_flags = UF_SCRIPT | UF_ACCOUNTDISABLE;
// Call the NetUserSetInfo function
// to disable the account, specifying level 1008.
nStatus = NetUserSetInfo(NULL,
user_name,
1008,
(LPBYTE)&ui1,
NULL);
// Display the result of the call.
if (nStatus == NERR_Success)
fwprintf(stderr, L"User account %s has been disabled\n", user_name);
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
// Delete user 删除用户
nStatus = NetUserDel(NULL, user_name);
// Display the result of the call.
if (nStatus == NERR_Success)
fwprintf(stderr, L"User %s has been successfully deleted on LOCAL\n",
user_name);
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
return 0;
}