2010-02-17 62 views
0

我有一個與服務器交互的應用程序。如果服務器已關閉,那麼我將得到ERROR_WINHTTP_CANNOT_CONNECT我正在使用getLastError() API來獲取此錯誤代碼,我正在處理此錯誤代碼以向用戶顯示正確的錯誤消息。這個程序在Windows 2003中工作正常。當我嘗試使用Windows7時,我沒有收到任何錯誤,即使發生錯誤,每次都會返回0 0 getLastError()。就編程語言而言,我正在使用C++。Windows 7上GetLastError API的問題

在此先感謝

Santhu

回答

0

我觀察GetLastError函數API的不同的行爲在Windows 2003和Windows 7 下面是我的觀察細節

在Windows 2003中:

示例代碼:

WinHttpOpen() - 完成成功

Winhttpconnect() - 此API由於某些原因失敗,請說錯誤代碼1202 9

GetLastErrorCode() - 返回預期

WinHttpCloseHandle(hOpen)錯誤代碼12029; - 爲HttpOpen關閉把手,successfilly

GetLastErrorCode()完成 - 返回錯誤代碼12029

在Windows 7

示例代碼:

WinHttpOpen() - 成功完成

Winhttpconnect() - 這個API由於某些原因失敗,請說錯誤代碼12029

GetLastErrorCode() - 返回預期

WinHttpCloseHandle(hOpen);錯誤代碼12029 - 關閉句柄HttpOpen,成功

GetLastErrorCode()完成 - 返回錯誤代碼0 // 看到與Windows 2003例不同,在Windows 2003的這個API返回最後一個錯誤是1209

0

如果你讓失敗和你調用GetLastError時間()之間的任何Windows API調用,錯誤代碼可以當API調用成功獲得重置爲0。

您需要在失敗後立即調用GetLastError(),並保存該值而不是等待稍後調用GetLastError()。

0

回答微軟這次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.