Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Save Unicode string to IniFile

Posted on 2010-05-03
10
Medium Priority
?
5,193 Views
Last Modified: 2012-08-14
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!
0
Comment
Question by:skynergy
  • 4
  • 3
  • 3
10 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 32628611
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.
0
 

Author Comment

by:skynergy
ID: 32628824
Thank you for your feedback.

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

Expert Comment

by:ebob42
ID: 32629684
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:aikimark
ID: 32632029
OMG!!  Welcome to EE, Dr. Bob.  I've been a fan of yours for some time.
0
 

Author Comment

by:skynergy
ID: 32637768
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.
0
 
LVL 7

Accepted Solution

by:
ebob42 earned 200 total points
ID: 32640421
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.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 32642134
Is your PC language setting compatible with the unicode string?
0
 

Author Comment

by:skynergy
ID: 32647166
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?
0
 
LVL 7

Expert Comment

by:ebob42
ID: 32647552
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)...
0
 

Author Closing Comment

by:skynergy
ID: 32648556
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question