Home / Simple Registry
Name Modified Size InfoDownloads / Week
Parent folder
ReadMe.md 2024-04-13 5.5 kB
SimpleRegistry.PAS 2024-04-12 20.5 kB
Totals: 2 Items   25.9 kB 0

Simple Registry

The purpose of this unit is to provide a possiblity to acces the windows registry in a simple manner.

Usage

The 'Interface' part of the unit looks as follows (the documentation is self explaining):


(*
  This Unit offers the most important functions of the TRegistry unit with the
  following differences:
  1. You do not have to 'open' and 'close' keys any more.
  2. Each function has an extra parameter: the 'Key'. This Key is automatically
     opened and closed to perform the function. The 'Key' is always a full path
     starting from the RootKey.
  3. All 'Read' functions require an extra parameter: the 'Default' Value.
     This is the Value returned if the Key or the Value does not exist.
  4. The 'Write' routines now return a Boolean Value indicating success instead of
     throwing an execption of failiure.
  5. Ability to Read and Write "TStrings".
  6. Ability to Read and Write "Multi-sz" strings.
  7. Ability to read and write some "Object" (like "TEdits") contents. 
*)

(* Example:

  -- Without usage of TSimpleRegistry --

  function TRegistratieForm.readregister_date : integer;
  var TheReg: TRegistry;
  begin
    Result := 0;
    TheReg := TRegistry.Create;
    try
      TheReg.RootKey := HKEY_CURRENT_USER;
      if (TheReg.KeyExists(KeyName)) then
      begin { Key exists }
        if TheReg.OpenKey(KeyName, False)
        then begin
               if TheReg.ValueExists('Reg') then { Value exists }
                  Result := TheReg.ReadInteger('Reg');
               TheReg.Closekey;
             end;
      end;    
    finally
      TheReg.Free;
    end;
  end;

  -- With usage of TSimpleRegistry --

  function TRegistratieForm.readregister_date : integer;
  var TheReg: TSimpleRegistry;
  begin
    Result := 0;
    TheReg := TSimpleRegistry.Create;
    try
      TheReg.RootKey := HKEY_CURRENT_USER;
      Result         := TheReg.ReadInteger(Keyname, 'Reg', 0);
    finally
      TheReg.Free;
    end;
  end;

*)

interface

uses Windows, Registry, Classes;

type
  TSimpleRegistry =
    class
  private
    Reg: TRegistry;
    function GetRoot: HKEY;
    procedure SetRoot(r: HKEY);
    function OpenForRead(const Key, Name: string): Boolean;
  public
    constructor Create; Overload;
    constructor Create(AAccess:LongWord); overload;
    destructor Destroy; override;

    // Root Key
    property RootKey: HKEY read GetRoot write SetRoot;

    // Read functions
    function ReadString(const Key, Name, Default: string): string;
    function ReadBool(const Key, Name: string; Default: Boolean): Boolean;
    function ReadInteger(const Key, Name: string; Default: Integer): Integer;
    function ReadBinaryData(const Key, Name: string; var Buffer; BufSize: Integer): Integer;
    function ReadCurrency(const Key, Name: string; Default: Currency): Currency;
    function ReadDate(const Key, Name: string; Default: TDateTime): TDateTime;
    function ReadDateTime(const Key, Name: string; Default: TDateTime): TDateTime;
    function ReadFloat(const Key, Name: string; Default: Double): Double;
    function ReadTime(const Key, Name: string; Default: TDateTime): TDateTime;
    procedure ReadTStrings(const Key, Name: string; Texts: TStrings);
    procedure ReadMultiString(const Key, Name: string; Texts: TStrings);
    function ReadWideString(const Key, Name: string; Default: WideString = ''): WideString;

    // Write functions
    function WriteString(const Key, Name, value: string): Boolean;
    function WriteExpandString(const Key, Name, value: string): Boolean;
    function WriteBool(const Key, Name: string; value: Boolean): Boolean;
    function WriteInteger(const Key, Name: string; value: Integer): Boolean;
    function WriteBinaryData(const Key, Name: string; var Buffer; BufSize: Integer): Boolean;
    function WriteCurrency(const Key, Name: string; value: Currency): Boolean;
    function WriteDate(const Key, Name: string; value: TDateTime): Boolean;
    function WriteDateTime(const Key, Name: string; value: TDateTime): Boolean;
    function WriteFloat(const Key, Name: string; value: Double): Boolean;
    function WriteTime(const Key, Name: string; value: TDateTime): Boolean;
    function WriteTStrings(const Key, Name: string; Texts: TStrings): Boolean;
    function WriteMultiString(const Key, Name: string; Texts: TStrings): boolean;
    function WriteWideString(const Key, Name: string; value: WideString): Boolean;

    // Miscellaneous functions
    function KeyExists(const Key: string): Boolean;
    function ValueExists(const Key, Name: string): Boolean;
    function DeleteKey(const Key: string): Boolean;
    function DeleteValue(const Key, Name: string): Boolean;
    function CreateKey(const Key: string): Boolean;
    function GetDataInfo(const Key, Name: string; var value: TRegDataInfo): Boolean;
    function GetDataSize(const Key, Name: string): Integer;
    function GetDataType(const Key, Name: string): TRegDataType;
    function GetKeyInfo(const Key: string; var value: TRegKeyInfo): Boolean;
    function GetKeyNames(const Key: string; Strings: TStrings): Boolean;
    function GetValueNames(const Key: string; Strings: TStrings): Boolean;
    function HasSubKeys(const Key: string): Boolean;
    function RenameValue(const Key, OldName, NewName: string): Boolean;
  end;

Have fun!

Source: ReadMe.md, updated 2024-04-13