summaryrefslogtreecommitdiff
path: root/rtl/win
diff options
context:
space:
mode:
authoryury <yury@3ad0048d-3df7-0310-abae-a5850022a9f2>2016-07-29 10:23:12 +0000
committeryury <yury@3ad0048d-3df7-0310-abae-a5850022a9f2>2016-07-29 10:23:12 +0000
commita8020038dab163075a45db7397146d6acafd9685 (patch)
tree17a19fe4998349c1a8959e87b24978a5f7ed3028 /rtl/win
parent41d595916116e0737313771c6349dbf01c9dba5e (diff)
downloadfpc-a8020038dab163075a45db7397146d6acafd9685.tar.gz
* Remove trailing #13#10 in the result of SysErrorMessage() on Windows and minor optimization. It is Delphi compatible.
git-svn-id: http://svn.freepascal.org/svn/fpc/trunk@34220 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'rtl/win')
-rw-r--r--rtl/win/sysutils.pp29
1 files changed, 16 insertions, 13 deletions
diff --git a/rtl/win/sysutils.pp b/rtl/win/sysutils.pp
index ea4598dbe0..5eda62b4e9 100644
--- a/rtl/win/sysutils.pp
+++ b/rtl/win/sysutils.pp
@@ -978,19 +978,22 @@ function SysErrorMessage(ErrorCode: Integer): String;
const
MaxMsgSize = Format_Message_Max_Width_Mask;
var
- MsgBuffer: PUnicodeChar;
-begin
- GetMem(MsgBuffer, MaxMsgSize*2);
- FillChar(MsgBuffer^, MaxMsgSize*2, #0);
- FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
- nil,
- ErrorCode,
- MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
- MsgBuffer,
- MaxMsgSize,
- nil);
- SysErrorMessage := MsgBuffer;
- FreeMem(MsgBuffer, MaxMsgSize*2);
+ MsgBuffer: unicodestring;
+ len: longint;
+begin
+ SetLength(MsgBuffer, MaxMsgSize);
+ len := FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
+ nil,
+ ErrorCode,
+ MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ PUnicodeChar(MsgBuffer),
+ MaxMsgSize,
+ nil);
+ // Remove trailing #13#10
+ if (len > 1) and (MsgBuffer[len - 1] = #13) and (MsgBuffer[len] = #10) then
+ Dec(len, 2);
+ SetLength(MsgBuffer, len);
+ Result := MsgBuffer;
end;
{****************************************************************************