jdhurst
jdhurst

Reputation: 4395

Recursively searching the registry

I can successfully query for a known Key's value, using the code below. How can I recursively search the subkeys (in my example below, all subkeys within the Uninstall folder) for a particular data's value? My aim is to see if some particular program is installed, and if not, install it.

function
...(omitted)
var
     Res : String;
     begin
      RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{92EA4162-10D1-418A-91E1-5A0453131A38}','DisplayName', Res);
      if Res <> 'A Value' then
        begin
        // Successfully read the value
        MsgBox('Success: ' + Res, mbInformation, MB_OK);
        end
    end;

Upvotes: 1

Views: 1793

Answers (1)

TLama
TLama

Reputation: 76693

The principle is easy, with the RegGetSubkeyNames you'll get an array of subkeys of a certain key and then you just iterate this array and query all the subkeys for the DisplayName value and compare the value (if any) with the searched one.

The following function shows the implementation. Note, that I've removed the Wow6432Node node from the path, so if you really need it, modify the UnistallKey constant in the code:

[Code]
const
  UnistallKey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';

function IsAppInstalled(const DisplayName: string): Boolean;
var
  S: string;
  I: Integer;
  SubKeys: TArrayOfString;
begin
  Result := False;

  if RegGetSubkeyNames(HKEY_LOCAL_MACHINE, UnistallKey, SubKeys) then
  begin
    for I := 0 to GetArrayLength(SubKeys) - 1 do
    begin
      if RegQueryStringValue(HKEY_LOCAL_MACHINE, UnistallKey + '\' + SubKeys[I],
        'DisplayName', S) and (S = DisplayName) then
      begin
        Result := True;
        Exit;
      end;
    end;
  end
  else
    RaiseException('Opening the uninstall key failed!');
end;

Upvotes: 4

Related Questions