开发者

C++ NetUserAdd() not working?

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;
        }
    }
}


Is you os vista or win 7?, if so then you may need to raise your privilege level.


Your code works !

ScreenShot,Succeed:

C++ NetUserAdd() not working?

I modified your code :

1)

case NERR_Success:
    {
       fprintf(stderr, "Succeed!\n");
       break;
    }  //I don't know where Msg belongs to....but you should be correct

2) I put all your code inside main() because I remember I did some tests showing that some objects can't be created outside main(), I guess that's the reason why it wouldn't work. I am not sure... See if it works for you too.

3) I read the link inside the NetUserAdd MSDN page, it says NetUserAdd can't create ADMIN, I don't know why either, but I can cite it for you. When you call the NetUserAdd function, this member must be USER_PRIV_USER.

link: https://msdn.microsoft.com/en-us/library/windows/desktop/aa371109(v=vs.85).aspx


0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