An introduction to working with Windows Registry

The Registry is simply a database that an application can use to store and retrieve configuration information (last window size and position, user options and information or any other configuration data). Registry also contains information about Windows (95/98/NT) and about your Windows configuration.

The Registry "database" is stored as a binary file. To find it, run regedit.exe (Windows registry editor utility) in your Windows directory.

You will see that information in Registry are organized in a similar way to Windows Explorer. We can use regedit to view registry information, change it or to add some information to it. It is obvious that modifications of the registry database could lead to system crash (of course if you don't know what you are doing).

INI vs. Registry

It is probably very well known that in the days of Windows 3.xx INI files were a popular way of storing application information and other user-configurable settings. The most terrifying aspect of INI files is that they are just text files that the user can easily edit (change or even delete them).
In 32-bit Windows Microsoft recommends using Registry to store the type of information that you would normally place in INI files (users are less likely to alter registry entries).

Delphi provides full support for changing entries in the Windows System Registry: via the TRegIniFile class (same basic interface as the TIniFile class for users of INI files with Delphi 1.0) and TRegistry class (low-level wrapper for the Windows registry and functions that operate on the registry).

Simple tip: writing to the Registry

As mentioned before in this article, basic registry operations (using code manipulation) are reading information from registry and writing information to the registry.

Next piece of code will change the Windows wallpaper and disable the screen saver using TRegistry class.

Before we can use TRegistry we have to add Registry unit to the uses clause at the top of source-code.

uses registry;
procedure TForm1.FormCreate(Sender: TObject) ;
with reg do begin
if OpenKey('\Control Panel\desktop', False) then begin
//change wallpaper and tile it
reg.WriteString ('Wallpaper','c:\windows\CIRCLES.bmp') ;
reg.WriteString ('TileWallpaper','1') ;
//disable screen saver//('0'=disable, '1'=enable)
reg.WriteString('ScreenSaveActive','0') ;
//update changes immediately

Those two lines of code that start with SystemParametersInfo ... force Windows to update the wallpaper and screen saver information immediately. When you run your application, you'll see the Windows wallpaper bitmap change to the Circles.bmp image (that is if you have circles.bmp image in your Windows directory).
Note: your screen saver is now disabled.

More TRegistry usage samples