当前位置:首页 > 网络编程 > 软件语言 > DELPHI > 实时侦测目录中文件变化

实时侦测目录中文件变化

点击次数:36 次 发布日期:2008-11-09 08:40:37 作者:源代码网
源代码网推荐
广告载入中
在WIN32下用DELPHI侦测目录变化,可用WIN32提供的文件改变通知API来完成。FindFirstChangeNotification, FindNextChangeNotification,FindCloseChangeNotification。
源代码网推荐 在应用程序中调用这些函数时,产生一个监控这个变化的句柄,可用WAIT函数集来等待这个变化。这样,当监控程序运行时,可以达到监控文件变化的动作。更进一步,可把此程序做成一个状态区图标(TRAY)来完成监控。
源代码网推荐
源代码网推荐 Windows在删除、复制、移动、访问文件时并不发送消息,当然截获不到。要截取这些操作过程的唯一办法就是截获API,这又需要你编写Vxd程序了,杀毒软件都是这样作的。你注意一下杀毒软件一般都带有一个vxd程序。光有vxd还不行,还需截获文件API。还有另外一个办法,就是CIH病毒采用的办法,直接跳到系统零层去操作。具体办法如下:
源代码网推荐 一、SIDT指令( 将中断描述符表寄存器IDTR--64位宽,16~47Bit存有中断描述符表IDT基地址--的内容存入指定地址单元)不是特权指令,就是说我们可以在Ring3下执行该指令,获得IDT的基地址,从而修改IDT,增加一个中断门安置我们的中断服务,一旦Ring3程序中产生此中断,VMM就会调用此中断服务程序,而此中断服务程序就运行在Ring0下了。这一点与在DOS下非常相似。
源代码网推荐
源代码网推荐 二、要实现对系统中所有文件I/O操作的实时监视,还要用到另一种关键技-FileHooking,通过挂接一个处理函数,截获所有与文件I/O操作有关的系 统调用。Windows9x使用32位保护模式可安装文件系统(IFS),由可安装文件系统管理器(IFSManager)协调对文件系统和设备的访问,它接收以Win32API函数调用形式向系统发出的文件I/O请求,再将请求转给文件系统驱动程序FSD,由它调用低级别的IOS系统实现最终访问。每个文件I/OAPI调用都有一个特定的FSD函数与之对应,IFSManager负责完成由API到FSD的参数装配工作,在完成文件I/OAPI函数参数的装配之后转相应FSD执行之前,它会调用一个称为FileSystemApiHookFunction的Hooker函数。通过安装自己的Hooker函数,就可以截获系统内所有对文件I/O的API调用,从而实现实时监控。
源代码网推荐 =========================================
源代码网推荐 procedure TForm1.Button2Click(Sender: TObject);
源代码网推荐 begin
源代码网推荐 {establish a notification for file name changes on the selected directory}
源代码网推荐 NotificationHandle := FindFirstChangeNotification(PChar(DirectoryListBox1.Directory), FALSE,FILE_NOTIFY_CHANGE_FILE_NAME);
源代码网推荐 {if the notification was set up correctly, modify some UI elements...}

源代码网推荐 if (NotificationHandle <> INVALID_HANDLE_VALUE) then
源代码网推荐 begin
源代码网推荐 Button1.Enabled := TRUE;
源代码网推荐 Button2.Enabled := FALSE;
源代码网推荐 end
源代码网推荐 else
源代码网推荐 begin
源代码网推荐 {...otherwise indicate that there was an error}
源代码网推荐 ShowMessage("There was an error setting the notification");
源代码网推荐 Exit;
源代码网推荐 end;
源代码网推荐 end;
源代码网推荐
源代码网推荐 procedure TForm1.Button1Click(Sender: TObject);
源代码网推荐 var
源代码网推荐 dwResult: DWORD; // holds the result of waiting on the notification
源代码网推荐 Waiting: Boolean; // loop control variable
源代码网推荐 begin
源代码网推荐 {setup the loop control for a continuous loop}
源代码网推荐 Waiting := TRUE;
源代码网推荐 {indicate that the application is waiting for the change notification to fire}
源代码网推荐 Button1.Enabled := FALSE;
源代码网推荐 StatusBar1.SimpleText := "Now waiting for a filename change";
源代码网推荐 Application.ProcessMessages;
源代码网推荐 {enter the loop}
源代码网推荐 while Waiting do
源代码网推荐 begin
源代码网推荐 {at this point, the application is suspended until the notification
源代码网推荐 object is signaled that a filename change has occured in the
源代码网推荐 selected directory (this includes file deletions)}
源代码网推荐 dwResult := WaitForSingleObject(NotificationHandle,INFINITE);
源代码网推荐 if (dwResult = WAIT_OBJECT_0) then
源代码网推荐
源代码网推荐 begin
源代码网推荐 {indicate that the notification object was signaled}
源代码网推荐 ShowMessage("The selected directory signaled a filename change");
源代码网推荐
源代码网推荐 {query the user to see if they wish to continue monitoring this
源代码网推荐 directory}
源代码网推荐 if Application.MessageBox("Do you wish to continue monitoring this directory?", "Continue?", MB_ICONQUESTION or
源代码网推荐 MB_YESNO) = IDYES then
源代码网推荐
源代码网推荐 {if the user wishes to continue monitoring the directory, reset
源代码网推荐 the notification object and continue the loop...}
源代码网推荐 FindNextChangeNotification(NotificationHandle)
源代码网推荐 else
源代码网推荐 {...otherwise break out of the loop}
源代码网推荐 Waiting := FALSE;
源代码网推荐 end;
源代码网推荐 end;
源代码网推荐
源代码网推荐 {close the notification object}
源代码网推荐 FindCloseChangeNotification(NotificationHandle);


