diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index c02ee410a2..cd49453c58 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -972,6 +972,10 @@ enum wxWinVersion WXDLLIMPEXP_BASE wxWinVersion wxGetWinVersion(); +// This is similar to wxSysErrorMsgStr(), but takes an extra HMODULE parameter +// specific to wxMSW. +WXDLLIMPEXP_BASE wxString wxMSWFormatMessage(DWORD nErrCode, HMODULE hModule = 0); + #if wxUSE_GUI && defined(__WXMSW__) // cursor stuff diff --git a/src/common/log.cpp b/src/common/log.cpp index 8c99a88277..f56c912dc7 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -1056,19 +1056,22 @@ unsigned long wxSysErrorCode() #endif //Win/Unix } -wxString wxSysErrorMsgStr(unsigned long nErrCode) -{ - if ( nErrCode == 0 ) - nErrCode = wxSysErrorCode(); - #if defined(__WINDOWS__) + +wxString wxMSWFormatMessage(DWORD nErrCode, HMODULE hModule) +{ + DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS; + if ( hModule ) + flags |= FORMAT_MESSAGE_FROM_HMODULE; + // get error message from system LPVOID lpMsgBuf; if ( ::FormatMessage ( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, + flags, + hModule, nErrCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, @@ -1103,6 +1106,17 @@ wxString wxSysErrorMsgStr(unsigned long nErrCode) } return str; +} + +#endif // __WINDOWS__ + +wxString wxSysErrorMsgStr(unsigned long nErrCode) +{ + if ( nErrCode == 0 ) + nErrCode = wxSysErrorCode(); + +#if defined(__WINDOWS__) + return wxMSWFormatMessage(nErrCode); #else // !__WINDOWS__ char buffer[1024]; char *errorMsg = buffer; diff --git a/src/msw/crashrpt.cpp b/src/msw/crashrpt.cpp index 437f6634b2..02b23db2d3 100644 --- a/src/msw/crashrpt.cpp +++ b/src/msw/crashrpt.cpp @@ -28,6 +28,7 @@ #include "wx/msw/debughlp.h" #include "wx/msw/crashrpt.h" +#include "wx/msw/private.h" // ---------------------------------------------------------------------------- // classes @@ -356,20 +357,7 @@ wxString wxCrashContext::GetExceptionString() const default: // unknown exception, ask NTDLL for the name - if ( !::FormatMessage - ( - FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_FROM_HMODULE, - ::GetModuleHandle(wxT("NTDLL.DLL")), - code, - 0, - wxStringBuffer(s, 1024), - 1024, - 0 - ) ) - { - s.Printf(wxT("UNKNOWN_EXCEPTION(%d)"), code); - } + s = wxMSWFormatMessage(code, ::GetModuleHandle(wxT("NTDLL.DLL"))); } #undef CASE_EXCEPTION diff --git a/src/msw/webrequest_winhttp.cpp b/src/msw/webrequest_winhttp.cpp index a56ce2c585..bba759a2b7 100644 --- a/src/msw/webrequest_winhttp.cpp +++ b/src/msw/webrequest_winhttp.cpp @@ -16,6 +16,7 @@ #include "wx/mstream.h" #include "wx/uri.h" +#include "wx/msw/private.h" #include "wx/msw/private/webrequest_winhttp.h" #ifndef WX_PRECOMP @@ -59,31 +60,6 @@ // Helper functions -static wxString wxWinHTTPErrorToString(DWORD errorCode) -{ - wxString errorString; - - LPVOID msgBuf; - if ( FormatMessageW( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_FROM_HMODULE, - GetModuleHandle(TEXT("WINHTTP")), - errorCode, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&msgBuf, - 0, NULL) ) - { - errorString.assign((LPWSTR)msgBuf); - LocalFree(msgBuf); - // Truncate trailing \n\r - if ( errorString.size() > 2 ) - errorString.resize(errorString.size()); - } - return errorString; -} - static wxString wxWinHTTPQueryHeaderString(HINTERNET hRequest, DWORD dwInfoLevel, LPCWSTR pwszName = WINHTTP_HEADER_NAME_BY_INDEX) { @@ -254,7 +230,8 @@ void wxWebRequestWinHTTP::CreateResponse() void wxWebRequestWinHTTP::SetFailed(DWORD errorCode) { - wxString failMessage = wxWinHTTPErrorToString(errorCode); + wxString failMessage = wxMSWFormatMessage(errorCode, + GetModuleHandle(TEXT("WINHTTP"))); SetState(wxWebRequest::State_Failed, failMessage); }