Browse Source

* Remove trailing #13#10 in the result of SysErrorMessage() on Windows and minor optimization. It is Delphi compatible.

git-svn-id: trunk@34220 -
yury 9 years ago
parent
commit
e3ebaa6e6a
1 changed files with 16 additions and 13 deletions
  1. 16 13
      rtl/win/sysutils.pp

+ 16 - 13
rtl/win/sysutils.pp

@@ -978,19 +978,22 @@ function SysErrorMessage(ErrorCode: Integer): String;
 const
 const
   MaxMsgSize = Format_Message_Max_Width_Mask;
   MaxMsgSize = Format_Message_Max_Width_Mask;
 var
 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;
 end;
 
 
 {****************************************************************************
 {****************************************************************************