Domanda

Ho un'applicazione che interagisce con il server. Se il server è giù, allora allora mi metterò ERROR_WINHTTP_CANNOT_CONNECT Sto usando getLastError() API per ottenere questo codice di errore, io sono la manipolazione di questo codice di errore per visualizzare i messaggi di errore corretti agli utenti. Questo programma funziona bene in Windows 2003. Quando ho provato con Windows7 non ricevo alcun errore, getLastError() API restituisce 0 ogni volta, anche se si è verificato un errore. Sto usando C ++ come del linguaggio di programmazione è interessato.

Grazie in anticipo

Santhu

È stato utile?

Soluzione 2

Ho osservato un comportamento diverso di GetLastError API in Windows 2003 e in Windows 7. Qui di seguito sono i miei dati di osservazione

in Windows 2003:

Esempio di codice:

WinHttpOpen () - Completa con successo

Winhttpconnect() - Questa API è riuscita a causa di alcuni motivi, ad esempio il codice di errore 12029

GetLastErrorCode() - Restituisce il codice di errore 12029 come previsto

WinHttpCloseHandle(hOpen); - maniglia di chiusura per HttpOpen, completa successfilly

GetLastErrorCode() - Restituisce il codice di errore 12029

In Windows 7

Esempio di codice:

WinHttpOpen () - Completa con successo

Winhttpconnect() - Questa API è riuscita a causa di alcuni motivi, ad esempio il codice di errore 12029

GetLastErrorCode() - Restituisce il codice di errore 12029 come previsto

WinHttpCloseHandle(hOpen); - Chiusura maniglia per HttpOpen, completa con successo

GetLastErrorCode() - Restituisce il codice di errore 0 // vedere la differenza con l'esempio Windows 2003, sulle finestre 2003 questa API restituisce ultimo errore che è 1209

Altri suggerimenti

Se si effettua tutte le chiamate API di Windows tra il fallimento e la volta che si chiama GetLastError (), il codice di errore può ottenere resettare a 0 quando quella chiamata API ha esito positivo.

È necessario chiamare GetLastError () subito dopo il fallimento, e salvare il valore piuttosto che cercare di aspettare e chiamare GetLastError () in seguito.

risposta da Microsoft su questo comportamento

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. 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top