Solved

Registry question

Posted on 2004-10-12
14
180 Views
Last Modified: 2010-04-05
Anyone have a good idea on what the best way to save/load information from a record structure to the registry?

record structure:

Type
    TidList = packed record

      item1 : boolean;
      item2 : byte;
      itype  : string [255];
      iavail  : boolean;

end
0
Comment
Question by:mantra246
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12286105
make it as a class and implement Load / Save methods...

type
  MyString = string[255];

  TidList = class
  private
  public
    item1   : boolean;
    item2   : byte;
    itype   : MyString;
    iavail  : boolean;
    procedure SetValues (it1 : Boolean; it2 : Byte; itype : MyString; iav : Boolean);
    procedure LoadFromRegistry;
    procedure SaveToRegistry;
  end;
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12286118
Here's an example, all-in-one, break out into separate routines...

procedure SaveOrLoad(aItem:tIDList);
const
  KeyName='SAVEME';
var
  R:tregistry;
begin
  R:=TRegistry.Create(KEY_READ);
  try
    R.RootKey = HKEY_LOCAL_MACHINE;

    // save
    R.OpenKey(KeyName,True); //create if doesn't exist
    with aItem do begin
      R.WriteBool('ITEM1');
      R.WriteString('ITYPE');
      R.WriteInteger('ITEM2');
      R.WriteBool('IAVAIL');
    end;

    // Load
    R.OpenKey(KeyName,False); //do not create if doesn't exist
    with aItem do begin
      item1:=R.ReadBool('ITEM1');
      itype:=R.ReadString('ITYPE');
      item2:=R.ReadInteger('ITEM2');
      iavail:=R.ReadBool('IAVAIL');
    end;
  finally
    R.Free;
  end;
end;
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12286133
Oops...this is the SAVE part:

    with aItem do begin
      R.WriteBool('ITEM1',item1);
      R.WriteString('ITYPE',itype);
      R.WriteInteger('ITEM2',item2);
      R.WriteBool('IAVAIL',iavail);
    end;
0
 
LVL 7

Accepted Solution

by:
sftweng earned 250 total points
ID: 12286195
Using TRegistry or TRegistryIniFile, you are pretty much restricted to doing individual read and write operations for each element of the record structure. E.g.,

procedure WriteToRegistry (aTidList : TidList);
const
  RegistryName = 'MyRegistryTidList';
var
  fRegistryIniFile : TRegistryIniFile;
begin
  fRegistryIniFile                                              
    := TRegistryIniFile.Create(RegistryName);                    
  try
    fRegistryIniFile.WriteBool(RegistrySection,'item1',aTidList.item1);
    fRegistryIniFile.WriteInteger(RegistrySection,'item2',ord(aTidList.item2));
    fRegistryIniFile.WriteString(RegistrySection,'itype',aTidList.iType);
    fRegistryIniFile.WriteBool(RegistrySection,'iavail',aTidList.iavail);
  finally                                                        
    fRegistryIniFile.Free;                                      
  end {try};  
end {WriteToRegistry};

procedure ReadFromRegistry (aTidList : TidList);
const
  RegistryName = 'MyRegistryTidList';
var
  fRegistryIniFile : TRegistryIniFile;
begin
  fRegistryIniFile                                              
    := TRegistryIniFile.Create(RegistryName);                    
  try
    aTidList.item1 := fRegistryIniFile.ReadBool(RegistrySection,'item1',False);
    aTidList.item2 :=  fRegistryIniFile.Integer(RegistrySection,'item2',0) mod 256;
    aTidList.itype := fRegistryIniFile.ReadString(RegistrySection,'itype','');
    aTidList.iavail := fRegistryIniFile.ReadBool(RegistrySection,'iavail',Falsel);
  finally                                                        
    fRegistryIniFile.Free;                                      
  end {try};  
end {ReadFromRegistry};

You might also like to use ReadSection to get the list ok keys.
0
 
LVL 6

Assisted Solution

by:rbohac
rbohac earned 250 total points
ID: 12286508
You could also write the entire record as binary data:


var
  Form1: TForm1;
  reg : TRegistry;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Reg := TRegistry.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Reg.Free;
end;

procedure TForm1.btnWriteDataClick(Sender: TObject);
var
  Data : TidList;
begin
  if Reg.OpenKey('SOFTWARE\TEST',True) then
    begin
      Reg.WriteBinaryData('MyData',Data,SizeOf(TIdList));
      Reg.CloseKey;
    end;
end;

procedure TForm1.btnReadDataClick(Sender: TObject);
var
  Data : TidList;
begin
  if Reg.OpenKey('SOFTWARE\TEST',False) then
    begin
      if Reg.ValueExists('MyData') then
        Reg.ReadBinaryData('MyData',Data,SizeOf(TIdList));
      Reg.CloseKey;
    end;
end;
0
 
LVL 7

Expert Comment

by:sftweng
ID: 12286553
rbohac, agreed that you CAN do that however I recommend against it; by writing the individual elements, you can survive a change of structure later on, e.g., allowing the use of registry entries from a prior release of the software even though the internal representation might have changed.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 12286570
Another consideration - Microsoft is moving away from a centralized registry in .NET, in favour of local INI files. Therefore I believe that TRegistryIniFile is a better choice since it supports both the registry and *.ini files, depending on the opearting system.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mantra246
ID: 12286652
sftweng - neat method you use, but what do you mean about ReadSection? (i've never really used the registry before other than to store the odd variable or two)
0
 

Author Comment

by:mantra246
ID: 12286669
> Microsoft is moving away from a centralized registry in .NET, in favour of local INI files

I wish Microsoft would make up their mind - can you say U-Turn.  It used to be that INI files were the expected way to go then the registry was the better option to be used - now ini files are to be used again!  Sheesh!
0
 
LVL 6

Expert Comment

by:rbohac
ID: 12286705
I agree, but it is useful if you don't want the data to be <easily> editably by the end user
0
 
LVL 7

Expert Comment

by:sftweng
ID: 12287030
mantra246, ReadSection from Delphi help:

Reads all the key names from a specified section of an INI file into a string list.

procedure ReadSection (const Section: String; Strings: TStrings); override;

Description

Call ReadSection to retrieve the names of all keys within a specified section of an INI file into a string list object.

Section identifies the section from which to retrieve a list of key names. Strings specifies the string object to hold the retrieved names. Strings can point to a TString object such as a string list, or to a component property, such as Items for a TListBox component, that is a TString object itself.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 12287053
rbohac, a good point. The usual tradeoff between ease of use and security. ;-)
0
 

Author Comment

by:mantra246
ID: 12491750
Thanks to both sftweng and rbohac for showing two very good methods.

I raised the points to give you both the original 250 points each.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 12492175
Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A short film showing how OnPage and Connectwise integration works.

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now