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

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 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题