C++ NetUserAdd() not working?
- by Brett Powell
I posted earlier about how to do this, and got some great replies, and have managed to get the code written based off the MSDN example. However, it does not seem to be working properly. Its printing out the ERROR_ACCESS_DENIED message, but im not sure why as I am running it as a full admin. I was initially trying to create a USER_PRIV_ADMIN, but the MSDN said it can only use USER_PRIV_USER, but sadly neither work. Im hoping someone can spot a mistake or has an idea.
Thanks!
void AddRDPUser()
{
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
ui.usri1_name = L"DummyUserAccount";
ui.usri1_password = L"a2cDz3rQpG8";
//ignored by NetUserAdd
//ui.usri1_password_age = -1;
ui.usri1_priv = USER_PRIV_USER; //USER_PRIV_ADMIN;
ui.usri1_home_dir = NULL;
ui.usri1_comment = NULL;
ui.usri1_flags = UF_SCRIPT;
ui.usri1_script_path = NULL;
nStatus = NetUserAdd(NULL, dwLevel, (LPBYTE)&ui, &dwError);
switch (nStatus)
{
case NERR_Success:
{
Msg("SUCCESS!\n");
break;
}
case NERR_InvalidComputer:
{
fprintf(stderr, "A system error has occurred: NERR_InvalidComputer\n");
break;
}
case NERR_NotPrimary:
{
fprintf(stderr, "A system error has occurred: NERR_NotPrimary\n");
break;
}
case NERR_GroupExists:
{
fprintf(stderr, "A system error has occurred: NERR_GroupExists\n");
break;
}
case NERR_UserExists:
{
fprintf(stderr, "A system error has occurred: NERR_UserExists\n");
break;
}
case NERR_PasswordTooShort:
{
fprintf(stderr, "A system error has occurred: NERR_PasswordTooShort\n");
break;
}
case ERROR_ACCESS_DENIED:
{
fprintf(stderr, "A system error has occurred: ERROR_ACCESS_DENIED\n");
break;
}
}
}