We help IT Professionals succeed at work.

Save Unicode string to IniFile

skynergy
skynergy asked
on
I found this code to make sure that you save your ini file as a unicode file then when you write to it, it will write the unicode strings.

------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
  ini:TIniFile; t,filename:widestring;
  sl : tStrings;
  Enc : tEncoding;
begin

  filename:='test';
  sl := tStringList.Create;
  sl.Clear;
  Enc := TUnicodeEncoding.Create;
try
  sl.SaveToFile(filename,Enc.Unicode);
finally
  enc.Free;
  sl.Free;
end;

  ini:=TIniFile.Create(filename);
  ini.WriteString('mysection', 'myvar', '¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿¿¿ ');
  t := ini.ReadString('mysection', 'myvar', 'default');
  ini.Free;
  showmessage(t);

end;
------------------------------------------------------------

I am using Delphi 7 and get this message "Undeclared identifier: 'tEncoding'" I did include SystUtils under uses.

Can anyone please help me to either compile this code or give me an alternative way to save Unicode strings to an IniFile.

Thank you in advance!
Comment
Watch Question

Top Expert 2014

Commented:
The tEncoding class was introduced in D2009, so it isn't in your D7 VCL/RTL.

I guess the safest way to persist a unicode string is to encode it, probably with Base64, although you could use any encoding algorithm.

Author

Commented:
Thank you for your feedback.

Do you have any information on how to encode the string using Base64 as you suggested?

Commented:
You do not have to encode it. You can use WideString in Delphi 7 to contain the Unicode data and text.

The only thing you need to change is the way to write to the .ini files. The TIniFile class works with Ansi data in Delphi 7, but the Windows API offers both the ANSI and the Unicode (W) versions of the .ini file handling. You just need to call the "W"-versions of the API directly.

The following W-functions are available from Windows.pas that you can call.

function GetProfileStringW(lpAppName, lpKeyName, lpDefault: PWideChar;
  lpReturnedString: PWideChar; nSize: DWORD): DWORD; stdcall;
function WriteProfileStringW(lpAppName, lpKeyName, lpString: PWideChar): BOOL; stdcall;

function GetPrivateProfileStringW(lpAppName, lpKeyName, lpDefault: PWideChar;
  lpReturnedString: PWideChar; nSize: DWORD; lpFileName: PWideChar): DWORD; stdcall;
function WritePrivateProfileStringW(lpAppName, lpKeyName, lpString, lpFileName: PWideChar): BOOL; stdcall;

ini.WriteString('mysection', 'myvar', '¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿¿¿ ');

Would be translated to

WritePrivateProfileStringW('mysection', 'myvar', MyWideString, 'inifile.ini');

Where MyWideString is your variable of type WideString.
program Ini;
uses
  Windows;
var
  MyWideString: WideString;
begin
  MyWideString := '¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿¿¿';
  WritePrivateProfileStringW('mysection', 'myvar', PWideChar(MyWideString), 'inifile.ini');
end.

Open in new window

Top Expert 2014

Commented:
OMG!!  Welcome to EE, Dr. Bob.  I've been a fan of yours for some time.

Author

Commented:
Thank you for your answer ebob42. I really appreciate your help.

This is my code:
procedure TForm1.Button1Click(Sender: TObject);
var
  SectVar, Sect, MyWideString: WideString;
begin
  if FileExists('c:\inifile.ini') then
    DeleteFile('c:\inifile.ini');

  MyWideString := TntMemo1.Text;
  Sect := 'MySection';
  SectVar := 'MyVar';
  WritePrivateProfileStringW(PWideChar(Sect), PWideChar(SectVar), PWideChar(MyWideString), 'c:\inifile.ini');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  SectVar, Sect, MyWideString: WideString;
begin
  MyWideString := TntMemo1.Text;
  Sect := 'MySection';
  SectVar := 'MyVar';
  GetPrivateProfileStringW(PWideChar(Sect), PWideChar(SectVar), '', PWideChar(MyWideString), 1000, 'c:\inifile.ini');
  TntMemo2.Text := MyWideString;
end;

When I paste the Unicode string:  ¿¿¿¿: and click on Button1, it writes it to the IniFile. When I click on Button2 to read it back into TntMemo2, the it returns with ????, so either the writing or the reading is not correct. Also if I open the INI file in a text editor it show the string value as ????.

Am I doing something wrong?

TNTMemo1 and 2 are both UniCode controls. So visually I can see that the pasted Unicode string in TNTMemo1 is correct.
Commented:
I'm not sure where your code fails - it looks like you're reading and writing WideString values...

If you have the TNT controls, then there should also be a TTNTInifile control in there to solve your issue. Look for the unit TntIniFiles.pas.
Top Expert 2014

Commented:
Is your PC language setting compatible with the unicode string?

Author

Commented:
Thanks for the replies.

The TntIniFiles.pas is not with the free set of components I have from the TNT controls. I know that the TMS Unicode controls have this file included but I obviously will have to pay for them. I will try theirs and if it works I might as well buy the components. Thanks for the tip ebob42.

Aikimark, my XP development PC doesn't have the language setup but if I copy the compiled test.exe file to my Windows 7 PC which does support Unicode strings it still doesn't work unless the PC on which you compile the EXE must have the language set to support Unicode?

Commented:
I still have a free version of the TNT controls. I'm not sure if they can be distrbuted for free anymore (since were taken over by TMS)...

Author

Commented:
The TTNTIniFiles from TMS worked!

Thank you for all your help. I guess I will have to spend the money and just buy the components.