想飞-鱼 2015-05-07 06:52 采纳率: 0%
浏览 2381

win7专业版中NetUserChangePassword函数找不到用户的问题

计算机管理本地用户和组里已经显示存在用户,但在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;

}

  • 写回答

2条回答 默认 最新

  • oyljerry 2015-05-07 07:06
    关注

    自己再建一个帐户试试呢,Administrator是内嵌的帐号,不知道会不会有影响

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?