源代码网推荐
源代码网推荐 {reset UI elements}
源代码网推荐
源代码网推荐 Button1.Enabled := FALSE;
源代码网推荐 Button2.Enabled := TRUE;
源代码网推荐 StatusBar1.SimpleText := "";
源代码网推荐 FileListBox1.Update;
源代码网推荐 end;
源代码网推荐 ===========================================
源代码网推荐 下面是一个监视的控件:
源代码网推荐 unit dirnotify;
源代码网推荐
源代码网推荐 interface
源代码网推荐
源代码网推荐 uses
源代码网推荐 Windows, Messages, SysUtils, Classes,
源代码网推荐 Graphics, Controls, Forms, Dialogs;
源代码网推荐
源代码网推荐 type
源代码网推荐 EDirNotificationError = class(Exception);
源代码网推荐
源代码网推荐 TDirNotify = class;
源代码网推荐 TNotifyFilter = (nfFileName, nfDirName, nfAttributes, nfSize, nfLastWrite,
源代码网推荐 nfSecurity);
源代码网推荐 TNotifyFilters = set of TNotifyFilter;
源代码网推荐
源代码网推荐 TNotificationThread = class(TThread)
源代码网推荐 Owner: TDirNotify;
源代码网推荐 procedure Execute; override;
源代码网推荐 procedure DoChange;
源代码网推荐 end;
源代码网推荐
源代码网推荐 TDirNotify = class(TComponent)
源代码网推荐 private
源代码网推荐 FEnabled: Boolean;
源代码网推荐 FOnChange: TNotifyEvent;
源代码网推荐 FNotificationThread: TNotificationThread;
源代码网推荐 FPath: String;
源代码网推荐 FWatchSubTree: Boolean;
源代码网推荐 FFilter: TNotifyFilters;
源代码网推荐
源代码网推荐 procedure SetEnabled( Value: Boolean );
源代码网推荐 procedure SetOnChange( Value: TNotifyEvent );
源代码网推荐 procedure SetPath( Value: String );
源代码网推荐 procedure SetWatchSubTree( Value: Boolean );
源代码网推荐 procedure SetFilter( Value: TNotifyFilters );
源代码网推荐
源代码网推荐 procedure RecreateThread;
源代码网推荐
源代码网推荐 protected
源代码网推荐 procedure Change;
源代码网推荐 procedure Loaded; override;
源代码网推荐
源代码网推荐 public
源代码网推荐 constructor Create(AOwner: TComponent); override;
源代码网推荐 destructor Destroy; override;
源代码网推荐
源代码网推荐 published
源代码网推荐 property Enabled: Boolean read FEnabled write SetEnabled default True;
源代码网推荐 property OnChange: TNotifyEvent read FOnChange write SetOnChange;
源代码网推荐 property Path: String read FPath write SetPath;
源代码网推荐 property WatchSubTree: Boolean read FWatchSubTree write SetWatchSubTree;
源代码网推荐 property Filter: TNotifyFilters read FFilter write SetFilter default [nfFileName, nfDirName, nfAttributes, nfLastWrite, nfSecurity];
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure Register;
源代码网推荐
源代码网推荐 implementation
源代码网推荐
源代码网推荐 const
源代码网推荐 LASTERRORTEXTLENGTH = 500;
源代码网推荐
源代码网推荐 var
源代码网推荐 LastErrorText: array [0..LASTERRORTEXTLENGTH] of char;
源代码网推荐
源代码网推荐
源代码网推荐 function GetLastErrorText: PChar;
源代码网推荐 begin
源代码网推荐 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
源代码网推荐 nil, GetLastError, 0, LastErrorText, LASTERRORTEXTLENGTH, nil );
源代码网推荐 Result := LastErrorText;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TNotificationThread.Execute;
源代码网推荐 var
源代码网推荐 h: THandle;
源代码网推荐 nf: Longint;
源代码网推荐 wst: LongBool;
源代码网推荐 begin
源代码网推荐 nf := 0;
源代码网推荐 if (nfFileName in Owner.Filter) then nf := FILE_NOTIFY_CHANGE_FILE_NAME;
源代码网推荐 if (nfDirName in Owner.Filter) then nf := nf or FILE_NOTIFY_CHANGE_DIR_NAME;
源代码网推荐 if (nfAttributes in Owner.Filter) then nf := nf or FILE_NOTIFY_CHANGE_ATTRIBUTES;
源代码网推荐 if (nfSize in Owner.Filter) then nf := nf or FILE_NOTIFY_CHANGE_SIZE;
源代码网推荐 if (nfLastWrite in Owner.Filter) then nf := nf or FILE_NOTIFY_CHANGE_LAST_WRITE;
源代码网推荐 if (nfSecurity in Owner.Filter) then nf := nf or FILE_NOTIFY_CHANGE_SECURITY;
源代码网推荐
源代码网推荐 // yeahh, this one is stupid but Win98 malfunctions in any other value than 0 or 1
源代码网推荐 if Owner.FWatchSubTree then wst := Longbool(1)
源代码网推荐 else wst := Longbool(0);
源代码网推荐
源代码网推荐 h := FindFirstChangeNotification( Pointer(Owner.Path), wst, nf );
源代码网推荐 if (h = INVALID_HANDLE_VALUE) then
源代码网推荐 raise EDirNotificationError.Create( GetLastErrorText );
源代码网推荐
源代码网推荐 repeat
源代码网推荐 if (WaitForSingleObject( h, 1000 ) = WAIT_OBJECT_0) then
源代码网推荐 begin
源代码网推荐 Synchronize(DoChange);
源代码网推荐
源代码网推荐 if not FindNextChangeNotification( h ) then
源代码网推荐 raise EDirNotificationError.Create( GetLastErrorText );
源代码网推荐 end;
源代码网推荐 until Terminated;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TNotificationThread.DoChange;
源代码网推荐 begin
源代码网推荐 Owner.Change;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 constructor TDirNotify.Create(AOwner: TComponent);
源代码网推荐 begin
源代码网推荐 inherited Create(AOwner);
源代码网推荐
源代码网推荐 FEnabled := True;
源代码网推荐 FFilter := [nfFileName];
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 destructor TDirNotify.Destroy;
源代码网推荐 begin
源代码网推荐 FNotificationThread.Free;

