views:

115

answers:

3

I've console app. written in Delphi 2010. It's output is Unicode supported. (I used UTF8Encode and SetConsoleOutputCP(CP_UTF8) for this). When I run the program from command prompt it works fine.
Now I want to read the output from another program which was created in Delphi 5. I use this method. But I've problems with unicode characters.
Does anyone have a recommendation to read the unicode output of console app. from Delphi 5?

A: 

You have two problems using Delphi 5 with unicode output.

The first is TMemo does not support Unicode characters you will need to find another control, such as the ones in TMS Unicode Component Pack. However, this Component pack does not support Delphi 5.

The second problem is with this part of the code:

  repeat
    BytesRead := 0;
    ReadFile(ReadPipe,Buffer[0],
    ReadBuffer,BytesRead,nil) ;
    Buffer[BytesRead]:= #0;
    OemToAnsi(Buffer,Buffer) ;
    AMemo.Text := AMemo.text + String(Buffer) ;
  until (BytesRead < ReadBuffer) ;

It is reading he characters and placing them into buffer which is a PCHAR (single character per byte in D5) Then type casting this to a String which is an AnsiString in D5.

Although I have not used D5 for years, the only type that I can remember that can handle unicode data in D5 is WideString.

Robert Love
I still work with D5 on and off. It indeed does have WideString/Unicode support. However, WideStrings are UTF-16 encoded, UTF-16-LE to be more precise. Haven't found any native UTF-8 support in D5. Searching the D5 help doesn't bring up anything, nor does a search of the D5 sources.
Marjan Venema
@Marjan: UTF-16-LE to UTF-8 conversion and vice versa is no big problem, even when it's not part of the D5 RTL. The dxgettext sources for example have UTF-8 routines working with D5.
mghie
+1  A: 

Delphi 5 does have unicode support, but only through WideStrings which are UTF-16(-LE) encoded. Natively, D5 does not have UTF-8 support.

You can read the output of your D2010 console app in the way you already do, although I would take out the OemToAnsi conversion. OEMToAnsi was superseded (even in D5 days) by OEMToChar which can be used to convert OEM characters to Ansi (single byte characters using various code pages) or WideString (UTF-16-LE Unicode), but it won't do a thing to interpret the UTF-8 bytes coming in and might just mess things up.

What you need is a set of functions that can take all the "raw" utf-8 bytes you have read from the pipe and convert them to (UTF-16-LE encoded) WideStrings which you can then feed to a control that can take in and show WideStrings. Alternatively you could look for a control that does the "raw" byte interpretation and conversion all itself, but I must admit I haven't seen any let alone one that still supports D5.

A library that can convert many different encodings and still supports D5 is DIUnicode: http://www.wikitaxi.org/delphi/doku.php/products/unicode/index

Marjan Venema
A: 

I've changed somethings as follows and it works fine :
In console application, I didn't use SetConsoleOutputCP(CP_UTF8). Only use string output...
And at the other program (Delphi 5), I use this function without use OemToChar(Buffer,Buffer)

SimaWB
-1, The question asks how to read unicode data with D5. This answer does not involve unicode at all.
Sertac Akyuz