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 6eb37beb24)
This commit is contained in:
ondrej 2020-12-10 17:04:12 +00:00 committed by marcoonthegit
parent 0018e3a51f
commit a67b55f7fe

View File

@ -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;
{****************************************************************************