Hello everybody! In this "Quick Tips" we will create a routine to log information in the Windows registry.
For this we have the class “TRegIniFile” in Delphi which is in the Registry Unit.
Initially we will create one Unit and save as “uRegister.pas”. We can see below the complete code where we have two methods: SaveRegister (Procedure) and ReadRegister (Function).
unit uRegister;
interface
uses Windows, Registry;
procedure SaveRegister(xFile, xSection, xIdent, xValue: String);
function ReadRegister(xFile, xSection, xIdent: String): String;
implementation
{Procedure to create a certain value in registry}
procedure SaveRegister(xFile, xSection, xIdent, xValue: String);
var
FileReg: TRegIniFile;
begin
FileReg := TRegIniFile.Create(xFile);
Try
FileReg.WriteString(xSection, xIdent, xValue);
Finally
FileReg.Free;
end;
end;
{Function to return the contents of a determined registry}
function ReadRegister(xFile, xSection, xIdent: String): String;
var
FileReg: TRegIniFile;
begin
FileReg := TRegIniFile.Create(xFile);
Try
Result := FileReg.ReadString(xSection, xIdent, '');
Finally
FileReg.Free;
end;
end;
end.
Now implement the sample using the methods developed in uRegistro.pas. Let's create a form with:
* 4 TLabeledEdit (EdtContent, EdtFile, EdtSection, EdtIdent);
* 2 BitBtn (BtnSave, BtnRead).
See the image below:

{Implementing the OnClick event of BtnSave}
procedure TFrmPrincipal.BtnSaveClick(Sender: TObject);
begin
SaveRegister(EdtFile.Text, EdtSection.Text, EdtIdent.Text, EdtContent.Text);
EdtContent.Clear;
end;
{ Implementing the OnClick event of BtnRead }
procedure TFrmPrincipal.BtnReadClick(Sender: TObject);
begin
EdtContent.Text := ReadRegister (EdtFile.Text, EdtSection.Text, EdtIdent.Text );
end;
Let's see the result directly in the registry. To do this, go to the same through the start menu / run and type regedit.
Finding the key HKEY_CURRENT_USER \ DelphiRegister \ Data

Where can we use this feature?
We can save the last user logged on, the last date of entry into the system, a serial register to hinder piracy of our software and more.
Until next Quick Tips!
Wesley Y
wyamazack@rwsolution.com.br





See the prices for this post in Mr.Bool Credits System below: