NetUserSetInfo:“;找不到用户名"; 我写的是一个C++程序,可以改变登录用户的密码(只有一个用户将存在于该机器上),这个程序将作为该机器上的服务运行,我找到了一个方法:“代码> NETUSERSETIVION来执行这个作业,我选择 NETUSERSETIGNOB/的原因是,我不想输入旧密码,在下面找到我的代码 void chpwd::initialize() // function that initialize params of NetUserSetInfo { cout<<"initializing"<<endl; string un=getenv("USERDOMAIN"); un+="\\"; un+=getenv("USERNAME"); //"USERDOMAIN\USERNAME", I gave name also,since the target machine won't be in domain wstring uname=wstring(un.begin(),un.end()); wUserName=(wchar_t*)uname.c_str(); wcout<<wUserName<<endl; wComputerName=_wgetenv (L"COMPUTERNAME"); wcout<<wComputerName<<endl; string pw(p.genpassword()); cout<<"pw "<<pw<<endl; wstring pwd=wstring(pw.begin(),pw.end()); wcout.flush(); wNewPassword=const_cast<LPWSTR>(pwd.c_str()); wcout<<wNewPassword<<endl; } bool chpwd::chngpwd() //funtion that changes password { initialize(); do { pi1003.usri1003_password = wNewPassword; wcout.flush(); wcout<<wNewPassword<<endl; nas = NetUserSetInfo( wComputerName, // computer name wUserName, // username 1003, // info level (LPBYTE)&pi1003, // new info NULL ); if(nas != NERR_Success) { DisplayErrorText(nas); l.logic_log(l.time(),"Error occured while reseting password \n old password \""+p.prevp+"\" retains"); } Sleep(1000); }while(nas != NERR_Success); return 1; }

NetUserSetInfo:“;找不到用户名"; 我写的是一个C++程序,可以改变登录用户的密码(只有一个用户将存在于该机器上),这个程序将作为该机器上的服务运行,我找到了一个方法:“代码> NETUSERSETIVION来执行这个作业,我选择 NETUSERSETIGNOB/的原因是,我不想输入旧密码,在下面找到我的代码 void chpwd::initialize() // function that initialize params of NetUserSetInfo { cout<<"initializing"<<endl; string un=getenv("USERDOMAIN"); un+="\\"; un+=getenv("USERNAME"); //"USERDOMAIN\USERNAME", I gave name also,since the target machine won't be in domain wstring uname=wstring(un.begin(),un.end()); wUserName=(wchar_t*)uname.c_str(); wcout<<wUserName<<endl; wComputerName=_wgetenv (L"COMPUTERNAME"); wcout<<wComputerName<<endl; string pw(p.genpassword()); cout<<"pw "<<pw<<endl; wstring pwd=wstring(pw.begin(),pw.end()); wcout.flush(); wNewPassword=const_cast<LPWSTR>(pwd.c_str()); wcout<<wNewPassword<<endl; } bool chpwd::chngpwd() //funtion that changes password { initialize(); do { pi1003.usri1003_password = wNewPassword; wcout.flush(); wcout<<wNewPassword<<endl; nas = NetUserSetInfo( wComputerName, // computer name wUserName, // username 1003, // info level (LPBYTE)&pi1003, // new info NULL ); if(nas != NERR_Success) { DisplayErrorText(nas); l.logic_log(l.time(),"Error occured while reseting password \n old password \""+p.prevp+"\" retains"); } Sleep(1000); }while(nas != NERR_Success); return 1; },c++,winapi,C++,Winapi,同样,变量是用适当的值获得的,但仍然是正确的 说 找不到用户名使用wstring(迭代器,迭代器)不是将Ansi编码的std::string转换为Unicodestd::wstring的正确方法。这只适用于ASCII字符,其他字符将无法正确转换。使用MultiByteToWideChar()或等效工具,或者只使用\u wgetenv()而不是getenv() 但更重要的是,您应该直接将所有initialize()代码移动到chngpwd()中,并去掉所有中间变量。您正在初始化wUserName和

同样,变量是用适当的值获得的,但仍然是正确的 说

找不到用户名

使用
wstring(迭代器,迭代器)
不是将Ansi编码的
std::string
转换为Unicode
std::wstring
的正确方法。这只适用于ASCII字符,其他字符将无法正确转换。使用
MultiByteToWideChar()
或等效工具,或者只使用
\u wgetenv()
而不是
getenv()

