Question

I have a problem with displaying Japanese Character , specifically Unicode character "5c" in my delphi application . I need to save the application names into the registry and then display it in some kind of popup.

I have narrowed down the problem to this code specifically :-

  Var
   Str : WideString;
   Str2: WideString;
   Str3 : WideString; 
 TntEdit5.Text := TntOpenDialog1.FileName; //correctly displayed

 Str3 := TntEdit5.Text;

 ShowMessage('Original =' + Str3);

 Str := UTF8Encode(TntEdit5.Text) ;
 ShowMessage('UTF8Encode =' + Str3);


 Str2 := UTF8Decode(Str) ;
 ShowMessage('UTF8Decode =' + Str3);
 end;

I dont get the correct name in Str, Str2 and Str3 . So how to fetch the name in a string ? I dont want to display the text but i want to use it to save to registry and other functions.

Instead of SHowMessage, I used MessageBoxW(Form1.Handle, PWChar( Str3 ), 'Path', MB_OK ); which gave me correct result.

But I want to use this string internally, like write the string into a file etc. How to do that ?

Thanks In Advance

Was it helpful?

Solution

The type of Str does not match the type of result of UTF8Encode - so the line Str := UTF8Encode damages data. Instead of Str you should declare and use variable with a datatype mathcing the one of Utf8Encode result.

Same is true for Str2 := UTF8Decode(Str) line with regard to wrong data type of Str parameter the. It should be replaced with another var of proper datatype.

Str3 is not declared, so the code won't even compile. Add the Str3: WideString; line.


ShowMessage does not work with UTF-16, so then you make your own popup function that does.

Make your own dialog containing Tnt unicode-aware Label to display the text. And your new ShowMessage-like function would set the label's caption and then display that dialog instead of stock unicode-unaware one.

You may look at http://blog.synopse.info/post/2011/03/05/Open-Source-SynTaskDialog-unit-for-XP%2CVista%2CSeven for exampel of such dialogs, but i don't know if they are UTF-16 aware on D7.

Another option is searching TnT Sources for a ready-made unicode-aware function like ShowMessage - there may be one, or not.

Yet another option is using Win32 API directly, namely the MessageBoxW function working with PWideChar variables for texts: see http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505.aspx

@DavidHeffernan MessageBoxW needs a lot of boilerplate both due to using C-Strings and for giving too much flexibility. It may be considered kinda good replacement for MessageDlg but not so much for ShowMessage. Then i am sure that TnT has ShowMessage conversion and that implementing own dialog would be good for both application look-and-feel and topic-starter experience.

You may also switch from obsolete Delphi 7 to modern CodeTyphon that uses UTF-8 for strings out of the box. You should at very least give it a try.


To read and write WideString from registry using Delphi 7 RTL you can make two easy options:

  1. Convert WideString to UTF8 AnsiString and save it via TRegistry.WriteString and do back conversion on reading.
  2. Save WideString as binary data: Cardinal(Length) followed by array of WideChar using TRegistry.WriteBinaryData
  3. You can also use function RegReadWideString(const RootKey: DelphiHKEY; const Key, Name: string): WideString; and RegWriteWideString courtesy of http://jcl.sf.net

Whatever approach you'd choose - you have to do your own class on top of TRegistry that would be uniformly implementing those new TYourNewRegistry.WriteWideString and TYourNewRegistry.ReadWideString methods, so that the string written would always be read back using the same method.

However, since you already got TNT installed - then look carefully inside,. there just should be ready-made unicode-aware class like TTntRegistry or something like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top