Recent

Author Topic: TRegistry and cross platform  (Read 13169 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
TRegistry and cross platform
« on: March 22, 2010, 04:44:41 pm »
In this 3d was told that TRegistry is cross platform. But I wonder how this is possible, that I used on windows TRegistry correctly write and read from HKEY_CURRENT_USER
in \Software\name_software\ to do this with TRegistry in ubuntu how?

http://www.lazarus.freepascal.org/index.php/topic,3633.0.html
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1934
Re: TRegistry and cross platform
« Reply #1 on: March 22, 2010, 05:00:08 pm »
Afaics, it writes to a file "reg.xml" in a configuration directory.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TRegistry and cross platform
« Reply #2 on: March 22, 2010, 08:10:03 pm »
Does it means that if you use TRegistry object, it should work in Windows, but does nothing under Linux?

Windows programmers are used to store window position data, for example, in the registry. If TRegistry object does nothing under Linux, it is not so useful to cross-platform programming.

Add a Value with any Name to StoredValues property of a TXMLPropStorage and do something like:

Code: [Select]
procedure TFormPrincipal.XMLPropStorageSavingProperties(Sender: TObject);
begin
  XMLPropStorage.StoredValues[0].Value := Edit.Text;
end;

procedure TFormPrincipal.XMLPropStorageRestoreProperties(Sender: TObject);
begin
  Edit.Text := XMLPropStorage.StoredValues[0].Value;
end; 


You can edit the value in Edit.Text and get it back after closing and restarting the program. It will be stored in an INI file and it will work fine in Linux.

Linux has no Registry.

This example should work in Linux: http://www.lazarusbrasil.org/Salvando Propriedades Cross-Plataforma.zip
« Last Edit: March 22, 2010, 10:08:21 pm by typo »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12715
  • FPC developer.
Re: TRegistry and cross platform
« Reply #3 on: March 22, 2010, 10:10:36 pm »
Linux has no Registry.

(one could consider /proc or sysctl a registry. But that is more the kernel/system side of things. It is not an application settings database)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: TRegistry and cross platform
« Reply #4 on: March 23, 2010, 07:30:51 am »
Ok, I've tested my software at home on my Linux box (Ubuntu 9.10) and working properly, but where is this XML file is to include this data?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TRegistry and cross platform
« Reply #5 on: March 24, 2010, 12:37:19 pm »
Quote
where is this XML file is to include this data?
Usually /etc or your home directory (with . prepended so it's hidden)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: TRegistry and cross platform
« Reply #6 on: March 24, 2010, 02:38:00 pm »
Quote
where is this XML file is to include this data?
Usually /etc or your home directory (with . prepended so it's hidden)

Thank you!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

fedkad

  • Full Member
  • ***
  • Posts: 178
Re: TRegistry and cross platform
« Reply #7 on: December 21, 2017, 12:41:48 pm »
Sorry to raise this old topic. But, my question is very related to this and I haven't found anything in the forum about my problem.

Yes, TRegistry works in Linux. However, instead of using a "per user" location like "~/.config/Lazarus" to store the reg.xml file, it uses a different location for each of the program's main form title, which is very strange.

For example, when the title of main program's main window is "myProgram - aFile" it saves the reg.xml file under the directory "~/.config/aFile - myProgram/". This is completely useless, since even the same program cannot re-access its old configuration values, once its title changes. Is there a way to make the path at least something like "~/.config/myProgram/reg.xml"?
« Last Edit: December 21, 2017, 02:31:29 pm by fedkad »
Lazarus 4.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TRegistry and cross platform
« Reply #8 on: December 21, 2017, 01:48:52 pm »
Is there a way to make the path at least something like "~/.config/myProgram/reg.xml"?
Of course. Since this is open source you can just adapt the source of TRegistry to suit your preferences on Linux.
If you think other Linux users may want to adopt the monolithic Windows-OS-database-registry approach to storing program data defaults according to your preferences you could submit your adaptation as a patch. Perhaps it would be accepted.

fedkad

  • Full Member
  • ***
  • Posts: 178
Re: TRegistry and cross platform
« Reply #9 on: December 21, 2017, 02:10:40 pm »
It can be fixed of course. But, what I am asking is this:

(1) Why is the folder name chosen like this?
(2) Is there a simple solution to fix this without modifying Lazarus code?
Lazarus 4.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: TRegistry and cross platform
« Reply #10 on: December 21, 2017, 03:10:22 pm »
However, instead of using a "per user" location like "~/.config/Lazarus" to store the reg.xml file, it uses a different location for each of the program's main form title, which is very strange.

Are you sure it takes the "Title" of the main form of the application?

As far as I can see (I can't test now) Linux takes the ApplicationName. Default this is the executable name.
But you can override it with OnGetApplicationName.

(This is because  GetAppConfigDir  is used in the xregreg.inc for determining the directory.)

fedkad

  • Full Member
  • ***
  • Posts: 178
Re: TRegistry and cross platform
« Reply #11 on: December 22, 2017, 10:42:28 am »
Quote
Are you sure it takes the "Title" of the main form of the application?

As far as I can see (I can't test now) Linux takes the ApplicationName. Default this is the executable name.
But you can override it with OnGetApplicationName.

(This is because  GetAppConfigDir  is used in the xregreg.inc for determining the directory.)

You are right! I was also changing Application.Title in parallel to Main Form Title. This was an old Delphi / Lazarus code that I was trying to port to Linux.

The following code in the application main body did the trick:

Code: Pascal  [Select][+][-]
  1.   {$ifdef LINUX}
  2.   OnGetApplicationName := @appname;
  3.   {$endif}

where appname is a function that returns the appropriate String.

Thank you very much for pointing me to right places!
Lazarus 4.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

PatBayford

  • Full Member
  • ***
  • Posts: 125
Re: TRegistry and cross platform
« Reply #12 on: December 23, 2017, 01:22:15 am »
I believe this goes back to Borland's original TRegIniFile object, which was designed to allow simple access to Registry/INI file data without to programmer being aware of anything apart from the API.
At the moment I am having trouble reading data from the Windows 64bit registry - TREgistry refuses to access keys that I know are there, because I made them!!!!
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: TRegistry and cross platform
« Reply #13 on: December 23, 2017, 01:31:48 am »
At the moment I am having trouble reading data from the Windows 64bit registry - TREgistry refuses to access keys that I know are there, because I made them!!!!
And is your program 32 or 64 bit? If it is 32-bit you need to do something extra to read the 64-bit part of the registry.
Also see http://wiki.freepascal.org/fcl-registry

Otherwise show some code.

 

TinyPortal © 2005-2018