Solved

How do i search the Windows Regestery?

Posted on 1997-05-23
1
170 Views
Last Modified: 2010-04-04
From Delphi that is.

There is a TRegestry type component in Delphi that permits me to read and write to the regestry, but it does not seem to have a search facility.

All installed programs have there .EXE in the regestry, eg Word for windows, so when i want to run word from my application, it would be wisest to get the path from the regestry, (as it could of been moved from its default path).  to find its path i could do with a TRegestry.search(....) type function.  ayone know know what to write?

I would imagine it would look somthing like For 1 to Tregestry.GetKeyNames[list of key names for the regerstry root] do
move through each one using Copy(Tregestry.keyname.ReadString), last 8 letters) = Word.EXE
.......................
Or if i knew that it was always in a particulr Root, (eg
00020901-0000-0000-c000-0000000046) but who on earth knows such things.

.................
Or perhaps if anyone knows the OLE/DDE type code to access the Windows Regestry editor and POKE or whatever a Find at it?

..................
Or perhaps some API that will search files, (i do not know much about how to get at or uses Win API calls, but somthing like a FileSearchTree(filehandel, .exename) type call?
.................
Or am i just barking up the wrong tree all together.
........................

Anyone know the best way to search for the current directory of the .EXE
Martin.  
0
Comment
Question by:MARTIN030397
1 Comment
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
ID: 1336546
As usual, my answer is basic, but it seems to work well.
Just create a TRegistry object and call:
  SeekRegistry(Reg, '\software', 'delphi', rsKey);
    or
  SeekRegistry(Reg, '', 'c:\netscape', rsStr);
    or
  SeekRegistry(Reg, '', '1997', rsInt);
    or
  Something like that...

It uses a recursive procedure that scans all subkeys(strings/integers) from a start key (you'll figure it out).
If you can't make it work the way you want; let me know!

Here goes:

type
  RegSearchModes = (rsKey, rsStr, rsInt);

procedure SeekRegistry(var Reg: TRegistry; KeyPath, Criteria: string; Mode: RegSearchModes);
{ This can be a stack hog! }
var
  Keys, Vals : TStringList;
  Index      : Integer;
begin
  Reg.OpenKey(KeyPath, False);
  if Mode in [rsStr, rsInt] then begin
    Vals := TStringList.Create;
    Reg.GetValueNames(Vals);
    for Index := 0 to Vals.Count-1 do
    case Mode of
      rsStr: if Reg.GetDataType(KeyPath+'\'+Vals[Index]) = rdString then begin
        if Pos(UpperCase(Criteria), UpperCase(Reg.ReadString(KeyPath+'\'+Vals[Index])))>0
        then { Do something with the found string! };
      end;
      rsInt: if Reg.GetDataType(KeyPath+'\'+Vals[Index]) = rdInteger then begin
        if StrToInt(Criteria)=Reg.ReadInteger(KeyPath+'\'+Vals[Index])
        then { Do something with the found integer! };
      end;
    end;
    Vals.Free;
  end;
  Keys := TStringList.Create;
  Reg.GetKeyNames(Keys);
  for Index := 0 to Keys.Count-1 do begin
    if (Mode=rsKey) then if Pos(UpperCase(Criteria), (Keys[Index]))>0
    then { Do something with the found key! };
    SeekRegistry(Reg, KeyPath+'\'+Keys[Index], Criteria, Mode);
  end;
  Keys.Free;
end;

procedure TMainForm.Button9Click(Sender: TObject);
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  Reg.RootKey := HKEY_CURRENT_USER;
  SeekRegistry(Reg, '', Edit4.Text, rsKey);
  Reg.Free;
end;

Simple, huh?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question