views:

265

answers:

3

Hi,

I have one application that interacts with the server. If the server is down then then I will get ERROR_WINHTTP_CANNOT_CONNECT I am using getLastError() API to get this error code, I am handling this error code to display proper error messages to the users. This program works fine in Windows 2003. When i tried with Windows7 i am not getting any error, getLastError() API returns 0 every time even if error is occurred. I am using C++ as of programming language is concerned.

Thanks In advance

Santhu

A: 

If you make any Windows API calls between the failure and the time you call GetLastError(), the error code can get reset to 0 when that API call succeeds.

You need to call GetLastError() immediately after the failure, and save that value rather than trying to wait and call GetLastError() later.

John Knoeller
A: 

I have observed different behavior of GetLastError API in Windows 2003 and in Windows 7. Below are my observation details

In windows 2003:

Example Code:

WinHttpOpen () – Completes successfully

Winhttpconnect() – This API is failed due to some reasons, say error code 12029

GetLastErrorCode() – Returns error code 12029 as expected

WinHttpCloseHandle(hOpen); - Closing handle for HttpOpen, completes successfilly

GetLastErrorCode() – Returns error code 12029

In Windows 7

Example Code:

WinHttpOpen () – Completes successfully

Winhttpconnect() – This API is failed due to some reasons, say error code 12029

GetLastErrorCode() – Returns error code 12029 as expected

WinHttpCloseHandle(hOpen); - Closing handle for HttpOpen, completes successfully

GetLastErrorCode() – Returns error code 0 // see the difference with Windows 2003 example, on windows 2003 this API returns last error which is 1209

Santhosha
A: 

Answer From Microsoft on this behaviuor

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. 
Santhosha