1 | {******************************************************************************
|
---|
2 | TRegistry
|
---|
3 | ******************************************************************************}
|
---|
4 |
|
---|
5 | Procedure TRegistry.SysRegCreate;
|
---|
6 | begin
|
---|
7 | FStringSizeIncludesNull:=True;
|
---|
8 | end;
|
---|
9 |
|
---|
10 | Procedure TRegistry.SysRegfree;
|
---|
11 |
|
---|
12 | begin
|
---|
13 | end;
|
---|
14 |
|
---|
15 | Function PrepKey(Const S : String) : pChar;
|
---|
16 |
|
---|
17 | begin
|
---|
18 | Result:=PChar(S);
|
---|
19 | If Result^='\' then
|
---|
20 | Inc(Result);
|
---|
21 | end;
|
---|
22 |
|
---|
23 | Function RelativeKey(Const S : String) : Boolean;
|
---|
24 |
|
---|
25 | begin
|
---|
26 | Result:=(S='') or (S[1]<>'\')
|
---|
27 | end;
|
---|
28 |
|
---|
29 |
|
---|
30 | function TRegistry.sysCreateKey(const Key: String): Boolean;
|
---|
31 | Var
|
---|
32 | P: PChar;
|
---|
33 | Disposition: Dword;
|
---|
34 | Handle: HKEY;
|
---|
35 | SecurityAttributes: Pointer; //LPSECURITY_ATTRIBUTES;
|
---|
36 |
|
---|
37 | begin
|
---|
38 | SecurityAttributes := Nil;
|
---|
39 | P:=PrepKey(Key);
|
---|
40 | Result:=RegCreateKeyExA(GetBaseKey(RelativeKey(Key)),
|
---|
41 | P,
|
---|
42 | 0,
|
---|
43 | '',
|
---|
44 | REG_OPTION_NON_VOLATILE,
|
---|
45 | KEY_ALL_ACCESS,
|
---|
46 | SecurityAttributes,
|
---|
47 | Handle,
|
---|
48 | @Disposition) = ERROR_SUCCESS;
|
---|
49 | RegCloseKey(Handle);
|
---|
50 | end;
|
---|
51 |
|
---|
52 | function TRegistry.DeleteKey(const Key: String): Boolean;
|
---|
53 |
|
---|
54 | Var
|
---|
55 | P: PChar;
|
---|
56 | begin
|
---|
57 | P:=PRepKey(Key);
|
---|
58 | Result:=RegDeleteKeyA(GetBaseKey(RelativeKey(Key)),P)=ERROR_SUCCESS;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | function TRegistry.DeleteValue(const Name: String): Boolean;
|
---|
62 | begin
|
---|
63 | Result := RegDeleteValueA(fCurrentKey, @Name[1]) = ERROR_SUCCESS;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | function TRegistry.SysGetData(const Name: String; Buffer: Pointer;
|
---|
67 | BufSize: Integer; var RegData: TRegDataType): Integer;
|
---|
68 | Var
|
---|
69 | P: PChar;
|
---|
70 | RD : DWord;
|
---|
71 |
|
---|
72 | begin
|
---|
73 | P := PChar(Name);
|
---|
74 | If RegQueryValueExA(fCurrentKey,P,Nil,
|
---|
75 | @RD,Buffer,lpdword(@BufSize))<>ERROR_SUCCESS Then
|
---|
76 | Result:=-1
|
---|
77 | else
|
---|
78 | begin
|
---|
79 | If (RD=REG_SZ) then
|
---|
80 | RegData:=rdString
|
---|
81 | else if (RD=REG_EXPAND_SZ) then
|
---|
82 | Regdata:=rdExpandString
|
---|
83 | else if (RD=REG_DWORD) then
|
---|
84 | RegData:=rdInteger
|
---|
85 | else if (RD=REG_BINARY) then
|
---|
86 | RegData:=rdBinary
|
---|
87 | else
|
---|
88 | RegData:=rdUnknown;
|
---|
89 | Result:=BufSize;
|
---|
90 | end;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | function TRegistry.GetDataInfo(const ValueName: String; var Value: TRegDataInfo): Boolean;
|
---|
94 |
|
---|
95 | Var
|
---|
96 | P: PChar;
|
---|
97 |
|
---|
98 | begin
|
---|
99 | P:=PChar(ValueName);
|
---|
100 | With Value do
|
---|
101 | Result:=RegQueryValueExA(fCurrentKey,P,Nil,lpdword(@RegData),Nil,lpdword(@DataSize))=ERROR_SUCCESS;
|
---|
102 | If Not Result Then
|
---|
103 | begin
|
---|
104 | Value.RegData := rdUnknown;
|
---|
105 | Value.DataSize := 0
|
---|
106 | end
|
---|
107 | end;
|
---|
108 |
|
---|
109 |
|
---|
110 | function TRegistry.GetKey(const Key: String): HKEY;
|
---|
111 | var
|
---|
112 | S : string;
|
---|
113 | Rel : Boolean;
|
---|
114 | begin
|
---|
115 | Result:=0;
|
---|
116 | S:=Key;
|
---|
117 | Rel:=RelativeKey(S);
|
---|
118 | if not(Rel) then
|
---|
119 | Delete(S,1,1);
|
---|
120 | {$ifdef WinCE}
|
---|
121 | RegOpenKeyEx(GetBaseKey(Rel),PWideChar(WideString(S)),0,FAccess,Result);
|
---|
122 | {$else WinCE}
|
---|
123 | RegOpenKeyEx(GetBaseKey(Rel),PChar(S),0,FAccess,Result);
|
---|
124 | {$endif WinCE}
|
---|
125 | end;
|
---|
126 |
|
---|
127 |
|
---|
128 | function TRegistry.GetKeyInfo(var Value: TRegKeyInfo): Boolean;
|
---|
129 | var
|
---|
130 | winFileTime: Windows.FILETIME;
|
---|
131 | sysTime: TSystemTime;
|
---|
132 | begin
|
---|
133 | FillChar(Value, SizeOf(Value), 0);
|
---|
134 | With Value do
|
---|
135 | Result:=RegQueryInfoKeyA(CurrentKey,nil,nil,nil,lpdword(@NumSubKeys),
|
---|
136 | lpdword(@MaxSubKeyLen),nil,lpdword(@NumValues),lpdword(@MaxValueLen),
|
---|
137 | lpdword(@MaxDataLen),nil,@winFileTime)=ERROR_SUCCESS;
|
---|
138 | if Result then
|
---|
139 | begin
|
---|
140 | FileTimeToSystemTime(@winFileTime, @sysTime);
|
---|
141 | Value.FileTime := SystemTimeToDateTime(sysTime);
|
---|
142 | end;
|
---|
143 | end;
|
---|
144 |
|
---|
145 |
|
---|
146 | function TRegistry.KeyExists(const Key: string): Boolean;
|
---|
147 | var
|
---|
148 | KeyHandle : HKEY;
|
---|
149 | OldAccess : LONG;
|
---|
150 | begin
|
---|
151 | Result:=false;
|
---|
152 | OldAccess:=FAccess;
|
---|
153 | try
|
---|
154 | FAccess:=KEY_QUERY_VALUE or KEY_ENUMERATE_SUB_KEYS or STANDARD_RIGHTS_READ;
|
---|
155 | KeyHandle:=GetKey(Key);
|
---|
156 | if KeyHandle<>0 then
|
---|
157 | begin
|
---|
158 | RegCloseKey(KeyHandle);
|
---|
159 | Result:=true;
|
---|
160 | end;
|
---|
161 | finally
|
---|
162 | FAccess:=OldAccess;
|
---|
163 | end;
|
---|
164 | end;
|
---|
165 |
|
---|
166 |
|
---|
167 | function TRegistry.LoadKey(const Key, FileName: string): Boolean;
|
---|
168 | begin
|
---|
169 | Result := False;
|
---|
170 | end;
|
---|
171 |
|
---|
172 |
|
---|
173 | function TRegistry.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
|
---|
174 |
|
---|
175 | Var
|
---|
176 | P: PChar;
|
---|
177 | Handle: HKEY;
|
---|
178 | Disposition: Integer;
|
---|
179 | SecurityAttributes: Pointer; //LPSECURITY_ATTRIBUTES;
|
---|
180 |
|
---|
181 | begin
|
---|
182 | SecurityAttributes := Nil;
|
---|
183 | P:=PrepKey(Key);
|
---|
184 | If CanCreate then
|
---|
185 | begin
|
---|
186 | Handle:=0;
|
---|
187 | Result:=RegCreateKeyExA(GetBaseKey(RelativeKey(Key)),P,0,'',
|
---|
188 |
|
---|
189 | REG_OPTION_NON_VOLATILE,
|
---|
190 | fAccess,SecurityAttributes,Handle,
|
---|
191 | pdword(@Disposition))=ERROR_SUCCESS
|
---|
192 |
|
---|
193 | end
|
---|
194 | else
|
---|
195 | Result:=RegOpenKeyExA(GetBaseKey(RelativeKey(Key)),
|
---|
196 | P,0,fAccess,Handle)=ERROR_SUCCESS;
|
---|
197 | If Result then
|
---|
198 | fCurrentKey:=Handle;
|
---|
199 | end;
|
---|
200 |
|
---|
201 | function TRegistry.OpenKeyReadOnly(const Key: string): Boolean;
|
---|
202 |
|
---|
203 | Var
|
---|
204 | P: PChar;
|
---|
205 | Handle: HKEY;
|
---|
206 |
|
---|
207 | begin
|
---|
208 | P:=PrepKey(Key);
|
---|
209 | Result := RegOpenKeyExA(GetBaseKey(RelativeKey(Key)),P,0,KEY_READ,Handle) = 0;
|
---|
210 | If Result Then
|
---|
211 | fCurrentKey := Handle;
|
---|
212 | end;
|
---|
213 |
|
---|
214 | function TRegistry.RegistryConnect(const UNCName: string): Boolean;
|
---|
215 | begin
|
---|
216 | Result := False;
|
---|
217 | end;
|
---|
218 |
|
---|
219 | function TRegistry.ReplaceKey(const Key, FileName, BackUpFileName: string): Boolean;
|
---|
220 | begin
|
---|
221 | Result := False;
|
---|
222 | end;
|
---|
223 |
|
---|
224 | function TRegistry.RestoreKey(const Key, FileName: string): Boolean;
|
---|
225 | begin
|
---|
226 | Result := False;
|
---|
227 | end;
|
---|
228 |
|
---|
229 | function TRegistry.SaveKey(const Key, FileName: string): Boolean;
|
---|
230 | begin
|
---|
231 | Result := False;
|
---|
232 | end;
|
---|
233 |
|
---|
234 | function TRegistry.UnLoadKey(const Key: string): Boolean;
|
---|
235 | begin
|
---|
236 | Result := false;
|
---|
237 | end;
|
---|
238 |
|
---|
239 | function TRegistry.ValueExists(const Name: string): Boolean;
|
---|
240 |
|
---|
241 | var
|
---|
242 | Info : TRegDataInfo;
|
---|
243 |
|
---|
244 | begin
|
---|
245 | Result:=GetDataInfo(Name,Info);
|
---|
246 | end;
|
---|
247 |
|
---|
248 | procedure TRegistry.CloseKey;
|
---|
249 | begin
|
---|
250 | If (CurrentKey<>0) then
|
---|
251 | begin
|
---|
252 | if LazyWrite then
|
---|
253 | RegCloseKey(CurrentKey)
|
---|
254 | else
|
---|
255 | RegFlushKey(CurrentKey);
|
---|
256 | fCurrentKey:=0;
|
---|
257 | end
|
---|
258 | end;
|
---|
259 |
|
---|
260 | procedure TRegistry.CloseKey(key:HKEY);
|
---|
261 | begin
|
---|
262 | RegCloseKey(CurrentKey)
|
---|
263 | end;
|
---|
264 |
|
---|
265 | procedure TRegistry.ChangeKey(Value: HKey; const Path: String);
|
---|
266 | begin
|
---|
267 | CloseKey;
|
---|
268 | FCurrentKey:=Value;
|
---|
269 | FCurrentPath:=Path;
|
---|
270 | end;
|
---|
271 |
|
---|
272 | procedure TRegistry.GetKeyNames(Strings: TStrings);
|
---|
273 |
|
---|
274 | Var
|
---|
275 | L : Cardinal;
|
---|
276 | I: Integer;
|
---|
277 | Info: TRegKeyInfo;
|
---|
278 | P : PChar;
|
---|
279 |
|
---|
280 | begin
|
---|
281 | Strings.Clear;
|
---|
282 | if GetKeyInfo(Info) then
|
---|
283 | begin
|
---|
284 | L:=Info.MaxSubKeyLen+1;
|
---|
285 | GetMem(P,L);
|
---|
286 | Try
|
---|
287 | for I:=0 to Info.NumSubKeys-1 do
|
---|
288 | begin
|
---|
289 | L:=Info.MaxSubKeyLen+1;
|
---|
290 | RegEnumKeyExA(CurrentKey,I,P,L,Nil,Nil,Nil,Nil);
|
---|
291 | Strings.Add(StrPas(P));
|
---|
292 | end;
|
---|
293 | Finally
|
---|
294 | FreeMem(P);
|
---|
295 | end;
|
---|
296 | end;
|
---|
297 | end;
|
---|
298 |
|
---|
299 | procedure TRegistry.GetValueNames(Strings: TStrings);
|
---|
300 |
|
---|
301 | Var
|
---|
302 | L : Cardinal;
|
---|
303 | I: Integer;
|
---|
304 | Info: TRegKeyInfo;
|
---|
305 | P : PChar;
|
---|
306 |
|
---|
307 | begin
|
---|
308 | Strings.Clear;
|
---|
309 | if GetKeyInfo(Info) then
|
---|
310 | begin
|
---|
311 | L:=Info.MaxValueLen+1;
|
---|
312 | GetMem(P,L);
|
---|
313 | Try
|
---|
314 | for I:=0 to Info.NumValues-1 do
|
---|
315 | begin
|
---|
316 | L:=Info.MaxValueLen+1;
|
---|
317 | RegEnumValueA(CurrentKey,I,P,L,Nil,Nil,Nil,Nil);
|
---|
318 | Strings.Add(StrPas(P));
|
---|
319 | end;
|
---|
320 | Finally
|
---|
321 | FreeMem(P);
|
---|
322 | end;
|
---|
323 | end;
|
---|
324 |
|
---|
325 | end;
|
---|
326 |
|
---|
327 | Function TRegistry.SysPutData(const Name: string; Buffer: Pointer;
|
---|
328 | BufSize: Integer; RegData: TRegDataType) : Boolean;
|
---|
329 |
|
---|
330 | Var
|
---|
331 | P: PChar;
|
---|
332 | RegDataType: DWORD;
|
---|
333 |
|
---|
334 | begin
|
---|
335 | Case RegData of
|
---|
336 | rdUnknown : RegDataType:=REG_NONE;
|
---|
337 | rdString : RegDataType:=REG_SZ;
|
---|
338 | rdExpandString : RegDataType:=REG_EXPAND_SZ;
|
---|
339 | rdInteger : RegDataType:=REG_DWORD;
|
---|
340 | rdBinary : RegDataType:=REG_BINARY;
|
---|
341 | end;
|
---|
342 | P:=PChar(Name);
|
---|
343 | Result:=RegSetValueExA(fCurrentKey,P,0,RegDataType,Buffer,BufSize)=ERROR_SUCCESS;
|
---|
344 | end;
|
---|
345 |
|
---|
346 | procedure TRegistry.RenameValue(const OldName, NewName: string);
|
---|
347 |
|
---|
348 | var
|
---|
349 | L: Integer;
|
---|
350 | InfoO,InfoN : TRegDataInfo;
|
---|
351 | D : TRegDataType;
|
---|
352 | P: PChar;
|
---|
353 |
|
---|
354 | begin
|
---|
355 | If GetDataInfo(OldName,InfoO) and Not GetDataInfo(NewName,InfoN) then
|
---|
356 | begin
|
---|
357 | L:=InfoO.DataSize;
|
---|
358 | if L>0 then
|
---|
359 | begin
|
---|
360 | GetMem(P,L);
|
---|
361 | try
|
---|
362 | L:=GetData(OldName,P,L,D);
|
---|
363 | If SysPutData(NewName,P,L,D) then
|
---|
364 | DeleteValue(OldName);
|
---|
365 | finally
|
---|
366 | FreeMem(P);
|
---|
367 | end;
|
---|
368 | end;
|
---|
369 | end;
|
---|
370 | end;
|
---|
371 |
|
---|
372 | procedure TRegistry.SetCurrentKey(Value: HKEY);
|
---|
373 | begin
|
---|
374 | fCurrentKey := Value;
|
---|
375 | end;
|
---|
376 |
|
---|
377 | procedure TRegistry.SetRootKey(Value: HKEY);
|
---|
378 | begin
|
---|
379 | fRootKey := Value;
|
---|
380 | end;
|
---|
381 |
|
---|