/source/utils/RegistryUtils.pas
Pascal | 154 lines | 126 code | 28 blank | 0 comment | 3 complexity | f43a09b0e4cccff0246d49bf35dbbbfa MD5 | raw file
1unit RegistryUtils; 2 3interface 4 5uses 6 Classes, Registry; 7 8const 9 ERROR_SUCCESS = 0; 10 11 ERROR_REGISTRY_NOTASSIGNED = ERROR_SUCCESS - 1; 12 ERROR_REGISTRY_OPENKEY = ERROR_SUCCESS - 2; 13 ERROR_REGISTRY_READWRITE = ERROR_SUCCESS - 3; 14 15function RegistryReadValue(Section, Key : String; DefaultValue : Variant; AType : TVarType) : Variant; 16function RegistryReadSections (Strings : TStrings; Key : string) : Boolean; 17 18function RegistryReadBoolean(Section, Key : String; DefaultValue : Boolean = false) : Boolean; 19function RegistryReadInteger(Section, Key : String; DefaultValue : Integer = 0) : Integer; 20function RegistryReadString(Section, Key : String; DefaultValue : String = '') : String; 21function RegistryReadFloat(Section, Key : String; DefaultValue : Double = 0.0) : Double; 22function RegistryReadDateTime(Section, Key : String; DefaultValue : TDateTime) : TDateTime; 23 24procedure RegistryWriteValue(Section, Key : String; Value : Variant; AType : TVarType); 25 26procedure RegistryWriteBoolean(Section, Key : String; Value : Boolean); 27procedure RegistryWriteInteger(Section, Key : String; Value : Integer); 28procedure RegistryWriteString(Section, Key : String; Value : String); 29procedure RegistryWriteFloat(Section, Key : String; Value : Double); 30procedure RegistryWriteDateTime(Section, Key : String; Value : TDateTime); 31 32var 33 TheRegistry : TRegIniFile; 34 35implementation 36 37uses 38 Windows, RegistryKeys, Variants, SysUtils; 39 40function RegistryReadBoolean(Section, Key : String; DefaultValue : Boolean = false) : Boolean; 41begin 42 result := RegistryReadValue(Section, Key, DefaultValue, varBoolean); 43end; 44 45function RegistryReadInteger(Section, Key : String; DefaultValue : Integer = 0) : Integer; 46begin 47 result := RegistryReadValue(Section, Key, DefaultValue, varInteger); 48end; 49 50function RegistryReadDateTime(Section, Key : String; DefaultValue : TDateTime) : TDateTime; 51begin 52 result := RegistryReadValue(Section, Key, DefaultValue, varDate); 53end; 54 55function RegistryReadSections (Strings : TStrings; Key : string) : Boolean; 56var 57 s : string; 58begin 59 with TheRegistry do 60 try 61 s := Filename; 62 if s = '' then 63 s := REGISTRY_KEY_PRODUCT; 64 Result := OpenKey (Key, False); 65 if Result then 66 begin 67 ReadSections (Strings); 68 CloseKey; 69 OpenKey (s, False) 70 end 71 except 72 Result := False 73 end; 74end; 75 76function RegistryReadString(Section, Key : String; DefaultValue : String = '') : String; 77begin 78 result := RegistryReadValue(Section, Key, DefaultValue, varUString); 79end; 80 81function RegistryReadFloat(Section, Key : String; DefaultValue : Double = 0.0) : Double; 82begin 83 result := RegistryReadValue(Section, Key, DefaultValue, varDouble); 84end; 85 86function RegistryReadValue(Section, Key : String; DefaultValue : Variant; AType : TVarType) : Variant; 87begin 88 case AType of 89 varBoolean : 90 result := TheRegistry.ReadBool(Section, Key, DefaultValue); 91 varInteger : 92 result := TheRegistry.ReadInteger(Section, Key, DefaultValue); 93 varUString : 94 result := TheRegistry.ReadString(Section, Key, DefaultValue); 95 varSingle, varDouble : 96 result := StrToFloat(TheRegistry.ReadString(Section, Key, FloatToStr(DefaultValue))); 97 varDate : 98 result := StrToDate(TheRegistry.ReadString(Section, Key, DateToStr(DefaultValue))); 99 end; 100end; 101 102procedure RegistryWriteBoolean(Section, Key : String; Value : Boolean); 103begin 104 RegistryWriteValue(Section, Key, Value, varBoolean); 105end; 106 107procedure RegistryWriteInteger(Section, Key : String; Value : Integer); 108begin 109 RegistryWriteValue(Section, Key, Value, varInteger); 110end; 111 112procedure RegistryWriteString(Section, Key : String; Value : String); 113begin 114 RegistryWriteValue(Section, Key, Value, varUString); 115end; 116 117procedure RegistryWriteFloat(Section, Key : String; Value : Double); 118begin 119 RegistryWriteValue(Section, Key, Value, varDouble); 120end; 121 122procedure RegistryWriteDateTime(Section, Key : String; Value : TDateTime); 123begin 124 RegistryWriteValue(Section, Key, Value, varDate); 125end; 126 127 128procedure RegistryWriteValue(Section, Key : String; Value : Variant; AType : TVarType); 129begin 130 case AType of 131 varBoolean : 132 TheRegistry.WriteBool(Section, Key, Value); 133 varInteger : 134 TheRegistry.WriteInteger(Section, Key, Value); 135 varUString : 136 TheRegistry.WriteString(Section, Key, Value); 137 varSingle, varDouble : 138 TheRegistry.WriteString(Section, Key, FloatToStr(Value)); 139 varDate : 140 TheRegistry.WriteString(Section, Key, DateToStr(Value)); 141 end; 142end; 143 144initialization 145 TheRegistry := TRegIniFile.Create; 146 TheRegistry.OpenKey(REGISTRY_KEY_PRODUCT, true); 147 148finalization 149 if Assigned (TheRegistry) then begin 150 TheRegistry.CloseKey; 151 TheRegistry.Free 152 end; 153 154end.