Add wxMSWFormatMessage() and use it from other places

Don't duplicate calls to ::FormatMessage(), which is difficult to use
correctly, in wxCrashReport and wxWebRequestWinHTTP, but just reuse the
same code that was already present in wxSysErrorMsgStr() after
refactoring it into a reusable function allowing to specify the module
name to use for the error code lookup (before falling back to
interpreting it as system error code).

This fixes not trimming the trailing "\r\n" from the string in the other
places (wxWinHTTPErrorToString() had code to do it, but it was wrong,
while wxCrashContext::GetExceptionString() didn't do it at all) and
avoids duplication.
This commit is contained in:
Vadim Zeitlin
2021-01-09 17:41:49 +01:00
parent 2869e1ccd6
commit 5d256988be
4 changed files with 31 additions and 48 deletions

View File

@@ -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);
}