Using Registry in Delphi

Registry is a very vital part in the Windows. Because of its importance, a lot of viruses and hacking programs that use the registry as part of his illegal operations. In the present article, I’ll take playing around with the registry for a moment.

We start by declaring the Registry unit in the Uses, looks like:

Uses     Registry;

It’s simple, isn’t it?. Go on. The registry data type can be:

  1. Binary
  2. String
  3. DWord
  4. Boolean

With different data types, then writing to the registry will use a different function. The following description will explain how to write each data type before.

A. Writing Binary Data

  1. Instantiate the class of type Registry.
    instantiating class must be preceded by declaring a variable as TRegistry. In the example below, the variable is the reg:

    var
        reg : TRegistry;
        i : integer;
        DataKey : array of byte;
        info : TRegDataInfo;

    begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_CURRENT_USER;
    …………………………………………………………..

  2. Open registry

    Example:
    reg.OpenKey(’Control PanelAppearanceSchemes’,true);

    OpenKey command takes two parameters. The first parameter is a subkey of the registry, while the second parameter declares whether allowed to make a key if the key is not found.

  3. Write registry

    Example:
    DataKey[0] := $10;
    reg.WriteBinaryData(’Aubergine’,pointer(DataKey)^, info.DataSize);     

  4. Close registry for saving all data that is wrote

    reg.CloseKey;

  5. Destroy object from memory

    FreeAndNil(reg);

B. Read Binary Data

  1. Instantiate the class of type Registry.
    instantiating class must be preceded by declaring a variable as TRegistry. In the example below, the variable is the reg:

    var
        reg :  TRegistry;
        i  :  integer;
        DataKey :  string;
    begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_CURRENT_USER;
    …………………………………………………………..

  2. Open registry

    Example:
    reg.OpenKey(’Control PanelAppearanceSchemes’,true);

  3. Read registry

    Example:
    reg.ReadBinaryData(’TempatData’, DataKey, sizeof(DataKey));
    ShowMessage(DataKey);

  4. Close registry. Without it, data is not saved

    reg.CloseKey;

  5. Releasing memory from object

    FreeAndNil(reg);

C. Writing String Data

Below, I give example for writing autostart your program when windows

  1. Instantiate the class of type Registry.
    instantiating class must be preceded by declaring a variable as TRegistry. In the example below, the variable is the reg:

    var
        reg : TRegistry;
        i : integer;
        DataKey : string;
    begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_LOCAL_MACHINE;
    …………………………………………………………..

  2. Open registry

    Example:
    reg.OpenKey(’SoftwareMicrosoftWindowsCurrentVersionRun’,true);

  3. Write this

    Example:
    reg.WriteString(’MyProgram’,'C:MyFolderProgramMyProgram.exe’);

  4. Close registry object

    reg.CloseKey;

  5. Destroy object

    FreeAndNil(reg);

D. Read String Data

  1. Instantiate the class of type Registry.
    instantiating class must be preceded by declaring a variable as TRegistry. In the example below, the variable is the reg:

    var
        reg :  TRegistry;
        i :  integer;
        DataKey, IsiReg :  string;
    begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_LOCAL_MACHINE;
    ………………………………………………………………..

  2. Open registry

    Example:
    reg.OpenKey(’SoftwareMicrosoftWindowsCurrentVersionRun’,true);

  3. Read registry:

    Example:
    IsiReg := reg.ReadString(’MyProgram’);
    ShowMessage(IsiReg);

  4. Close registry

    reg.CloseKey;

  5. Erase object from memory

    FreeAndNil(reg);

E. Writing DWord Data

The explanation below is about the writing of data type DWORD in the registry by taking samples in order to disable registry.

  1. Instantiate the class of type Registry.
    instantiating class must be preceded by declaring a variable as TRegistry. In the example below, the variable is the reg:

    var
        reg :  TRegistry;
        i :  integer;
        DataKey, IsiReg :  string;
    begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_CURRENT_USER;
    ………………………………………………………………..

  2. Open registry

    Example:
    reg.OpenKey(’SoftwareMicrosoftWindowsCurrentVersionPoliciesSystem’,true);

  3. Write registry

    Example:
    reg.WriteInteger(’DisableRegistryTools’,00000001);

  4. Close registry

    reg.CloseKey;

  5. Destroy object

    FreeAndNil(reg);


F. Read DWord Data

  1. Instantiate the class of type Registry.
    instantiating class must be preceded by declaring a variable as TRegistry. In the example below, the variable is the reg:

    var
        reg :  TRegistry;
        i :  word;
        DataKey :  integer;
    begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_CURRENT_USER;
    …………………………………………………………

  2. Open registry

    Example:
    reg.OpenKey(’SoftwareMicrosoftWindowsCurrentVersionPoliciesSystem’,true);

  3. Read registry

    Example:
    i := reg.ReadInteger(’DisableRegistryTools’);
    ShowMessage(IntTostr(i));

  4. For saving data, we have to close registry

    reg.CloseKey;

  5. Always destroy object after using it

    FreeAndNil(reg);

G. Write Boolean Data

  1. Instantiate the class of type Registry.
    instantiating class must be preceded by declaring a variable as TRegistry. In the example below, the variable is the reg:

    var
        reg :  TRegistry;
        i :  word;
        DataKey :  integer;
    begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_CURRENT_USER;
    …………………………………………………………

  2. Open registry

    Example:
    reg.OpenKey(’SubKey’,true);

  3. Read registry

    Example:
    reg.WriteBool(’DisableRegistryTools’);

  4. For saving data, we have to close registry

    reg.CloseKey;

  5. Always destroy object after using it

    FreeAndNil(reg);

H. Read Boolean Data

  1. Instantiate the class of type Registry.
    instantiating class must be preceded by declaring a variable as TRegistry. In the example below, the variable is the reg:

    var
        reg :  TRegistry;
        YesNo :  Boolean;
        DataKey :  integer;
    begin
        reg := TRegistry.Create;
        reg.RootKey := HKEY_CURRENT_USER;
    …………………………………………………………

  2. Open registry

    Example:
    reg.OpenKey(’SubKey’,true);

  3. Read registry

    Example:
    YesNo := reg.ReadBool(’DisableRegistryTools’);

    if YesNo then
        ShowMessage(’YES’)
    else
        ShowMessage(’NO’);

  4. For saving data, we have to close registry

    reg.CloseKey;

  5. Always destroy object after using it

    FreeAndNil(reg);

comments powered by Disqus
Loading