Registry


Windows registry is a big hierarchical database which contains information about
hardware and software including windows itself. Configuration information about any
application can be stored and retreived from Windows registry. This configuration
information can be application form coordinates, width, height, color, and many others.

In early Windows versions befor using registry, applications save it's configuration
in a seperate file called initialization file, or configuration file (.ini, or .cfg)
which always exists withen application directory or in Windows directory. Initialization
and configuration files are easy to be deleted and damaged, moreover any normal user
can access it and modify it's contents, but Windows registry is more safe than normal
files. Windows grantees that registry database will be exists in safe area and it
makes a backup for it periodically. In addition to that it is hard be accessed and
modified by normal users.

Accessing Registry in Delphi

Windows Registry can be accessed by declaring TRegistry objects which exists in Registry
unit.
TRegistry class encapsulate functions and properties which used to access and
manipulate Windows registry such as adding keys (Key is like a directory in which
we can store data or create sub keys), reading and writing different kinds of data,
deleting key, and so on.

Example

In this example we want to store form's coordinates in registry and retrieving it
when opening the application again:

- Add
Registry to unit's clause.
- At form's
OnClose event write:

var
MyRegs:
TRegistry;
begin
MyRegs:= TRegistry.Create;
MyRegs.
RootKey:= HKEY_LOCAL_MACHINE;  // Root key (directory)

(*** Open \Software\My Company\ key, create it if it is not exist ***)
MyRegs.
OpenKey('\SOFTWARE\My Company\', True);

(*** Write data to registry,
 in (HKEY_LOCAL_MACHINE\SOFTWARE\My Company ***)
MyRegs.
WriteInteger('Left', Self.Left);
MyRegs.
WriteInteger('Top', Self.Top);
MyRegs.
WriteInteger('Width', Self.Width);
MyRegs.
WriteInteger('Height', Self.Height);
MyRegs.
CloseKey;
MyRegs.Free;

end;


- At form's
OnCreate event write:

var
MyRegs: TRegistry;
L, T, W, H: integer;
begin
MyRegs:= TRegistry.Create;
MyRegs.RootKey:= HKEY_LOCAL_MACHINE;  // Root key (directory)

(*** Open \Software\My Company\ key if there is a data ***)
if MyRegs.
OpenKey('\SOFTWARE\My Company\', False) then
begin

  (*** Read data from registry,
   at (HKEY_LOCAL_MACHINE\SOFTWARE\My Company ***)
  L:= MyRegs.
ReadInteger('Left');
  T:= MyRegs.
ReadInteger('Top');
  W:= MyRegs.
ReadInteger('Width');
  H:= MyRegs.
ReadInteger('Height');

  (*** Apply form intialization ***)
  Self.Left:= L;
  Self.Top:= T;
  Self.Width:= W;
  Self.Height:= H;
  MyRegs.
CloseKey;
end;
MyRegs.Free;

end;

- Run the application then try to resize and move the form, after that close it
and run the application again. You will notice that the form will maintain it's last
postion and size.


Sammary:


This is the main methods and properties to access Windows registry:

- Create registry object:

MyRegs:= TRegistry.Create;

- Selecting root key (directory)

MyRegs.RootKey:= HKEY_LOCAL_MACHINE;

- Opening a key: There are two methods of opening keys:

1.  MyRegs.OpenKey('\Software\AKey', False);

This method try to open an existing key it it is not exits, it will return false.
If it is successed on opening key it will return True. This kind of opening key always
used when reading from registry.

2.  MyRegs.OpenKey('\Software\AKey', True);

This method try to open an existing key, if key not found it will attempt to create
new one.
OpenKey will return True if it is successed in opening or creating key,
otherwise it will return
False. This kind of opening keys always used when writing
to registry.

- Writing to registry: you can write to the registry different data types such as:

MyRegs.
WriteString('Name', 'Mohammed');
MyRegs.
WriteInteger('Age', 24);
MyRegs.
WriteBoolean('Single', True);

- Reading from registry: you can read from registry existed data such as:

 Name:= MyRegs.ReadString('Name');
Age:= MyRegs.
ReadInteger('Age');
Single:= MyRegs.
ReadBoolean('Single');

- Closing key:

 MyRegs.CloseKey;



Note:

You can use Regedit.exe to access Windows registry. Using Regedit is very helpfull
to understand Windows registry.

See also:

Creating components at run-time