/source/utils/RegistryUtils.pas
http://sales-purchases.googlecode.com/ · Pascal · 154 lines · 126 code · 28 blank · 0 comment · 3 complexity · f43a09b0e4cccff0246d49bf35dbbbfa MD5 · raw file
- unit RegistryUtils;
-
- interface
-
- uses
- Classes, Registry;
-
- const
- ERROR_SUCCESS = 0;
-
- ERROR_REGISTRY_NOTASSIGNED = ERROR_SUCCESS - 1;
- ERROR_REGISTRY_OPENKEY = ERROR_SUCCESS - 2;
- ERROR_REGISTRY_READWRITE = ERROR_SUCCESS - 3;
-
- function RegistryReadValue(Section, Key : String; DefaultValue : Variant; AType : TVarType) : Variant;
- function RegistryReadSections (Strings : TStrings; Key : string) : Boolean;
-
- function RegistryReadBoolean(Section, Key : String; DefaultValue : Boolean = false) : Boolean;
- function RegistryReadInteger(Section, Key : String; DefaultValue : Integer = 0) : Integer;
- function RegistryReadString(Section, Key : String; DefaultValue : String = '') : String;
- function RegistryReadFloat(Section, Key : String; DefaultValue : Double = 0.0) : Double;
- function RegistryReadDateTime(Section, Key : String; DefaultValue : TDateTime) : TDateTime;
-
- procedure RegistryWriteValue(Section, Key : String; Value : Variant; AType : TVarType);
-
- procedure RegistryWriteBoolean(Section, Key : String; Value : Boolean);
- procedure RegistryWriteInteger(Section, Key : String; Value : Integer);
- procedure RegistryWriteString(Section, Key : String; Value : String);
- procedure RegistryWriteFloat(Section, Key : String; Value : Double);
- procedure RegistryWriteDateTime(Section, Key : String; Value : TDateTime);
-
- var
- TheRegistry : TRegIniFile;
-
- implementation
-
- uses
- Windows, RegistryKeys, Variants, SysUtils;
-
- function RegistryReadBoolean(Section, Key : String; DefaultValue : Boolean = false) : Boolean;
- begin
- result := RegistryReadValue(Section, Key, DefaultValue, varBoolean);
- end;
-
- function RegistryReadInteger(Section, Key : String; DefaultValue : Integer = 0) : Integer;
- begin
- result := RegistryReadValue(Section, Key, DefaultValue, varInteger);
- end;
-
- function RegistryReadDateTime(Section, Key : String; DefaultValue : TDateTime) : TDateTime;
- begin
- result := RegistryReadValue(Section, Key, DefaultValue, varDate);
- end;
-
- function RegistryReadSections (Strings : TStrings; Key : string) : Boolean;
- var
- s : string;
- begin
- with TheRegistry do
- try
- s := Filename;
- if s = '' then
- s := REGISTRY_KEY_PRODUCT;
- Result := OpenKey (Key, False);
- if Result then
- begin
- ReadSections (Strings);
- CloseKey;
- OpenKey (s, False)
- end
- except
- Result := False
- end;
- end;
-
- function RegistryReadString(Section, Key : String; DefaultValue : String = '') : String;
- begin
- result := RegistryReadValue(Section, Key, DefaultValue, varUString);
- end;
-
- function RegistryReadFloat(Section, Key : String; DefaultValue : Double = 0.0) : Double;
- begin
- result := RegistryReadValue(Section, Key, DefaultValue, varDouble);
- end;
-
- function RegistryReadValue(Section, Key : String; DefaultValue : Variant; AType : TVarType) : Variant;
- begin
- case AType of
- varBoolean :
- result := TheRegistry.ReadBool(Section, Key, DefaultValue);
- varInteger :
- result := TheRegistry.ReadInteger(Section, Key, DefaultValue);
- varUString :
- result := TheRegistry.ReadString(Section, Key, DefaultValue);
- varSingle, varDouble :
- result := StrToFloat(TheRegistry.ReadString(Section, Key, FloatToStr(DefaultValue)));
- varDate :
- result := StrToDate(TheRegistry.ReadString(Section, Key, DateToStr(DefaultValue)));
- end;
- end;
-
- procedure RegistryWriteBoolean(Section, Key : String; Value : Boolean);
- begin
- RegistryWriteValue(Section, Key, Value, varBoolean);
- end;
-
- procedure RegistryWriteInteger(Section, Key : String; Value : Integer);
- begin
- RegistryWriteValue(Section, Key, Value, varInteger);
- end;
-
- procedure RegistryWriteString(Section, Key : String; Value : String);
- begin
- RegistryWriteValue(Section, Key, Value, varUString);
- end;
-
- procedure RegistryWriteFloat(Section, Key : String; Value : Double);
- begin
- RegistryWriteValue(Section, Key, Value, varDouble);
- end;
-
- procedure RegistryWriteDateTime(Section, Key : String; Value : TDateTime);
- begin
- RegistryWriteValue(Section, Key, Value, varDate);
- end;
-
-
- procedure RegistryWriteValue(Section, Key : String; Value : Variant; AType : TVarType);
- begin
- case AType of
- varBoolean :
- TheRegistry.WriteBool(Section, Key, Value);
- varInteger :
- TheRegistry.WriteInteger(Section, Key, Value);
- varUString :
- TheRegistry.WriteString(Section, Key, Value);
- varSingle, varDouble :
- TheRegistry.WriteString(Section, Key, FloatToStr(Value));
- varDate :
- TheRegistry.WriteString(Section, Key, DateToStr(Value));
- end;
- end;
-
- initialization
- TheRegistry := TRegIniFile.Create;
- TheRegistry.OpenKey(REGISTRY_KEY_PRODUCT, true);
-
- finalization
- if Assigned (TheRegistry) then begin
- TheRegistry.CloseKey;
- TheRegistry.Free
- end;
-
- end.