0

サーバーと対話するアプリケーションが1つあります。サーバーがダウンしている場合は、 APIをERROR_WINHTTP_CANNOT_CONNECT使用getLastError()してこのエラーコードを取得しています。このエラーコードを処理して、ユーザーに適切なエラーメッセージを表示しています。このプログラムはWindows2003で正常に動作します。Windows7で試してgetLastError()もエラーが発生しない場合、エラーが発生してもAPIは毎回0を返します。プログラミング言語に関しては、C++を使用しています。

前もって感謝します

サントゥー

4

3 に答える 3

0

失敗してからGetLastError()を呼び出すまでの間にWindows API呼び出しを行うと、そのAPI呼び出しが成功すると、エラーコードが0にリセットされる可能性があります。

失敗の直後にGetLastError()を呼び出し、後でGetLastError()を呼び出そうとするのではなく、その値を保存する必要があります。

于 2010-02-17T07:28:19.647 に答える
0

Windows2003とWindows7でGetLastErrorAPIの異なる動作を観察しました。以下は私の観察の詳細です

Windows 2003の場合:

サンプルコード:

WinHttpOpen ()–正常に完了します

Winhttpconnect()–このAPIは、エラーコード12029などの何らかの理由で失敗しました

GetLastErrorCode()–エラーコード12029を期待どおりに返します

WinHttpCloseHandle(hOpen); -HttpOpenのハンドルを閉じると、正常に完了します

GetLastErrorCode()–エラーコード12029を返します

Windows7の場合

サンプルコード:

WinHttpOpen ()–正常に完了します

Winhttpconnect()–このAPIは、エラーコード12029などの何らかの理由で失敗しました

GetLastErrorCode()–エラーコード12029を期待どおりに返します

WinHttpCloseHandle(hOpen);-HttpOpenのハンドルを閉じると、正常に完了します

GetLastErrorCode()–エラーコード0を返します// Windows 2003の例との違いを確認します。Windows2003では、このAPIは最後のエラーである1209を返します。

于 2010-02-18T10:34:10.413 に答える
0

この動作に関するMicrosoftからの回答

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 に答える