skynergy
asked on
Save Unicode string to IniFile
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(filen ame);
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!
--------------------------
procedure TForm1.Button1Click(Sender
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
finally
enc.Free;
sl.Free;
end;
ini:=TIniFile.Create(filen
ini.WriteString('mysection
t := ini.ReadString('mysection'
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!
ASKER
Thank you for your feedback.
Do you have any information on how to encode the string using Base64 as you suggested?
Do you have any information on how to encode the string using Base64 as you suggested?
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(lpAppNam e, lpKeyName, lpDefault: PWideChar;
lpReturnedString: PWideChar; nSize: DWORD): DWORD; stdcall;
function WriteProfileStringW(lpAppN ame, lpKeyName, lpString: PWideChar): BOOL; stdcall;
function GetPrivateProfileStringW(l pAppName, 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 ('mysectio n', 'myvar', MyWideString, 'inifile.ini');
Where MyWideString is your variable of type WideString.
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(lpAppNam
lpReturnedString: PWideChar; nSize: DWORD): DWORD; stdcall;
function WriteProfileStringW(lpAppN
function GetPrivateProfileStringW(l
lpReturnedString: PWideChar; nSize: DWORD; lpFileName: PWideChar): DWORD; stdcall;
function WritePrivateProfileStringW
ini.WriteString('mysection
Would be translated to
WritePrivateProfileStringW
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.
OMG!! Welcome to EE, Dr. Bob. I've been a fan of yours for some time.
ASKER
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(P WideChar(S ect), 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.
This is my code:
procedure TForm1.Button1Click(Sender
var
SectVar, Sect, MyWideString: WideString;
begin
if FileExists('c:\inifile.ini
DeleteFile('c:\inifile.ini
MyWideString := TntMemo1.Text;
Sect := 'MySection';
SectVar := 'MyVar';
WritePrivateProfileStringW
end;
procedure TForm1.Button2Click(Sender
var
SectVar, Sect, MyWideString: WideString;
begin
MyWideString := TntMemo1.Text;
Sect := 'MySection';
SectVar := 'MyVar';
GetPrivateProfileStringW(P
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is your PC language setting compatible with the unicode string?
ASKER
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?
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?
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)...
ASKER
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.
Thank you for all your help. I guess I will have to spend the money and just buy the components.
I guess the safest way to persist a unicode string is to encode it, probably with Base64, although you could use any encoding algorithm.