源代码网推荐 inherited Destroy;
源代码网推荐 end;
源代码网推荐
源代码网推荐 procedure TDirNotify.Loaded;
源代码网推荐 begin
源代码网推荐 inherited;
源代码网推荐
源代码网推荐 RecreateThread;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TDirNotify.SetEnabled(Value: Boolean);
源代码网推荐 begin
源代码网推荐 if Value <> FEnabled then
源代码网推荐 begin
源代码网推荐 FEnabled := Value;
源代码网推荐
源代码网推荐 RecreateThread;
源代码网推荐 end;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TDirNotify.SetPath( Value: String );
源代码网推荐 begin
源代码网推荐 if Value <> FPath then
源代码网推荐 begin
源代码网推荐 FPath := Value;
源代码网推荐 RecreateThread;
源代码网推荐 end;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TDirNotify.SetWatchSubTree( Value: Boolean );
源代码网推荐 begin
源代码网推荐 if Value <> FWatchSubTree then
源代码网推荐 begin
源代码网推荐 FWatchSubTree := Value;
源代码网推荐 RecreateThread;
源代码网推荐 end;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TDirNotify.SetFilter( Value: TNotifyFilters );
源代码网推荐 begin
源代码网推荐 if Value <> FFilter then
源代码网推荐 begin
源代码网推荐 FFilter := Value;
源代码网推荐 RecreateThread;
源代码网推荐 end;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TDirNotify.SetOnChange(Value: TNotifyEvent);

源代码网推荐 begin
源代码网推荐 FOnChange := Value;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TDirNotify.Change;
源代码网推荐 begin
源代码网推荐 if Assigned(FOnChange) then
源代码网推荐 FOnChange(Self);
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure TDirNotify.RecreateThread;
源代码网推荐 begin
源代码网推荐 // destroy thread
源代码网推荐 FNotificationThread.Free;
源代码网推荐 FNotificationThread := nil;
源代码网推荐
源代码网推荐 if FEnabled and not(csDesigning in ComponentState)
源代码网推荐 and not(csLoading in ComponentState) and (FPath <> "") then
源代码网推荐 begin
源代码网推荐 // create thread
源代码网推荐 FNotificationThread := TNotificationThread.Create(True);
源代码网推荐 FNotificationThread.Owner := self;
源代码网推荐 FNotificationThread.Resume;
源代码网推荐 end;
源代码网推荐 end;
源代码网推荐
源代码网推荐
源代码网推荐 procedure Register;
源代码网推荐 begin
源代码网推荐 RegisterComponents("System", [TDirNotify]);
源代码网推荐 end;
源代码网推荐
源代码网推荐 end.


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华