2010-02-17 6 views
0

J'ai une application qui interagit avec le serveur. Si le serveur est en panne alors je vais obtenir ERROR_WINHTTP_CANNOT_CONNECT J'utilise getLastError() API pour obtenir ce code d'erreur, je gère ce code d'erreur pour afficher les messages d'erreur appropriés pour les utilisateurs. Ce programme fonctionne très bien dans Windows 2003. Lorsque j'ai essayé avec Windows7 je ne reçois aucune erreur, getLastError() API retourne 0 à chaque fois même si une erreur est survenue. J'utilise C++ en ce qui concerne le langage de programmation.Problème avec l'API GetLastError sur Windows 7

Merci à l'avance

Santhu

Répondre

0

J'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() - Remplit succès

Winhttpconnect() - Cette API a échoué pour certaines raisons, disons code d'erreur 1202 9

GetLastErrorCode() - Retourne le code d'erreur 12029 comme prévu

WinHttpCloseHandle(hOpen); - Poignée de clôture pour HttpOpen, complète successfilly

GetLastErrorCode() - Retourne le code d'erreur 12029

Dans Windows 7

Exemple de code:

WinHttpOpen() - finalise avec succès

Winhttpconnect() - Cette API est en panne pour certaines raisons, disons 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, sur Windows 2003 cette API retourne la dernière erreur qui est 1209

0

Si vous effectuez 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 enregistrer cette valeur plutôt que d'essayer d'attendre et d'appeler GetLastError() plus tard.

0

réponse de Microsoft sur ce Coportement

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.