//////////////////////////////////////////////////////////
//
// Name        : TEasyReg
// Author      : Christian Aaangel (C.Aaangel@Mailexcite.com)
// Group       : Registry
// Purpose     : Easy Access to the registry
// Last update : Nov 20, 2006
//
//////////////////////////////////////////////////////////
//
// What's new:
// 1.00 - initial Release.
// 1.01 - Fixed Leave Open bug
//      - Added OnWrite event
//      - Added OnRead event
//      - Added OnClose event
// 1.02 - check for value before attemping to Read it
// 1.03 - Added close procedure
//      - Added open procedure
// 1.04 - Added DeleteKey Function
//        Added MakeKey Function
//
//////////////////////////////////////////////////////////
unit EasyReg;

interface

uses
  Windows, Messages, SysUtils, Classes, Registry;

type
  TOnWrite = Procedure(Sender: TObject) of Object;
  TOnRead = Procedure(Sender: TObject) of Object;
  TOnClose = Procedure(Sender: TObject) of Object;

  THKey = (CLASSES_ROOT, CURRENT_USER, LOCAL_MACHINE, USERS, CURRENT_CONFIG);
  TEasyReg = class(TComponent)
  private
   fHKey: THKey;
   fLocation: String;
   fLeaveOpen: Boolean;
   RegIsOpen: Boolean;
   WinReg: TRegistry;
   fOnWrite: TOnWrite;
   fOnRead: TOnRead;
   fOnClose: TOnClose;
   procedure SetHKey(const Value: THKEY);
   procedure SetLocation(const Value: String);
   procedure SetLeaveOpen(const Value: Boolean);
  protected
    { Protected declarations }
  public
   Constructor Create(aOwner: TComponent); Override;
   Destructor Destroy; Override;
   Procedure Write(Name, Value: String); overload;
   Procedure Write(Name: String; Value: Integer); overload;
   Function Read(Name: String; Var Value: string): boolean; overload;
   Function Read(Name: String; Var Value: Integer): boolean; overload;
   Procedure Close;
   Function Open(CanCreate: boolean): boolean;
   Function DeleteKey(Key: String): Boolean;
   Function MakeKey(Key: String): Boolean; 
  published
   Property HKEY: THKEY Read fHKey Write SetHKey;
   Property Path: String Read fLocation Write SetLocation;
   Property LeaveOpen: Boolean Read fLeaveOpen Write SetLeaveOpen;
   Property OnWrite: TOnWrite read fOnWrite write fOnWrite;
   Property OnRead: TOnRead read fOnRead Write fOnRead;
   Property OnClose: TOnClose read fOnClose Write fOnClose;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('LPL Tools', [TEasyReg]);
end;

{ TEasyReg }

constructor TEasyReg.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  RegIsOpen := False;
end;

destructor TEasyReg.Destroy;
begin
 If RegIsOpen Then Begin
  If Assigned(fOnClose) Then fOnClose(Self);
  WinReg.Free;
 End;
 inherited;
end;

procedure TEasyReg.SetHKey(const Value: THKEY);
begin
  fHKey := Value;
  If RegIsOpen Then Begin
   If Assigned(fOnClose) Then fOnClose(Self);
   WinREg.Free;
   RegIsOpen := false;
  End;
end;

procedure TEasyReg.SetLocation(const Value: String);
begin
  fLocation := Value;
end;

Procedure TEasyReg.Write(Name, Value: string);
Begin
 If Not RegIsOpen Then Begin
  WinReg := TRegistry.Create;
  Case HKEY of
   CLASSES_ROOT: WinReg.RootKey := HKEY_CLASSES_ROOT;
   CURRENT_USER: WinReg.RootKey := HKEY_CURRENT_USER;
   LOCAL_MACHINE: WinReg.RootKey := HKEY_LOCAL_MACHINE;
   USERS: WinReg.RootKey := HKEY_USERS;
   CURRENT_CONFIG: WinReg.RootKey := HKEY_CURRENT_CONFIG;
  End;
//  WinReg.RootKey := HKey;
  RegIsOpen := true;
 End;
 If WinReg.OpenKey(fLocation, True) then
  WinReg.WriteString(Name, Value);
 If Not fLeaveOpen Then Begin
  If Assigned(fOnClose) Then fOnClose(Self);
  winreg.Free;
  RegIsOpen := False;
 End;
 If Assigned(fOnWrite) then fOnWrite(Self);
End;

Function TEasyReg.Read(Name: String; Var Value: string): boolean;
Begin
 Result := false;
 If Not RegIsOpen Then Begin
  WinReg := TRegistry.Create;
  Case HKEY of
   CLASSES_ROOT: WinReg.RootKey := HKEY_CLASSES_ROOT;
   CURRENT_USER: WinReg.RootKey := HKEY_CURRENT_USER;
   LOCAL_MACHINE: WinReg.RootKey := HKEY_LOCAL_MACHINE;
   USERS: WinReg.RootKey := HKEY_USERS;
   CURRENT_CONFIG: WinReg.RootKey := HKEY_CURRENT_CONFIG;
  End;