但更重要的是,您应该直接将所有
initialize()
代码移动到
chngpwd()
中,并去掉所有中间变量。您正在初始化
wUserName
wNewPassword
,当
initialize()
退出时,使用来自超出范围的局部变量的数据。因此,当它们被传递到
NetUserSetInfo()
时,它们将指向无效内存

尝试类似以下内容:

bool chpwd::chngpwd()
{
    wstring uname = _wgetenv(L"USERDOMAIN");
    uname += L"\\";
    uname += _wgetenv(L"USERNAME");
    wcout << L"un " << uname << endl;

    wstring cname = _wgetenv (L"COMPUTERNAME");
    wcout << L"cn " << cname << endl;

    wstring pwd;
    string pw = p.genpassword();
    int len = MultiByteToWideChar(CP_ACP, 0, pw.c_str(), pw.length(), NULL, 0);
    if (len > 0)
    {
        pwd.resize(len);
        MultiByteToWideChar(CP_ACP, 0, pw.c_str(), pw.length(), &pwd[0], len);
    }
    wcout << L"pw " << pwd << endl;

    USER_INFO_1003 pi1003;
    pi1003.usri1003_password = const_cast<LPWSTR>(pwd.c_str());

    NET_API_STATUS nas;
    do
    {
        nas = NetUserSetInfoW(
                   cname.c_str(),  // computer name
                   uname.c_str(),      // username
                   1003,           // info level
                   (LPBYTE)&pi1003,     // new info
                   NULL
        );
        if (nas == NERR_Success) break;
        DisplayErrorText(nas);
        l.logic_log(l.time(), "Error occured while reseting password \n old password \"" + p.prevp + "\" retains");
        Sleep(1000);
    }
    while (true);
    return 1;
}
bool chpwd::chngpwd()
{
wstring uname=_wgetenv(L“用户域”);
uname+=L“\\”;
uname+=_wgetenv(L“用户名”);

wcout这是一个尝试和错误测试用例,但是您是否尝试过将计算机名设置为NULL,以便它使用本地计算机(正如您所提到的,服务正在本地计算机上安装)您是否尝试过
NetUserGetInfo
检索任何数据并遇到相同的问题?您没有显示变量的类型,也没有显示变量的值。错误代码表明您的用户名错误。您现在对我们有何期望?我们应该为您猜测什么?您测试过
NetUserSetInfo使用硬编码字符串文字来消除为
NetUserSetInfo
构建参数期间的任何错误?您不能将字符*强制转换为wchar\u t*,用户名将转换为中文。顺便说一句,使用调试器很容易看到。需要转换,请使用mbstowcs()或MultiByteToWideChar()。或使用NetUserSetInfoA()。如果用户名包含无法在当前代码页中编码的Unicode代码点,则此操作仍会失败。请一致使用wchar\u t。据我所知,
NetUser…
函数不接受符合条件的用户名,无论是反斜杠还是符号格式。如果这是本地帐户,请尝试使用裸用户名。如果这是域帐户,您需要找到域控制器的名称并将其作为计算机名传递。
bool chpwd::chngpwd()
{
    wstring uname = _wgetenv(L"USERDOMAIN");
    uname += L"\\";
    uname += _wgetenv(L"USERNAME");
    wcout << L"un " << uname << endl;

    wstring cname = _wgetenv (L"COMPUTERNAME");
    wcout << L"cn " << cname << endl;

    wstring pwd;
    string pw = p.genpassword();
    int len = MultiByteToWideChar(CP_ACP, 0, pw.c_str(), pw.length(), NULL, 0);
    if (len > 0)
    {
        pwd.resize(len);
        MultiByteToWideChar(CP_ACP, 0, pw.c_str(), pw.length(), &pwd[0], len);
    }
    wcout << L"pw " << pwd << endl;

    USER_INFO_1003 pi1003;
    pi1003.usri1003_password = const_cast<LPWSTR>(pwd.c_str());

    NET_API_STATUS nas;
    do
    {
        nas = NetUserSetInfoW(
                   cname.c_str(),  // computer name
                   uname.c_str(),      // username
                   1003,           // info level
                   (LPBYTE)&pi1003,     // new info
                   NULL
        );
        if (nas == NERR_Success) break;
        DisplayErrorText(nas);
        l.logic_log(l.time(), "Error occured while reseting password \n old password \"" + p.prevp + "\" retains");
        Sleep(1000);
    }
    while (true);
    return 1;
}