An Introduction to Working With Windows Registry

Windows 8 Start Screen on a Laptop
georgeclerk / Getty Images

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 is organized in a similar way to Windows Explorer. We can use regedit.exe 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 a ​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 database.

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) ;
var
reg:TRegistry;
begin
reg:=TRegistry.Create;
with reg do begin
try
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
SystemParametersInfo (SPI_SETDESKWALLPAPER,0, nil,SPIF_SENDWININICHANGE) ;
SystemParametersInfo (SPI_SETSCREENSAVEACTIVE,0, nil,SPIF_SENDWININICHANGE) ;
end
finally
reg.Free;
end;
end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

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.)

Format
mla apa chicago
Your Citation
Gajic, Zarko. "An Introduction to Working With Windows Registry." ThoughtCo, Feb. 16, 2021, thoughtco.com/working-with-windows-registry-1058474. Gajic, Zarko. (2021, February 16). An Introduction to Working With Windows Registry. Retrieved from https://www.thoughtco.com/working-with-windows-registry-1058474 Gajic, Zarko. "An Introduction to Working With Windows Registry." ThoughtCo. https://www.thoughtco.com/working-with-windows-registry-1058474 (accessed March 28, 2024).