//  WinReg.RootKey := HKey;
  RegIsOpen := true;
 End;
 If WinReg.OpenKey(fLocation, False) Then begin
  If Winreg.valueexists(Name) then begin
   Value := WinReg.ReadString(Name);
   Result := true;
  end;
 end;
 If Not fLeaveOpen Then Begin
  If Assigned(fOnClose) Then fOnClose(Self);
  winreg.Free;
  RegIsOpen := False;
 End;
 If Assigned(fOnRead) then fOnRead(Self);
End;

procedure TEasyReg.SetLeaveOpen(const Value: Boolean);
begin
 If fLeaveOpen <> Value Then fLeaveOpen := Value;
end;

procedure TEasyReg.Write(Name: String; Value: Integer);
Begin
 If Not RegIsOpen Then Begin
  WinReg := TRegistry.Create;
  Case HKEY of
   CLASSES_ROOT: WinReg.RootKey := HKEY_CLASSES_ROOT;
   CURRENT_USER: WinReg.RootKey := HKEY_CURRENT_USER;
   LOCAL_MACHINE: WinReg.RootKey := HKEY_LOCAL_MACHINE;
   USERS: WinReg.RootKey := HKEY_USERS;
   CURRENT_CONFIG: WinReg.RootKey := HKEY_CURRENT_CONFIG;
  End;
//  WinReg.RootKey := HKey;
  RegIsOpen := true;
 End;
 If WinReg.OpenKey(fLocation, True) then
  WinReg.WriteInteger(Name, Value);
 If Not fLeaveOpen Then Begin
  If Assigned(fOnClose) Then fOnClose(Self);
  winreg.Free;
  RegIsOpen := False;
 End;
 If Assigned(fOnWrite) then fOnWrite(Self);
End;

function TEasyReg.Read(Name: String; var Value: Integer): boolean;
Begin
 Result := false;
 If Not RegIsOpen Then Begin
  WinReg := TRegistry.Create;
  Case HKEY of
   CLASSES_ROOT: WinReg.RootKey := HKEY_CLASSES_ROOT;
   CURRENT_USER: WinReg.RootKey := HKEY_CURRENT_USER;
   LOCAL_MACHINE: WinReg.RootKey := HKEY_LOCAL_MACHINE;
   USERS: WinReg.RootKey := HKEY_USERS;
   CURRENT_CONFIG: WinReg.RootKey := HKEY_CURRENT_CONFIG;
  End;
//  WinReg.RootKey := HKey;
  RegIsOpen := true;
 End;
 If WinReg.OpenKey(fLocation, False) Then begin
  If Winreg.valueexists(Name) then begin
   Value := WinReg.ReadInteger(Name);
   Result := true;
  end;
 end;
 If Not fLeaveOpen Then Begin
  If Assigned(fOnClose) Then fOnClose(Self);
  winreg.Free;
  RegIsOpen := False;
 End;
 If Assigned(fOnRead) then fOnRead(Self);
End;

procedure TEasyReg.Close;
begin
 If RegIsOpen then begin
  RegIsOpen := false;
  FreeAndNil(WinReg);
 end;
end;

function TEasyReg.Open(CanCreate: boolean): boolean;
begin
 LeaveOpen := true;
 If not RegIsOpen then begin
  WinReg := TRegistry.Create;
  RegIsOpen := true;
 end;
 Case HKEY of
  CLASSES_ROOT: WinReg.RootKey := HKEY_CLASSES_ROOT;
  CURRENT_USER: WinReg.RootKey := HKEY_CURRENT_USER;
  LOCAL_MACHINE: WinReg.RootKey := HKEY_LOCAL_MACHINE;
  USERS: WinReg.RootKey := HKEY_USERS;
  CURRENT_CONFIG: WinReg.RootKey := HKEY_CURRENT_CONFIG;
 End;
 Result := WinReg.OpenKey(fLocation, CanCreate);
end;

Function TEasyReg.DeleteKey(Key: String): Boolean;
Var
 tempReg: TRegistry;
begin
 tempReg := TRegistry.Create;
 Case HKEY of
  CLASSES_ROOT: tempReg.RootKey := HKEY_CLASSES_ROOT;
  CURRENT_USER: tempReg.RootKey := HKEY_CURRENT_USER;
  LOCAL_MACHINE: tempReg.RootKey := HKEY_LOCAL_MACHINE;
  USERS: tempReg.RootKey := HKEY_USERS;
  CURRENT_CONFIG: tempReg.RootKey := HKEY_CURRENT_CONFIG;
 End;
 Result := tempReg.DeleteKey(Key);
 FreeAndNil(tempReg);
end;

function TEasyReg.MakeKey(Key: String): Boolean;
Var
 tempReg: TRegistry;
begin
 tempReg := TRegistry.Create;
 Case HKEY of
  CLASSES_ROOT: tempReg.RootKey := HKEY_CLASSES_ROOT;
  CURRENT_USER: tempReg.RootKey := HKEY_CURRENT_USER;
  LOCAL_MACHINE: tempReg.RootKey := HKEY_LOCAL_MACHINE;
  USERS: tempReg.RootKey := HKEY_USERS;
  CURRENT_CONFIG: tempReg.RootKey := HKEY_CURRENT_CONFIG;
 End;
 Result := tempReg.CreateKey(Key);
 FreeAndNil(tempReg);
end;

end.
