计算机管理本地用户和组里已经显示存在用户,但在C++代码中使用:
NET_API_STATUS NetUserChangePassword(
LPWSTR domainname, [L"",]
LPWSTR username, [L"administrator",]
LPWSTR oldpassword,[L"12345",]
LPWSTR newpassword [L"54321"]
);
我试过好几次了,他总是返回 NERR_UserNotFound,说该
用户不存在,明显有问题,不知道哪位大侠能给于指点。
// test3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "common.h"
#include "addIntoGroup.h"
#include "utf8.h"
#ifndef UNICODE
#define UNICODE
#endif
using namespace std;
#pragma comment(lib,"Netapi32.lib")
int main(int argc, char* argv[])
{
USER_INFO_1 ui;
DWORD level = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
string user_name = "ework_ganqi";
wstring w_username;
string err_msg;
if (UTF8_To_UTF16(user_name.c_str(), w_username) == false) {
err_msg = "Failed to convert a member name to a unicode string.";
return -1;
}
ui.usri1_name = (LPWSTR)w_username.c_str();
ui.usri1_password = L"12345sdas@139.mail.com";
//ui.usri1_password = NULL;
ui.usri1_priv = USER_PRIV_USER;
ui.usri1_home_dir = NULL;
ui.usri1_comment = L"This is a test user account for using [NetUserAdd] function";
ui.usri1_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD | UF_PASSWD_CANT_CHANGE | UF_NORMAL_ACCOUNT;
ui.usri1_script_path = NULL;
nStatus = NetUserAdd(NULL,level,(LPBYTE)&ui,&dwError);
switch(nStatus)
{
case NERR_Success:
cout << "NetUserAdd excuted successfully!" << endl;
break;
case ERROR_ACCESS_DENIED:
cout << "Permission denied!" << endl;
break;
case NERR_NotPrimary:
cout << "The operation is allowed only on the primary domain controller of the domain.!" << endl;
break;
case NERR_UserExists:
cout << "The user account already exists." << endl;
break;
case NERR_PasswordTooShort:
cout << "The password is shorter than required." << endl;
break;
default:
break;
}
wchar_t name[256] = {0};
wcscpy_s(name,ui.usri1_name);
#if 0
LOCALGROUP_MEMBERS_INFO_3 account;
account.lgrmi3_domainandname = name;
NET_API_STATUS Status = NetLocalGroupAddMembers(NULL,L"Remote Desktop Users",3,(LPBYTE)&account,1);
if(Status == NERR_Success||Status == ERROR_MEMBER_IN_ALIAS)
cout<<"Successfully!"<<endl;
else
cout<<"Add in group failed!"<<endl;
#endif
CWinLocalGroup RDP_group;
const string group_name("Remote Desktop Users");
NET_API_STATUS error_code;
if(RDP_group.Find(group_name.c_str(),error_code))
{
if(!RDP_group.add_member(group_name, user_name, err_msg))
{
cout << "Add group member failed,please check!" << endl;
}
}
nStatus = NetUserChangePassword(NULL,ui.usri1_name,ui.usri1_password,L"123456789@_MACganqi");
cout << "=======================" << endl;
switch(nStatus)
{
case NERR_Success:
cout << "Change password succeed!" << endl;
break;
case NERR_UserNotFound:
cout << "The user name could not be found." << endl;
break;
case ERROR_ACCESS_DENIED:
cout << "The user does not have access to the requested information." << endl;
break;
case NERR_PasswordTooShort:
cout << "Password too short!" << endl;
break;
default:
break;
}
return 0;
}