我有一个应用程序,它与服务器交互。如果服务器停机,则然后我会得到ERROR_WINHTTP_CANNOT_CONNECT我使用getLastError() API来获取该错误代码,我处理这个错误代码,以显示正确的错误信息给用户。本程序在Windows 2003中正常工作,当我试图用Windows7的我没有得到任何错误,getLastError() API每即使发生错误时,返回0。我使用C ++如编程的语言有关的。

由于事先

Santhu

有帮助吗?

解决方案 2

我已在Windows 2003和Windows 7观察到GetLastError函数API的不同的行为。 下面是我的观察细节

<强> 2003年窗口:

实施例编号:

WinHttpOpen () - 成功完成

Winhttpconnect() - 该API失败,由于一些原因,说错误代码12029

GetLastErrorCode() - 如预期返回错误代码12029

WinHttpCloseHandle(hOpen); - 用于HttpOpen关闭把手,完成successfilly

GetLastErrorCode() - 返回错误代码12029

<强>在Windows 7

实施例编号:

WinHttpOpen () - 成功完成

Winhttpconnect() - 该API失败,由于一些原因,说错误代码12029

GetLastErrorCode() - 如预期返回错误代码12029

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

GetLastErrorCode() - 返回错误代码0 // 看到与Windows 2003示例的差,在2003年的窗口此API返回一个错误,其是1209

其他提示

如果你让失败和你调用GetLastError(的时间)之间的任何Windows API调用,错误代码可以得到当API调用成功重置为0。

您需要调用GetLastError()失败后,并保存价值,而不是尝试后等待和调用GetLastError()。

回答微软这种行为

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. 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top