开发者

Why does GetLastError() return different codes during debug vs "normal" execution?

开发者 https://www.devze.com 2022-12-18 19:49 出处:网络
try { pConnect = sess->GetFtpConnection(ftpArgs.host, ftpArgs.userName, ftpArgs.password, port, FALSE );
try
{      

    pConnect = sess->GetFtpConnection(ftpArgs.host, ftpArgs.userName, ftpArgs.password, port, FALSE );
}
catch (CInternetException* pEx) 
{
    loginErrCode = GetLastError();
    printf("loginErrCode: %d\n", loginErrCode);

    if(loginErrCode == 12013)
    {
        printf("Incorrect user name!\n");
        exit(0);
    }
    else if(loginErrCode == 12014)
    {
        printf("Incorrect password!\n");
        exit(0);
    }
    else if(loginErrCode == 12007)
    {
        printf("Incorrect server name!\n");
        exit(0);
    }
    else //display all other errors
    {   
        TCHAR sz[1024];
        pEx->GetErrorMessage(sz, 1024);
        printf("ERROR!  %s\n, sz);
        pEx->Delete();
        exit(0);
    }  

When this code runs from visual studio with an intentional incorrect user name, GetLastError() returns 12014 (expected).

However, when running the same code from the command line (with the same exact incorrect username) GetLastError() returns 2? (the GetErrorMessage() does return incorrect password)

I do not understand what the difference is.

In addition, I ran the program from the command line while attaching the process to it in visual studio, to debug. I received 12014.

Whenever the debugger is involved, I get 12014. When I run the executable "n开发者_高级运维ormally" with the same parameters, I get 2.

Are the WinInet error codes not being found when I run the program outside of the debugger? Do I need to compile the program differently?

Any help is appreciated. Thank You.


My memory is a little hazy in this regard, but what happens if you use the m_dwError field of the CInternetException object instead of calling GetLastError()?

My guess is that something is causing the error code to be reset between when the actual error and your call to GetLastError(). I don't know why this happens when running outside the debugger but not inside the debugger. However, MFC caches the error code that caused the exception in the thrown object, so you should be able to use the cached value regardless of what API calls have happened since the exception was thrown.

GetErrorMessage() returns the correct error string because it uses this m_dwError field, rather than calling GetLastError().

0

精彩评论

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