Solved

How do i search the Windows Regestery?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

939 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

7 Experts available now in Live!

Get 1:1 Help Now