1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Essentials|Books|Link Back
 
Monitoring Registry Changes
Page 3: Sample Delphi Registry monitoring application
 More of this Feature
• Page 1: RegNotifyChange...
• Page 2: TRegMonitorThread
• Page 4: Source CODE

Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Working with the Registry
• Threading in Delphi
• Message handling
• Writing components

• "Big Brother" - Part 1
• "Big Brother" - Part 2
• "Big Brother" - Part 3

Since we now have developed a thread that monitors the Registry and listens to specified changes, let's see how to call the monitoring code from a Delphi project. If you recall, you have dropped a TMemo component on a form, when any specified change occurs, the change will be reported in this memo.

An example of usage
First, add a variable of type TRegMonitorThread in the interface var section (under the Form1: TForm1; line). Second, add a declaration of the procedure that will handle a message received from the thread class, in the private part of the Form1 declaration, and finally add the following code in the OnCreate event of the Form:

procedure TForm1.FormCreate(Sender: TObject);
begin
  RegMonitorThread := TRegMonitorThread.Create;
  with RegMonitorThread do
  begin
    FreeOnTerminate:=True;
    Wnd := Form1.Handle;
    Key := 'Software\ADP';
    RootKey := HKEY_LOCAL_MACHINE;
    WatchSub := True;
    Resume;
  end;
end;

Next, we add the code in the message handling procedure

procedure TForm1.WMREGCHANGE(var Msg: TMessage);
begin
 with Memo1.Lines
  Add('-----------------------------------');
  Add('Registry change at ' + DateTimeToStr(Now));
  Add(IntToStr(RegMonitorThread.ChangeData.RootKey) +
      ' - ' + RegMonitorThread.ChangeData.Key);
end;

The actual WMREGCHANGE procedure is declared as

procedure WMREGCHANGE(var Msg : TMessage); 
          message WM_REGCHANGE;

The above procedure responds to a WM_REGCHANGE message defined in the TRegMonitorThread class as a user defined message.

This is how the "program" looks at run time (after some manual Registry editing)

Delphi Registry Monitor

As you can see, the code "only" reports that a change has been captured, it does provide the time of the change but no other information like, for example, the name of the key added / deleted / edited.
After you have trapped the change in Registry you could, for example, iterate through all the keys you are interested in and compare them to some values that *should* be there. All in all, what you will do with the change remains for you to handle.

Source code
That's it. As I always love to say: "as simple as only Delphi can be!". The next page of the article provides the full source to both the TRegMonitorThread class and the sample project.

If you have any questions please post to the Delphi Programming Forum.

Next page > Full Source Code > Page 1, 2, 3, 4

~ Zarko Gajic

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2003|2002|2001|2000|1999|1998 or by TOPIC.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.