Browse Source

Bug 1439: When the current console font (e.g. raster) does not support Unicode, some console output might be lost

https://winscp.net/tracker/1439

Source commit: 24a52fabd74590e1aad7b62f18108d5ca256bc4a
Martin Prikryl 9 years ago
parent
commit
d654a79359
1 changed files with 19 additions and 1 deletions
  1. 19 1
      source/console/Main.cpp

+ 19 - 1
source/console/Main.cpp

@@ -401,6 +401,7 @@ void Print(const wchar_t* Message)
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 void Print(bool FromBeginning, const wchar_t* Message)
 void Print(bool FromBeginning, const wchar_t* Message)
 {
 {
+  size_t Len = wcslen(Message);
   if ((OutputType == FILE_TYPE_DISK) || (OutputType == FILE_TYPE_PIPE))
   if ((OutputType == FILE_TYPE_DISK) || (OutputType == FILE_TYPE_PIPE))
   {
   {
     if (FromBeginning && (Message[0] != L'\n'))
     if (FromBeginning && (Message[0] != L'\n'))
@@ -434,7 +435,24 @@ void Print(bool FromBeginning, const wchar_t* Message)
     {
     {
       WriteConsole(ConsoleOutput, L"\r", 1, &Written, NULL);
       WriteConsole(ConsoleOutput, L"\r", 1, &Written, NULL);
     }
     }
-    WriteConsole(ConsoleOutput, Message, wcslen(Message), &Written, NULL);
+    bool WriteResult =
+      WriteConsole(ConsoleOutput, Message, Len, &Written, NULL);
+    int Error = GetLastError();
+    // The current console font does not support some characters in the message,
+    // fall back to ansi-writting
+    if (!WriteResult && (Error == ERROR_GEN_FAILURE))
+    {
+      int Size = WideCharToMultiByte(CP_ACP, 0, Message, -1, 0, 0, 0, 0);
+      if (Size > 0)
+      {
+        char* Buffer = new char[Size];
+        if (WideCharToMultiByte(CP_ACP, 0, Message, -1, Buffer, Size, 0, 0) > 0)
+        {
+          WriteConsoleA(ConsoleOutput, Buffer, strlen(Buffer), &Written, NULL);
+        }
+        delete[] Buffer;
+      }
+    }
   }
   }
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------