好痛苦啊,我说了为什么不相信我?在猛料包里面噢!
Windows API里面有的RegNotifyChangeKeyValue!
我特地帮你找了一下:
在猛料包技巧1的Windows API下面:监视注册表的某一个位置的值的变化
RegNotifyChangeKeyValue
我曾经做过一测试的程序,不过有点问题,我把全部代码贴出来吧:
unit reg1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,registry,
StdCtrls;
const
machine_rootkey=HKEY_LOCAL_MACHINE;
user_rootkey=HKEY_CURRENT_USER;
defaultuser_rootkey=HKEY_USERS;
subkey1='SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/RUN';
subkey2='SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/Run-';
function regmonitor(hkeys:hkey;keys:string):boolean;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function regmonitor(hkeys:hkey;keys:string):boolean;
var
reg1:tregistry;
begin
reg1:=tregistry.Create;
reg1.RootKey:=hkeys;
try
reg1.OpenKey(keys,false);
//我的问题是在这下面的几句,当我执行时,如果注册表里我指定的键没被改变过,程序
//好像失去响应一样,你试试就知道了,也许要用多线程才行,不过我后来没去试了
application.ProcessMessages;
if RegNotifyChangeKeyValue(reg1.CurrentKey,false,REG_NOTIFY_CHANGE_NAME+REG_NOTIFY_CHANGE_LAST_SET,0,false)=ERROR_SUCCESS then
result:=true
else
result:=false;
finally
reg1.CloseKey;
reg1.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Lines.Clear;
if regmonitor(machine_rootkey,subkey1) then
memo1.Lines.Add('registry is monitored')
else
memo1.Lines.Add('registry monitors fail');
end;
end.