Search code examples
winapiwindows-7getlasterror

Problem with GetLastError API on windows 7


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


Solution

  • 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