Link to home
Start Free TrialLog in
Avatar of angel_fire2701
angel_fire2701

asked on

Read unknown registry Key

How would I read a registry key in delphi with a wildcard or similar if I don't know the whole key name.

I know the whole path to the key and the first part of the key name but the ending will not allways be the same.
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Assuming '*' as wildcard you could do something like this


uses
Registry;

procedure TForm1.Button1Click(Sender: TObject);
function FindRegistryKey(RootKey: HKEY;
                               RootPath: String;
                               KeyWithWildCard: String): String;
var
  Reg: TRegistry;
  Subkeys: TStrings;
  I: Integer;
  PrefixKey: String;
begin
  I := 1;
  while i < length(KeyWithWildCard) do
   begin
    if KeyWithWildCard[i] = '*' then break;
    PrefixKey := PrefixKey+KeyWithWildCard[i];
    inc(i);
   end;
  Result := '';
  Reg := TRegistry.Create;
  Subkeys := TStringList.Create;
  try
    Reg.RootKey := RootKey;
    if (Reg.OpenKey(RootPath, False)) then
      begin
      Reg.GetKeyNames(Subkeys);
      for I := 0 to (Subkeys.Count - 1) do
          begin
          If pos(PrefixKey,Subkeys[I]) > 0 then
          Result := RootPath + '\' + Subkeys[I];
          if (Result <> '') then
            Break;
          end;
      end;
  finally
    Subkeys.Free;
    Reg.Free;
  end;
end;
begin
 Showmessage(FindRegistryKey(HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion','Windows*'));
end;

Open in new window

Avatar of angel_fire2701
angel_fire2701

ASKER

Thanks for the reply.
I created my form, added a button then went into the source and pasted in the code you provided.
As you can see I changed the HKEY to current user and as a test I tried to pull the path key for 7-zip using a wildcard with no success.
I think were on the right track, do you think I'm missing something?

Capture.JPG
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes sorry if i didn't elaborate, I need the value of an partially known key. I'm off for the weekend and will return to this on Monday, thanks for your help so far.
Thank You, That works Perfectly.