Reputation: 11
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
Upvotes: 0
Views: 314
Reputation: 11
After looking at the status code rather than m_dwLastError it turns out it was NERR_PasswordTooShort as the minimum password requirement on a fresh Windows 2008 server is 8 characters.
Upvotes: 1