为什么 GetLastError() 在调试期间与“正常”期间返回不同的代码执行?
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);
}
当此代码从 Visual Studio 运行时故意使用不正确的用户名时,GetLastError() 返回 12014(预期)。
但是,当从命令行运行相同的代码时(使用完全相同的错误用户名)GetLastError() 返回 2? (GetErrorMessage() 确实返回了错误的密码)
我不明白有什么区别。
此外,我从命令行运行该程序,同时在 Visual Studio 中将进程附加到该程序以进行调试。我收到 12014。
每当涉及调试器时,我都会收到 12014。当我使用相同的参数“正常”运行可执行文件时,我会收到 2。
当我在调试器之外运行程序时,是否未找到 WinInet 错误代码?我需要以不同的方式编译程序吗?
任何帮助表示赞赏。谢谢。
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 "normally" 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这方面我的记忆有点模糊,但是如果您使用
CInternetException
对象的m_dwError
字段而不是调用GetLastError()
会发生什么?我的猜测是,在实际错误和调用
GetLastError()
之间,某些原因导致错误代码被重置。我不知道为什么在调试器外部运行而不是在调试器内部运行时会发生这种情况。但是,MFC 会在抛出的对象中缓存导致异常的错误代码,因此无论抛出异常后发生了什么 API 调用,您都应该能够使用缓存的值。GetErrorMessage()
返回正确的错误字符串,因为它使用此m_dwError
字段,而不是调用GetLastError()
。My memory is a little hazy in this regard, but what happens if you use the
m_dwError
field of theCInternetException
object instead of callingGetLastError()
?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 thism_dwError
field, rather than callingGetLastError()
.