Home Index

Registry Prog. with C++ Builder and Delphi, Part I, Reading from registry
Date: April  01 2001
Subject: Online Programmer Ezine, Volume 2, No.4
Article: Registry Prog. with C++ Builder and Delphi, Part I, Reading from registry

Old windows programmers may remember when they used to save 
program initialization parameters in .ini files in windows 
or program directory.

In windows 95 and afterwards there is a system database 
called registry. Registry is a big database that both 
programs and windows itself use it to save initialization 
and settings of programs.

This solution eliminates the need for several ini files. The
only point that you must consider is that registry database 
is very sensitive. If you delete or damage a part of it can 
make your windows useless. So you must work with it with care.

You can also edit and view registry with regedit.exe program
that comes with windows. (You can run it from command prompt)

Windows 2000 stores its configuration information in a 
database called the registry. Registry is created from 
thousands of setting values. This setting values are 
categorized in a tree of folders. These folders are called 
keys. Setting values are inserted inside keys.

Values have different types. REG_SZ is string type. REG_DWORD
is long integer type. REG_BINARY is binary type. 

You can save settings of your programs in registry database.

In this article we want to see how we can create, read and 
update registry keys and values in C++ Builder and Delphi.

In C++ Builder and Delphi, there is a registry object. We 
will use registry object methods and properties for modifying
and reading registry.

In this part of article we will see how we can read setting 
values from registry database.

Following function is a sample code for reading a value from 
registry into a character string "gSerial".

void ReadSerKey()
AnsiString temp;
char gSerial[40];

TRegistry *Reg = new TRegistry();
ShowMessage("Can not open program serial key .");
catch(ERegistryException &E) {
delete Reg;
delete Reg;

1- First of all we must create a Registry object in memory 
using "new" command.

2- Registry database has five root keys. Five trees grow 
from these five root keys. Root keys are:


Before starting to work we must determine one of root keys 
as starting point for the tree that our key is on it. In 
this example our key is located on HKEY_LOCAL_MACHINE.

Complete address of our key is: 


i.e. our key is TestSoftware. We have chosen the name 
exactly as our software name. This will ease finding the 

3-Now we test to see if the key we want to read exists. If 
the key exists we start reading setting values, otherwise 
we delete registry object we created when starting this 
function and leave the function. This is done using 
"exists" method of registry object.

4- if the key exists we can open. We use try...catch to 
prevent exceptions. We can open the key using "OpenKey" 
method of registry object.

5- If opening the key was successful then we can use 
"ReadString" method of registry object to read the value 
"SERIAL" from registry key and put it inside an AnsiString 
type and then copy it in an ordinary C type string array.

6- After all we will close the key. And before exit we must 
delete registry key. In next issue of ezine we will see 
sample codes for creating and writing registry keys and 

Home Index
2001 OnlineProgrammer (HtmSoft.com) . All Rights Reserved