Problème avec l'API GetLastError sur Windows 7
-
21-09-2019 - |
Question
J'ai une application qui interagit avec le serveur. Si le serveur est en panne alors je vais me ERROR_WINHTTP_CANNOT_CONNECT
J'utilise API getLastError()
pour obtenir ce code d'erreur, je traitais ce code d'erreur d 'afficher des messages d'erreur aux utilisateurs. Ce programme fonctionne très bien dans Windows 2003. Lorsque je l'ai essayé avec Windows7 je ne reçois aucune erreur, API renvoie 0 getLastError()
à chaque fois, même si l'erreur est produite. J'utilise C ++ comme langage de programmation est concerné.
Merci à l'avance
Santhu
La solution 2
Je l'ai observé un comportement différent de l'API GetLastError dans Windows 2003 et Windows 7. Voici les détails de mon observation
Dans Windows 2003:
Exemple de code:
WinHttpOpen ()
- se termine avec succès
Winhttpconnect()
- Cette API est échoué en raison de certaines raisons, par exemple le code d'erreur 12029
GetLastErrorCode()
- Retourne le code d'erreur 12029 comme prévu
WinHttpCloseHandle(hOpen)
; - Poignée de fermeture pour HttpOpen, complète successfilly
GetLastErrorCode()
- Retourne le code d'erreur 12029
Dans Windows 7
Exemple de code:
WinHttpOpen ()
- se termine avec succès
Winhttpconnect()
- Cette API est échoué en raison de certaines raisons, par exemple le code d'erreur 12029
GetLastErrorCode()
- Retourne le code d'erreur 12029 comme prévu
WinHttpCloseHandle(hOpen);
- Poignée de fermeture pour HttpOpen, complète avec succès
GetLastErrorCode()
- Retourne le code d'erreur 0 // voir la différence avec l'exemple Windows 2003, Windows 2003 sur cette API renvoie la dernière erreur qui est 1209
Autres conseils
Si vous faites des appels API Windows entre l'échec et le temps que vous appelez GetLastError (), le code d'erreur peut se réinitialiser à 0 lorsque cet appel API réussit.
Vous devez appeler GetLastError () immédiatement après l'échec, et enregistrez cette valeur plutôt que d'essayer d'attendre et appeler GetLastError () plus tard.
Réponse de Microsoft sur ce comportement
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.