PDA

View Full Version : Recursive Registry



jamesa
28-02-2005, 09:17 AM
Hello,

Maybe this is not so related with games, but it can be used as debugging tools.

Anyone can show me some code to output registry tree and its value ?

I got this code somewhere:



function readTree(rootKey: HKEY; startKey: string): TStringList;
var reg: TRegistry;
childKeys, valueList: TStringList;
i: integer;
begin
childKeys:=TStringList.Create;
valueList:=TStringList.Create;
result:=TStringList.Create;

reg:=TRegistry.Create;
reg.RootKey:=rootKey;
reg.OpenKey(startKey, false);

reg.GetKeyNames(childKeys);
reg.GetValueNames(valueList);

result.AddStrings(valueList);

for i:=0 to childKeys.count-1 do begin
result.AddStrings(readTree(rootKey, startKey + '\' + childKeys[i]));
end;
end;


Usage:


procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Text := readTree(HKEY_CURRENT_USER, '\Software\MySoftware\').Text;
end;


But it did not print the path and value.

Please if anyone can help me, I am stuck with this for 2 days now :(

Thanks.

siim
01-03-2005, 09:25 PM
You are leaking memory all over the place.
This code should work, although I don't know if this is exactly what you wanted:



uses Registry;

procedure readTree(List: TStrings; rootKey: HKEY; const startKey: string);
var
reg: TRegistry;
childKeys, valueList: TStringList;
i: integer;
begin
childKeys := TStringList.Create;
valueList := TStringList.Create;
reg := TRegistry.Create;
try
reg.RootKey:=rootKey;
reg.OpenKey(startKey, false);

reg.GetKeyNames(childKeys);
reg.GetValueNames(valueList);
for i := 0 to valueList.Count-1 do
List.Add(startKey + valueList[i]);

for i:=0 to childKeys.count-1 do
readTree(List, rootKey, startKey + '\' + childKeys[i]);
finally
childKeys.Free;
valueList.Free;
reg.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
with Memo1 do
begin
Lines.BeginUpdate;
Lines.Clear;
readTree(Memo1.Lines, HKEY_CURRENT_USER, 'Software');
Lines.EndUpdate;
end;
end;

jamesa
02-03-2005, 08:35 PM
Thanks siim,

This is very useful for diagnostic things. I dont know, sometimes got a complaint from user that the game does not work anymore. After I checked, the registry settings was messed up.