Solved

How do i search the Windows Regestery?

Posted on 1997-05-23
1
168 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now