简体   繁体   中英

NetUserAdd failing when running on workgroup Windows Server 2008 R2

I'm trying to add a local user to Windows using the Network Management API. I'm having no problems when running on Windows 7 (either on a domain or workgroup) or windows 2008 R2 server when on a domain. However, if I run this on Windows server 2008 R2 that isn't on a domain I get an error. Problem is the error code returned is -1 rather than one of the given errors in the documentation

bool CLocalUsers::AddUser(LPCTSTR lpszUserName, LPCTSTR lpszPassword)
{
    // Clear error code
    m_dwLastError = 0;

    USER_INFO_1 ui;
    ui.usri1_name = (LPTSTR)(LPCTSTR)lpszUserName;
    ui.usri1_priv = USER_PRIV_USER;
    ui.usri1_home_dir = NULL;
    ui.usri1_comment = NULL;
    ui.usri1_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD;
    ui.usri1_script_path = NULL;

    // Point to a zero character password if null
    if(lpszPassword == NULL)
    {
        lpszPassword = _T("");
        ui.usri1_flags |= UF_PASSWD_NOTREQD;
    }
    ui.usri1_password = (LPTSTR)(LPCTSTR)lpszPassword;

    // Add the user
    NET_API_STATUS nStatus = ::NetUserAdd(NULL, 1, (LPBYTE)&ui, &m_dwLastError);
    if(nStatus != NERR_Success)
    {
        // DEBUG ONLY
        CString szErrorMsg;
        szErrorMsg.Format(_T("Error code %d"), m_dwLastError);
        ::AfxMessageBox(szErrorMsg, MB_OK);
        // DEBUG ONLY
        return false;
    }
    return true;
}

It's being run under admin privileges. I can call other Network functions with out any problems (NetUserGetInfo, NetLocalGroupAddMembers).

I've experimented with different password sizes but I can happily create the same account with the same information using the Server Manager tool that comes with Windows 2008

Thanks

查看状态代码而不是m_dwLastError后,发现它是NERR_PasswordTooShort,因为在新的Windows 2008服务器上的最低密码要求为8个字符。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM