Unicode Console Application in Delphi 2009

后端 未结 6 1374
不知归路
不知归路 2020-12-10 14:19

How can I create unicode console application with Delphi 2009?

If I do like this:

{$APPTYPE CONSOLE}
uses
  SysUtils;
begin
  writeln(\'öüğşç سيمانتت         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 15:01

    Actually, there is a way to do this with standard WriteLn() calls, but it involves patching a bug in the Delphi 2009 RTL. The thing is, Delphi does some compiler magic for WriteLn. For UnicodeString arguments, this results in a call to _WriteUString. That method can be seen in System.pas, although you can't call it directly. In it you'll see a call to _WriteLString, but that method receives a AnsiString argument. So when this call is taking place, your UnicodeString is being downcasted to AnsiString.

    The solution is, to change this UnicodeString->AnsiString cast into a UnicodeString->UTF8String cast.

    Now, when you set the console to UTF8, all your characters will go through untouched (and yes, ofcourse you'll need a font with support for the characters you want to show) :

    SetConsoleOutputCP(CP_UTF8)
    

    For this RTL fix, you'll need to do some nifty code-hooking. I've done this already, and a collegue of mine is busy writing an article about this. I'll post a link once it's available online.

    Cheers!

提交回复
热议问题