Home Index

Registry Prog. with C++ Builder and Delphi , Part II, Writing and Updating
 
Date: June  01 2001
Subject: Online Programmer Ezine, Volume 2, No.5
Article: Registry Prog. with C++ Builder and Delphi , Part II, Writing and Updating

In previous part of this article we learned how we can read
a value from windows registry.

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

Following functions are sample codes for writing and updating
a value in windows registry from a global character string 
variable "gSerial".


void UpdateSerKey()
{
TRegistry *Reg = new TRegistry();
AnsiString temp;

Reg->RootKey = HKEY_LOCAL_MACHINE;
if(Reg->KeyExists("SOFTWARE\\TestSoftware"))
{
try {
if(Reg->OpenKey("Software\\TestSoftware",FALSE))
{
Reg->WriteString("SERIAL",gSerial);
Reg->CloseKey();
}
else ShowMessage("Can not open program serial key .");
Reg->CloseKey();
}
catch(ERegistryException &E) {
ShowMessage(E.Message);
delete Reg;
return;
}
}
delete Reg;
}


void MakeSerKey()
{
TRegistry *Reg = new TRegistry();
AnsiString temp;

Reg->RootKey = HKEY_LOCAL_MACHINE;
if(!Reg->KeyExists("SOFTWARE\\TestSoftware"))
{
if(!Reg->CreateKey("Software\\TestSoftware"))
{
Application->MessageBox
("Can not Make Serial Key","Error",MB_OK);
return;
}

try {
if(Reg->OpenKey("Software\\TestSoftware",FALSE))
{
Reg->WriteString("SERIAL",gSerial);
Reg->CloseKey();
}
else ShowMessage("Registry RootDir error");
Reg->CloseKey();
}
catch(ERegistryException &E) {
ShowMessage(E.Message);
delete Reg;
return;
}
}
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:

HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
HKEY_CURRENT_CONFIG

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: 

HKEY_LOCAL_MACHINE\Software\TestSoftware 

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

3-Now we test to see if the key we want to write a value 
inside it exists. If the key exists we start writing or
updating 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 
"WriteString" method of registry object to write the value 
"SERIAL" to registry key.

6- After all we will close the key. And before exit we must 
delete registry key. 

If you have any question about this article you can post
your message in our message boards.

http://op.htmsoft.com/forums/


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