source: trunk/Packages/fcl-registry/winreg.inc

Last change on this file was 13, checked in by chronos, 10 years ago
  • Added: Basic parsing of "Depends on" expressions.
File size: 7.9 KB
Line 
1{******************************************************************************
2 TRegistry
3 ******************************************************************************}
4
5Procedure TRegistry.SysRegCreate;
6begin
7 FStringSizeIncludesNull:=True;
8end;
9
10Procedure TRegistry.SysRegfree;
11
12begin
13end;
14
15Function PrepKey(Const S : String) : pChar;
16
17begin
18 Result:=PChar(S);
19 If Result^='\' then
20 Inc(Result);
21end;
22
23Function RelativeKey(Const S : String) : Boolean;
24
25begin
26 Result:=(S='') or (S[1]<>'\')
27end;
28
29
30function TRegistry.sysCreateKey(const Key: String): Boolean;
31Var
32 P: PChar;
33 Disposition: Dword;
34 Handle: HKEY;
35 SecurityAttributes: Pointer; //LPSECURITY_ATTRIBUTES;
36
37begin
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);
50end;
51
52function TRegistry.DeleteKey(const Key: String): Boolean;
53
54Var
55 P: PChar;
56begin
57 P:=PRepKey(Key);
58 Result:=RegDeleteKeyA(GetBaseKey(RelativeKey(Key)),P)=ERROR_SUCCESS;
59end;
60
61function TRegistry.DeleteValue(const Name: String): Boolean;
62begin
63 Result := RegDeleteValueA(fCurrentKey, @Name[1]) = ERROR_SUCCESS;
64end;
65
66function TRegistry.SysGetData(const Name: String; Buffer: Pointer;
67 BufSize: Integer; var RegData: TRegDataType): Integer;
68Var
69 P: PChar;
70 RD : DWord;
71
72begin
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;
91end;
92
93function TRegistry.GetDataInfo(const ValueName: String; var Value: TRegDataInfo): Boolean;
94
95Var
96 P: PChar;
97
98begin
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
107end;
108
109
110function TRegistry.GetKey(const Key: String): HKEY;
111var
112 S : string;
113 Rel : Boolean;
114begin
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}
125end;
126
127
128function TRegistry.GetKeyInfo(var Value: TRegKeyInfo): Boolean;
129var
130 winFileTime: Windows.FILETIME;
131 sysTime: TSystemTime;
132begin
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;
143end;
144
145
146function TRegistry.KeyExists(const Key: string): Boolean;
147var
148 KeyHandle : HKEY;
149 OldAccess : LONG;
150begin
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;
164end;
165
166
167function TRegistry.LoadKey(const Key, FileName: string): Boolean;
168begin
169 Result := False;
170end;
171
172
173function TRegistry.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
174
175Var
176 P: PChar;
177 Handle: HKEY;
178 Disposition: Integer;
179 SecurityAttributes: Pointer; //LPSECURITY_ATTRIBUTES;
180
181begin
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;
199end;
200
201function TRegistry.OpenKeyReadOnly(const Key: string): Boolean;
202
203Var
204 P: PChar;
205 Handle: HKEY;
206
207begin
208 P:=PrepKey(Key);
209 Result := RegOpenKeyExA(GetBaseKey(RelativeKey(Key)),P,0,KEY_READ,Handle) = 0;
210 If Result Then
211 fCurrentKey := Handle;
212end;
213
214function TRegistry.RegistryConnect(const UNCName: string): Boolean;
215begin
216 Result := False;
217end;
218
219function TRegistry.ReplaceKey(const Key, FileName, BackUpFileName: string): Boolean;
220begin
221 Result := False;
222end;
223
224function TRegistry.RestoreKey(const Key, FileName: string): Boolean;
225begin
226 Result := False;
227end;
228
229function TRegistry.SaveKey(const Key, FileName: string): Boolean;
230begin
231 Result := False;
232end;
233
234function TRegistry.UnLoadKey(const Key: string): Boolean;
235begin
236 Result := false;
237end;
238
239function TRegistry.ValueExists(const Name: string): Boolean;
240
241var
242 Info : TRegDataInfo;
243
244begin
245 Result:=GetDataInfo(Name,Info);
246end;
247
248procedure TRegistry.CloseKey;
249begin
250 If (CurrentKey<>0) then
251 begin
252 if LazyWrite then
253 RegCloseKey(CurrentKey)
254 else
255 RegFlushKey(CurrentKey);
256 fCurrentKey:=0;
257 end
258end;
259
260procedure TRegistry.CloseKey(key:HKEY);
261begin
262 RegCloseKey(CurrentKey)
263end;
264
265procedure TRegistry.ChangeKey(Value: HKey; const Path: String);
266begin
267 CloseKey;
268 FCurrentKey:=Value;
269 FCurrentPath:=Path;
270end;
271
272procedure TRegistry.GetKeyNames(Strings: TStrings);
273
274Var
275 L : Cardinal;
276 I: Integer;
277 Info: TRegKeyInfo;
278 P : PChar;
279
280begin
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;
297end;
298
299procedure TRegistry.GetValueNames(Strings: TStrings);
300
301Var
302 L : Cardinal;
303 I: Integer;
304 Info: TRegKeyInfo;
305 P : PChar;
306
307begin
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
325end;
326
327Function TRegistry.SysPutData(const Name: string; Buffer: Pointer;
328 BufSize: Integer; RegData: TRegDataType) : Boolean;
329
330Var
331 P: PChar;
332 RegDataType: DWORD;
333
334begin
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;
344end;
345
346procedure TRegistry.RenameValue(const OldName, NewName: string);
347
348var
349 L: Integer;
350 InfoO,InfoN : TRegDataInfo;
351 D : TRegDataType;
352 P: PChar;
353
354begin
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;
370end;
371
372procedure TRegistry.SetCurrentKey(Value: HKEY);
373begin
374 fCurrentKey := Value;
375end;
376
377procedure TRegistry.SetRootKey(Value: HKEY);
378begin
379 fRootKey := Value;
380end;
381
Note: See TracBrowser for help on using the repository browser.