From a67b55f7fe675310d9336089ca8bd0f89e7bfd70 Mon Sep 17 00:00:00 2001 From: ondrej Date: Thu, 10 Dec 2020 17:04:12 +0000 Subject: [PATCH] win32: SysErrorMessage: ignore inserts (when not used the messages with inserts are empty) and allocate message buffer git-svn-id: trunk@47748 - (cherry picked from commit 6eb37beb2420409c9e113b06fb651c9cb91c7e72) --- rtl/win/sysutils.pp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/rtl/win/sysutils.pp b/rtl/win/sysutils.pp index f58a796d15..86626c19df 100644 --- a/rtl/win/sysutils.pp +++ b/rtl/win/sysutils.pp @@ -1406,25 +1406,26 @@ end; ****************************************************************************} function SysErrorMessage(ErrorCode: Integer): String; -const - MaxMsgSize = Format_Message_Max_Width_Mask; var - MsgBuffer: unicodestring; + MsgBuffer: PWideChar; + Msg: UnicodeString; len: longint; begin - SetLength(MsgBuffer, MaxMsgSize); - len := FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, + len := FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM or + FORMAT_MESSAGE_IGNORE_INSERTS or + FORMAT_MESSAGE_ALLOCATE_BUFFER, nil, ErrorCode, MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT), - PUnicodeChar(MsgBuffer), - MaxMsgSize, + PWideChar(@MsgBuffer), + 0, nil); // Remove trailing #13#10 - if (len > 1) and (MsgBuffer[len - 1] = #13) and (MsgBuffer[len] = #10) then + if (len > 1) and (MsgBuffer[len - 2] = #13) and (MsgBuffer[len - 1] = #10) then Dec(len, 2); - SetLength(MsgBuffer, len); - Result := MsgBuffer; + SetString(Msg, PUnicodeChar(MsgBuffer), len); + LocalFree(HLOCAL(MsgBuffer)); + Result := Msg; end; {****************************************************************************