user1137890
user1137890

Reputation: 157

Why does LogonUserW fail with error code 183 (ERROR_ALREADY_EXISTS)

I checked the error code by calling GetLastError() right after the failure of LogonUserW, it is always 183, but I don't know why does LogonUserW fail with such a value. Searched the msdn, and found 183 (ERROR_ALREADY_EXISTS) means "Cannot create a file when that file already exists", so what file will LogonUserW create?

Can anyone shed some light here please?

if (LogonUserW(uniUserName, uniDomainName, uniPassword, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token))
{
//do something when success
}
else
{
    STI_LOG(LOG_AUTH_DETAILS, ("Login fail\n"), true);
    DWORD ec = GetLastError();
    String message;
    switch (ec)
    {
        case ERROR_PRIVILEGE_NOT_HELD:
            message = "Error Privilege not held\n";
            break;
        case ERROR_LOGON_FAILURE:
            message = "Error Logon Failure\n";
            break;
        //...
        default:
            message = "Other errors\n";
    }
    STI_LOG(LOG_ERROR, ("Fail to log in user: %d-%s\n", ec, message.getCString()), true);
}

Upvotes: 2

Views: 1680

Answers (1)

Roger Rowland
Roger Rowland

Reputation: 26259

Before you call GetLastError, you are executing some logging code in this macro:

STI_LOG(LOG_AUTH_DETAILS, ("Login fail\n"), true);

It's quite possible that one of the API calls in this logging function is also setting the last error flag. Certainly an error like "file already exists" is much more consistent with a logging function than with a logon.

So, first of all you should call GetLastError immediately after the failing function call:

if (LogonUserW(uniUserName, uniDomainName, 
        uniPassword, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &token))
{
    //do something when success
}
else
{
    DWORD ec = GetLastError();

    STI_LOG(LOG_AUTH_DETAILS, ("Login fail\n"), true);

    // ... etc. ...
}

As it says on MSDN:

msdn

After you make this change, see if the return code from GetLastError is more like what you expected.

Upvotes: 6

Related Questions