How can I create unicode console application with Delphi 2009?
If I do like this:
{$APPTYPE CONSOLE}
uses
SysUtils;
begin
writeln('öüğşç سيمانتت');
end.
I get the following:
ougsc ???????
How can I create unicode console application with Delphi 2009?
If I do like this:
{$APPTYPE CONSOLE}
uses
SysUtils;
begin
writeln('öüğşç سيمانتت');
end.
I get the following:
ougsc ???????
You can't (At least not with the standard library). The console functions are the only non Unicode functions in Delphi 2009.
Windows console cannot display unicode charactes, so try to send output to file with:
my_app.exe > unicode_file.txt
and try viewing unicode_file.txt with good text editor.
I'm not sure that is what you're after, but you can create Unicode console applications in all 32-bit Delphi versions by using the Windows API functions. I just tried with Delphi 4:
program test;
{$APPTYPE CONSOLE}
uses
Windows;
var
s: WideString;
i: integer;
Written: Cardinal;
begin
SetLength(s, 80);
for i := 1 to 80 do
s[i] := WideChar(48 + i);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), PWideChar(s), 80, Written,
nil);
end.
I don't have Delphi 2009 to try how entering Unicode strings in the editor works...
@Kabrol: With the raster font set I don't see either of the chars, but with Lucida Console I see at least widechar($11f) - "ğ".
Please see "SetConsoleOutputCP Only Effective with Unicode Fonts" and in general the description of console API functions at "Console Reference".
Writeln in Delphi 2009 still uses ANSI (see System TTextRec) but you can use UTF8Encode and change the console's output code page to UTF8 by calling SetConsoleOutputCP(CP_UTF8). You will also need a good font to actually display Unicode characters.
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!