win32: SysErrorMessage: ignore inserts (when not used the messages with inserts are empty) and allocate message buffer

git-svn-id: trunk@47748 -
This commit is contained in:
ondrej 2020-12-10 17:04:12 +00:00
parent 1efeb5851a
commit 6eb37beb24

View File

@ -1317,25 +1317,26 @@ end;
****************************************************************************} ****************************************************************************}
function SysErrorMessage(ErrorCode: Integer): String; function SysErrorMessage(ErrorCode: Integer): String;
const
MaxMsgSize = Format_Message_Max_Width_Mask;
var var
MsgBuffer: unicodestring; MsgBuffer: PWideChar;
Msg: UnicodeString;
len: longint; len: longint;
begin begin
SetLength(MsgBuffer, MaxMsgSize); len := FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM or
len := FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS or
FORMAT_MESSAGE_ALLOCATE_BUFFER,
nil, nil,
ErrorCode, ErrorCode,
MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT), MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
PUnicodeChar(MsgBuffer), PWideChar(@MsgBuffer),
MaxMsgSize, 0,
nil); nil);
// Remove trailing #13#10 // 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); Dec(len, 2);
SetLength(MsgBuffer, len); SetString(Msg, PUnicodeChar(MsgBuffer), len);
Result := MsgBuffer; LocalFree(HLOCAL(MsgBuffer));
Result := Msg;
end; end;
{**************************************************************************** {****************************************************************************