-
Notifications
You must be signed in to change notification settings - Fork 58
/
RegUtils.pas
337 lines (305 loc) · 8.62 KB
/
RegUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:gdeatz@hlmdd.com }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy (buzz@devrace.com) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit RegUtils;
{$I FIBPlus.inc}
interface
{$IFNDEF NO_REGISTRY}
uses
Windows, SysUtils, Classes, registry
{$IFDEF D6+}, Variants{$ENDIF};
procedure DefWriteToRegistry( const OtherKeys , ParamNames: array of string;
const Values: array of Variant
);
procedure WriteToRegistry(aRootKey:HKEY; const OtherKeys,ParamNames: array of string;
const Values: array of Variant
);
function ReadFromRegistry(aRootKey:HKEY;
const OtherKeys, ParamNames: array of string
):Variant;
function DefReadFromRegistry(const OtherKeys, ParamNames: array of string):Variant;
function AllSubKey(aRootKey:HKEY; const ForPath: array of string):Variant;
function DefAllSubKey(const ForPath: array of string):Variant;
function SaveRegKey(const FileName: String; const ForKey: array of string): Boolean;
function LoadRegKey(const FileName: String; const ForKey: array of string): Boolean;
function AltSaveRegKey(const FileName: String; const ForKey: array of string): Boolean;
function AltLoadRegKey(const FileName: String; const ForKey: array of string): Boolean;
function GetKeyForParValue(const aRootKey,ParName,ParValue:string):string;
{$ENDIF}
implementation
{$IFNDEF NO_REGISTRY}
uses IniFiles{,StrUtil};
{ HKEY VALUES :
HKEY_CURRENT_USER // Default
HKEY_LOCAL_MACHINE
HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_PERFORMANCE_DATA
HKEY_CURRENT_CONFIG
HKEY_DYN_DATA
}
var RegObj:TRegistry;
procedure DefWriteToRegistry( const OtherKeys ,
ParamNames: array of string;
const Values: array of Variant
);
begin
WriteToRegistry(HKEY_CURRENT_USER,OtherKeys,ParamNames,Values)
end;
procedure WriteToRegistry(aRootKey:HKEY;const OtherKeys,ParamNames: array of string;
const Values: array of Variant
);
var i,minPar:integer;
begin
with RegObj do
try
RootKey:=aRootKey;
for i:=Low(OtherKeys) to High(OtherKeys) do OpenKey(OtherKeys[i],true);
minPar:=High(ParamNames);
if minPar>High(Values) then minPar:=High(Values);
for i:=0 to minPar do
case VarType(Values[i]) of
varSmallint,varInteger {$IFDEF D6+},varWord, varLongWord,varInt64 {$ENDIF}:WriteInteger(ParamNames[i],Values[i]);
varSingle, varDouble :WriteFloat(ParamNames[i],Values[i]);
varCurrency :WriteCurrency(ParamNames[i],Values[i]);
varBoolean :WriteBool(ParamNames[i],Values[i]);
varString :WriteString(ParamNames[i],Values[i]);
varDate :WriteDateTime(ParamNames[i],Values[i]);
{$IFDEF D2009+}
varUString :WriteString(ParamNames[i],Values[i]);
{$ENDIF}
end
finally
CloseKey;
end;
end;
function ReadFromRegistry(aRootKey:HKEY;
const OtherKeys, ParamNames: array of string
):Variant;
var i,cPar:integer;
t:TRegDataType;
begin
// RegObj:=TRegistry.Create;
with RegObj do
try
Result:=false;
RootKey:=aRootKey;
for i:=Low(OtherKeys) to High(OtherKeys) do
if not OpenKey(OtherKeys[i],false) then Exit;
cPar:=High(ParamNames);
Result:=VarArrayCreate([0, 1, 0, cPar], varVariant);
for i:=0 to cPar do
try
t:= GetDataType(ParamNames[i]);
if t in [rdString,rdExpandString] then
begin
Result[1,i]:=ValueExists(ParamNames[i]);
if Result[1,i] then
Result[0,i]:=ReadString(ParamNames[i])
else
Result[0,i]:='';
end
else
if t=rdInteger then
begin
Result[0,i]:=ReadInteger(ParamNames[i]);
Result[1,i]:=true;
end;
except
Result[1,i]:=false //can't read as string
end;
finally
CloseKey;
end;
end;
function DefReadFromRegistry(const OtherKeys, ParamNames: array of string):Variant;
begin
Result:=ReadFromRegistry(HKEY_CURRENT_USER,OtherKeys,ParamNames)
end;
function AllSubKey(aRootKey:HKEY; const ForPath: array of string):Variant;
var
i:integer;
SubKeys:TStrings;
begin
SubKeys:=TStringList.Create;
Result:=false;
with RegObj do
try
Result:=false;
RootKey:=aRootKey;
for i:=Low(ForPath) to High(ForPath) do
if not OpenKey(ForPath[i],false) then Exit;
GetKeyNames(SubKeys);
if SubKeys.Count=0 then Exit;
Result:=VarArrayCreate([0,Pred(SubKeys.Count)], varOleStr);
for i:=0 to Pred(SubKeys.Count) do Result[i]:=SubKeys[i];
finally
SubKeys.Free;
CloseKey;
end;
end;
function DefAllSubKey(const ForPath: array of string):Variant;
begin
Result:=AllSubKey(HKEY_CURRENT_USER,ForPath)
end;
function SaveRegKey(const FileName: String;
const ForKey: array of string): Boolean;
var i:integer;
s:string;
begin
with RegObj do
try
RootKey:=HKEY_CURRENT_USER;
s:='';
if FileExists(FileName) then DeleteFile(FileName);
for i:=Low(ForKey) to High(ForKey) do
s:=s+'\'+ForKey[i];
Result:=SaveKey(s,FileName);
finally
CloseKey;
end;
end;
function LoadRegKey(const FileName: String; const ForKey: array of string): Boolean;
var i:integer;
s:string;
begin
with RegObj do
try
RootKey:=HKEY_CURRENT_USER;
s:='';
for i:=Low(ForKey) to High(ForKey) do
s:=s+'\'+ForKey[i];
Result:=ReplaceKey(s,FileName,'');
finally
CloseKey;
end;
end;
function AltSaveRegKey(const FileName: String;
const ForKey: array of string): Boolean;
var v:Variant;
i,c:integer;
Ini:TIniFile;
PathKey:string;
pv:string;
begin
v:=DefAllSubKey(ForKey);
if VarType(v)=varBoolean then
Result:=false
else
with RegObj do
begin
PathKey:='';
for i:=Low(ForKey) to High(ForKey) do
PathKey:=PathKey+'\'+ForKey[i];
if FileExists(FileName) then DeleteFile(FileName);
Ini:=TIniFile.Create(FileName);
try
c:=VarArrayHighBound(v, 1);
for i :=0 to c do
begin
if OpenKey(PathKey+'\'+v[i],false) then
begin
Ini.WriteString(v[i], 'Name' ,ReadString('Name') );
pv:=StringReplace(ReadString('Params'),#13#10,'#',[rfReplaceAll]);
Ini.WriteString(v[i], 'Params', pv);
end;
end;
Result:=true;
finally
Ini.Free
end;
end;
end;
function AltLoadRegKey(const FileName: String;
const ForKey: array of string): Boolean;
var v:Variant;
i,c:integer;
Ini:TIniFile;
PathKey:string;
ts:TStrings;
begin
v:=DefAllSubKey(ForKey);
with RegObj do
begin
PathKey:='';
for i:=Low(ForKey) to High(ForKey) do
PathKey:=PathKey+'\'+ForKey[i];
Ini:=TIniFile.Create(FileName);
ts :=TStringList.Create;
try
if VarType(v)<>varBoolean then
begin
c:=VarArrayHighBound(v, 1);
for i :=0 to c do
if OpenKey(PathKey+'\'+v[i],false) then
DeleteKey(PathKey+'\'+v[i]);
end;
Ini.ReadSections(ts);
c:=ts.Count-1;
for i :=0 to c do
if OpenKey(PathKey+'\'+ts[i],true) then
begin
WriteString('Name',Ini.ReadString(ts[i],'Name',''));
WriteString('Params',
StringReplace(Ini.ReadString(ts[i],'Params',''),'#',#13#10,[rfReplaceAll])
);
end;
Result:=true;
finally
ts.Free;
Ini.Free;
CloseKey
end;
end;
end;
function GetKeyForParValue(const aRootKey,ParName,ParValue:string):string;
var v:variant;
i,c:integer;
pv:string;
begin
Result:='';
v:=DefAllSubKey(aRootKey);
if VarType(v)=varBoolean then Exit;
c:=VarArrayHighBound(v, 1);
with RegObj do
try
for i:=0 to c do
if OpenKey(aRootKey+'\'+v[i],false) then
begin
pv:=ReadString(ParName);
if pv=ParValue then
begin
Result:=v[i]; Exit;
end;
CloseKey
end;
finally
CloseKey
end;
end;
{$ENDIF}
initialization
{$IFNDEF NO_REGISTRY}
RegObj:=TRegistry.Create;
{$ENDIF}
finalization
{$IFNDEF NO_REGISTRY}
RegObj.Free
{$ENDIF}
end.