Browse Source

* copy syserrorformat trunk version to fixes. (manual merge)

git-svn-id: branches/fixes_3_0@36291 -
marco 8 years ago
parent
commit
406fb857d9
1 changed files with 17 additions and 13 deletions
  1. 17 13
      rtl/win/sysutils.pp

+ 17 - 13
rtl/win/sysutils.pp

@@ -974,23 +974,27 @@ end;
                            Target Dependent
                            Target Dependent
 ****************************************************************************}
 ****************************************************************************}
 
 
+
 function SysErrorMessage(ErrorCode: Integer): String;
 function SysErrorMessage(ErrorCode: Integer): String;
 const
 const
   MaxMsgSize = Format_Message_Max_Width_Mask;
   MaxMsgSize = Format_Message_Max_Width_Mask;
 var
 var
-  MsgBuffer: pChar;
-begin
-  GetMem(MsgBuffer, MaxMsgSize);
-  FillChar(MsgBuffer^, MaxMsgSize, #0);
-  FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
-                 nil,
-                 ErrorCode,
-                 MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                 MsgBuffer,                 { This function allocs the memory }
-                 MaxMsgSize,                           { Maximum message size }
-                 nil);
-  SysErrorMessage := MsgBuffer;
-  FreeMem(MsgBuffer, MaxMsgSize);
+  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;
 
 
 {****************************************************************************
 {****************************************************************************