2007-08-10 14:48:00 +02:00
|
|
|
{
|
2008-03-17 10:33:24 +01:00
|
|
|
:: X2UtPersistRegistry implements persistency to the Windows Registry.
|
2007-08-10 14:48:00 +02:00
|
|
|
::
|
|
|
|
:: Last changed: $Date$
|
|
|
|
:: Revision: $Rev$
|
|
|
|
:: Author: $Author$
|
|
|
|
}
|
2007-06-08 16:10:03 +02:00
|
|
|
unit X2UtPersistRegistry;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
Classes,
|
|
|
|
Registry,
|
|
|
|
Windows,
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
X2UtPersist,
|
|
|
|
X2UtPersistIntf;
|
|
|
|
|
2007-06-08 16:10:03 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TX2UtPersistRegistry = class(TX2CustomPersist)
|
|
|
|
private
|
|
|
|
FKey: String;
|
|
|
|
FRootKey: HKEY;
|
|
|
|
protected
|
2007-08-10 14:48:00 +02:00
|
|
|
function CreateFiler(AIsReader: Boolean): IX2PersistFiler; override;
|
|
|
|
public
|
2010-04-13 12:04:05 +02:00
|
|
|
constructor Create;
|
2007-06-08 16:10:03 +02:00
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
property Key: String read FKey write FKey;
|
|
|
|
property RootKey: HKEY read FRootKey write FRootKey;
|
|
|
|
end;
|
2007-06-08 16:10:03 +02:00
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
TX2UtPersistRegistryFiler = class(TX2CustomPersistFiler)
|
|
|
|
private
|
|
|
|
FKey: String;
|
|
|
|
FRegistry: TRegistry;
|
|
|
|
protected
|
|
|
|
function OpenKey(const ANewKey: String): Boolean;
|
|
|
|
public
|
2007-06-08 16:10:03 +02:00
|
|
|
function BeginSection(const AName: String): Boolean; override;
|
2010-04-13 12:04:05 +02:00
|
|
|
procedure EndSection; override;
|
2007-06-08 16:10:03 +02:00
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
|
|
|
|
procedure GetKeys(const ADest: TStrings); override;
|
|
|
|
procedure GetSections(const ADest: TStrings); override;
|
|
|
|
|
2007-06-08 16:10:03 +02:00
|
|
|
|
|
|
|
function ReadInteger(const AName: String; out AValue: Integer): Boolean; override;
|
|
|
|
function ReadFloat(const AName: String; out AValue: Extended): Boolean; override;
|
|
|
|
function ReadString(const AName: String; out AValue: String): Boolean; override;
|
|
|
|
function ReadInt64(const AName: String; out AValue: Int64): Boolean; override;
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function ReadStream(const AName: string; AStream: TStream): Boolean; override;
|
|
|
|
|
2007-06-08 16:10:03 +02:00
|
|
|
|
|
|
|
function WriteInteger(const AName: String; AValue: Integer): Boolean; override;
|
|
|
|
function WriteFloat(const AName: String; AValue: Extended): Boolean; override;
|
|
|
|
function WriteString(const AName, AValue: String): Boolean; override;
|
|
|
|
function WriteInt64(const AName: String; AValue: Int64): Boolean; override;
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function WriteStream(const AName: string; AStream: TStream): Boolean; override;
|
|
|
|
|
|
|
|
procedure DeleteKey(const AName: string); override;
|
|
|
|
procedure DeleteSection(const AName: string); override;
|
2007-06-08 16:10:03 +02:00
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
|
|
|
|
property Key: String read FKey;
|
2007-06-08 16:10:03 +02:00
|
|
|
property Registry: TRegistry read FRegistry;
|
|
|
|
public
|
2007-08-10 14:48:00 +02:00
|
|
|
constructor Create(AIsReader: Boolean; ARootKey: HKEY; const AKey: String);
|
2010-04-13 12:04:05 +02:00
|
|
|
destructor Destroy; override;
|
2007-06-08 16:10:03 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ Wrapper functions }
|
|
|
|
function ReadFromRegistry(AObject: TObject; const AKey: String; ARootKey: HKEY = HKEY_CURRENT_USER): Boolean;
|
|
|
|
procedure WriteToRegistry(AObject: TObject; const AKey: String; ARootKey: HKEY = HKEY_CURRENT_USER);
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
SysUtils,
|
|
|
|
|
|
|
|
X2UtStrings;
|
|
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
RegistrySeparator = '\';
|
|
|
|
|
|
|
|
|
|
|
|
{ Wrapper functions }
|
|
|
|
function ReadFromRegistry(AObject: TObject; const AKey: String; ARootKey: HKEY): Boolean;
|
|
|
|
begin
|
2010-04-13 12:04:05 +02:00
|
|
|
with TX2UtPersistRegistry.Create do
|
2007-06-08 16:10:03 +02:00
|
|
|
try
|
|
|
|
RootKey := ARootKey;
|
|
|
|
Key := AKey;
|
|
|
|
Result := Read(AObject);
|
|
|
|
finally
|
2010-04-13 12:04:05 +02:00
|
|
|
Free;
|
2007-06-08 16:10:03 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure WriteToRegistry(AObject: TObject; const AKey: String; ARootKey: HKEY);
|
|
|
|
begin
|
2010-04-13 12:04:05 +02:00
|
|
|
with TX2UtPersistRegistry.Create do
|
2007-06-08 16:10:03 +02:00
|
|
|
try
|
|
|
|
RootKey := ARootKey;
|
|
|
|
Key := AKey;
|
|
|
|
Write(AObject);
|
|
|
|
finally
|
2010-04-13 12:04:05 +02:00
|
|
|
Free;
|
2007-06-08 16:10:03 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ TX2UtPersistRegistry }
|
2010-04-13 12:04:05 +02:00
|
|
|
constructor TX2UtPersistRegistry.Create;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
|
|
|
inherited;
|
2007-08-10 14:48:00 +02:00
|
|
|
|
2007-06-08 16:10:03 +02:00
|
|
|
FRootKey := HKEY_CURRENT_USER;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistry.CreateFiler(AIsReader: Boolean): IX2PersistFiler;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
Result := TX2UtPersistRegistryFiler.Create(AIsReader, Self.RootKey, Self.Key);
|
2007-06-08 16:10:03 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
{ TX2UtPersistRegistry }
|
|
|
|
constructor TX2UtPersistRegistryFiler.Create(AIsReader: Boolean; ARootKey: HKEY; const AKey: String);
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
inherited Create(AIsReader);
|
2007-06-08 16:10:03 +02:00
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
if AIsReader then
|
2007-06-08 16:10:03 +02:00
|
|
|
FRegistry := TRegistry.Create(KEY_READ)
|
|
|
|
else
|
2010-04-13 12:04:05 +02:00
|
|
|
FRegistry := TRegistry.Create;
|
2007-06-08 16:10:03 +02:00
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
FRegistry.RootKey := ARootKey;
|
|
|
|
FKey := AKey;
|
|
|
|
|
|
|
|
OpenKey('');
|
2007-06-08 16:10:03 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2010-04-13 12:04:05 +02:00
|
|
|
destructor TX2UtPersistRegistryFiler.Destroy;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
|
|
|
FreeAndNil(FRegistry);
|
2007-08-10 14:48:00 +02:00
|
|
|
|
|
|
|
inherited;
|
2007-06-08 16:10:03 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.OpenKey(const ANewKey: String): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
var
|
|
|
|
keyName: String;
|
|
|
|
sectionIndex: Integer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
keyName := Self.Key;
|
|
|
|
if (Length(keyName) > 0) and (keyName[Length(keyName)] = RegistrySeparator) then
|
|
|
|
SetLength(keyName, Pred(Length(keyName)));
|
|
|
|
|
|
|
|
|
|
|
|
for sectionIndex := 0 to Pred(Sections.Count) do
|
|
|
|
keyName := keyName + RegistrySeparator + Sections[sectionIndex];
|
|
|
|
|
|
|
|
if Length(ANewKey) > 0 then
|
|
|
|
keyName := keyName + RegistrySeparator + ANewKey;
|
|
|
|
|
|
|
|
|
|
|
|
if Length(keyName) > 0 then
|
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
if IsReader then
|
2007-06-08 16:10:03 +02:00
|
|
|
Result := FRegistry.OpenKeyReadOnly(keyName)
|
|
|
|
else
|
|
|
|
Result := FRegistry.OpenKey(keyName, True);
|
|
|
|
end else
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.BeginSection(const AName: String): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
|
|
|
Result := OpenKey(AName);
|
|
|
|
|
|
|
|
if Result then
|
|
|
|
inherited BeginSection(AName);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2010-04-13 12:04:05 +02:00
|
|
|
procedure TX2UtPersistRegistryFiler.EndSection;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
{ Re-open the previous section }
|
|
|
|
OpenKey('');
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
procedure TX2UtPersistRegistryFiler.GetKeys(const ADest: TStrings);
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
Registry.GetValueNames(ADest);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2UtPersistRegistryFiler.GetSections(const ADest: TStrings);
|
|
|
|
begin
|
|
|
|
Registry.GetKeyNames(ADest);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2UtPersistRegistryFiler.ReadInteger(const AName: String; out AValue: Integer): Boolean;
|
|
|
|
begin
|
|
|
|
AValue := 0;
|
2007-06-08 16:10:03 +02:00
|
|
|
Result := Registry.ValueExists(AName);
|
|
|
|
if Result then
|
|
|
|
AValue := Registry.ReadInteger(AName);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.ReadFloat(const AName: String; out AValue: Extended): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
AValue := 0;
|
2007-06-08 16:10:03 +02:00
|
|
|
Result := Registry.ValueExists(AName);
|
|
|
|
if Result then
|
|
|
|
AValue := Registry.ReadFloat(AName);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.ReadStream(const AName: string; AStream: TStream): Boolean;
|
|
|
|
var
|
|
|
|
bufferSize: Integer;
|
|
|
|
buffer: PChar;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := Registry.ValueExists(AName);
|
|
|
|
if Result then
|
|
|
|
begin
|
|
|
|
bufferSize := Registry.GetDataSize(AName);
|
|
|
|
|
|
|
|
if bufferSize > 0 then
|
|
|
|
begin
|
|
|
|
AStream.Size := 0;
|
|
|
|
|
|
|
|
GetMem(buffer, bufferSize);
|
|
|
|
try
|
|
|
|
Registry.ReadBinaryData(AName, buffer^, bufferSize);
|
|
|
|
AStream.WriteBuffer(buffer^, bufferSize);
|
|
|
|
finally
|
|
|
|
FreeMem(buffer, bufferSize);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2UtPersistRegistryFiler.ReadString(const AName: String; out AValue: String): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
AValue := '';
|
2007-06-08 16:10:03 +02:00
|
|
|
Result := Registry.ValueExists(AName);
|
|
|
|
if Result then
|
2007-08-10 14:48:00 +02:00
|
|
|
begin
|
|
|
|
{ Required for conversion of integer-based booleans }
|
|
|
|
if Registry.GetDataType(AName) = rdInteger then
|
2008-01-20 15:50:26 +01:00
|
|
|
AValue := BoolToStr(Registry.ReadBool(AName), True)
|
2007-08-10 14:48:00 +02:00
|
|
|
else
|
|
|
|
AValue := Registry.ReadString(AName);
|
|
|
|
end;
|
2007-06-08 16:10:03 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.ReadInt64(const AName: String; out AValue: Int64): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
AValue := 0;
|
2007-06-08 16:10:03 +02:00
|
|
|
Result := (Registry.GetDataSize(AName) = SizeOf(AValue));
|
|
|
|
if Result then
|
|
|
|
Registry.ReadBinaryData(AName, AValue, SizeOf(AValue));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.WriteInteger(const AName: String; AValue: Integer): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
|
|
|
Registry.WriteInteger(AName, AValue);
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.WriteFloat(const AName: String; AValue: Extended): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
|
|
|
Registry.WriteFloat(AName, AValue);
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.WriteStream(const AName: string; AStream: TStream): Boolean;
|
|
|
|
var
|
|
|
|
bufferSize: Integer;
|
|
|
|
buffer: PChar;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
AStream.Position := 0;
|
|
|
|
bufferSize := AStream.Size;
|
|
|
|
|
|
|
|
if bufferSize > 0 then
|
|
|
|
begin
|
|
|
|
GetMem(buffer, bufferSize);
|
|
|
|
try
|
|
|
|
AStream.ReadBuffer(buffer^, bufferSize);
|
|
|
|
Registry.WriteBinaryData(AName, buffer^, bufferSize);
|
|
|
|
finally
|
|
|
|
FreeMem(buffer, bufferSize);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2UtPersistRegistryFiler.WriteString(const AName, AValue: String): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
|
|
|
Registry.WriteString(AName, AValue);
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
function TX2UtPersistRegistryFiler.WriteInt64(const AName: String; AValue: Int64): Boolean;
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
|
|
|
Registry.WriteBinaryData(AName, AValue, SizeOf(AValue));
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
procedure TX2UtPersistRegistryFiler.DeleteKey(const AName: string);
|
2007-06-08 16:10:03 +02:00
|
|
|
begin
|
2007-08-10 14:48:00 +02:00
|
|
|
Registry.DeleteValue(AName);
|
|
|
|
end;
|
2007-06-08 16:10:03 +02:00
|
|
|
|
|
|
|
|
2007-08-10 14:48:00 +02:00
|
|
|
procedure TX2UtPersistRegistryFiler.DeleteSection(const AName: string);
|
|
|
|
begin
|
|
|
|
Registry.DeleteKey(AName);
|
2007-06-08 16:10:03 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|