0

我有一个与服务器交互的应用程序。如果服务器已关闭,那么我将ERROR_WINHTTP_CANNOT_CONNECT使用getLastError()API 获取此错误代码,我正在处理此错误代码以向用户显示正确的错误消息。该程序在 Windows 2003 中运行良好。当我尝试使用 Windows7 时,我没有收到任何错误,getLastError()即使发生错误,API 每次都会返回 0。我正在使用 C++ 作为编程语言。

提前致谢

桑图

4

3 回答 3

0

微软对此行为的回答

The rules for GetLastError are:

•   If the WinHttp API returns error (for example WinHttpIsHostInProxyBypassList, http://msdn.microsoft.com/en-us/library/ee861268(VS.85).aspx) this is the error and GetLastError should *NOT* be called.
o   If GetLastError() is called, regardless of the success or failure of the API, the returned value is unpredictable and may change between Windows versions, Service Packs, or even between runs.
•   If the WinHttp API returns BOOL (for example WinHttpSetTimeouts, http://msdn.microsoft.com/en-us/library/aa384116(VS.85).aspx), it indicates failure by returning FALSE. If the caller is interested in the detailed error, (s)he should call GetLastError(). Note that GetLastError should be called *if and only if* the API failed.
o   If GetLastError() is called when the API succeded (returned anything but FALSE), the returned value is unpredictable and may change between Windows versions, Service Packs, or even between runs. 
•   If the WinHttp API returns HINTERNET (pseudo handle) the rules are exactly the same, except failure is indicated by returning NULL. 
o   If GetLastError() is called when the API succeded (returned anything but NULL), the returned value is unpredictable and may change between Windows versions, Service Packs, or even between runs. 
于 2010-03-04T12:41:34.470 回答
0

如果您在失败和调用 GetLastError() 之间进行任何 Windows API 调用,则该 API 调用成功时错误代码可能会重置为 0。

您需要在失败后立即调用 GetLastError(),并保存该值,而不是尝试等待并稍后再调用 GetLastError()。

于 2010-02-17T07:28:19.647 回答
0

我在 Windows 2003 和 Windows 7 中观察到 GetLastError API 的不同行为。以下是我的观察细节

在 Windows 2003 中:

示例代码:

WinHttpOpen ()– 成功完成

Winhttpconnect()– 由于某些原因,此 API 失败,例如错误代码 12029

GetLastErrorCode()– 按预期返回错误代码 12029

WinHttpCloseHandle(hOpen); - HttpOpen 的关闭句柄,成功完成

GetLastErrorCode()– 返回错误代码 12029

在 Windows 7 中

示例代码:

WinHttpOpen ()– 成功完成

Winhttpconnect()– 由于某些原因,此 API 失败,例如错误代码 12029

GetLastErrorCode()– 按预期返回错误代码 12029

WinHttpCloseHandle(hOpen);- HttpOpen 的关闭句柄,成功完成

GetLastErrorCode()– 返回错误代码 0 //查看与 Windows 2003 示例的区别,在 Windows 2003 上,此 API 返回最后一个错误,即 1209

于 2010-02-18T10:34:10.413 回答