Solved

Save Unicode string to IniFile

Posted on 2010-05-03
10
5,015 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 45

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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 45

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 50 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 45

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

696 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