急!请问如何监视系统对文件的访问?(100分)

  • 主题发起人 主题发起人 fzyzchg
  • 开始时间 开始时间
F

fzyzchg

Unregistered / Unconfirmed
GUEST, unregistred user!
我想写一个能够监视系统对整个文件IO的访问的程序,<br>捕获一切读取文件、写入文件、创建文件的操作(也包括软盘上)<br>但是,像ReadDirectoryChangesW和FindFirstChangeNotification<br>这些函数都只能通知文件的改变事件,却无法捕获IO读取<br>又一个叫Filemon的软件能够实现这种功能,但我不知道该如何实现<br>请高手指点!<br>
 
关注一下硬盘的读写可以吗?
 
Filemon使用虚拟驱动的方法做到的!除此之外好像没有其它更好更完美解决的方法!<br>如果只是一般监视文件,如:文件名称、读写、大小、属性、安全级别的改变,<br>还是用WINDOWS的API函数吧!用SHChangeNotifyRegister和FindFirstChangeNotification两个函数,<br>已经可以基本满足你的要求和好奇!
 
可是FindFirstChangeNotification无法捕获到文件读取啊
 
送你一段源程序,也许对你有一些帮助![:)][:)][:)]<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls,shlobj,Activex;<br><br>const<br>&nbsp; SHCNE_RENAMEITEM = $1;<br>&nbsp; SHCNE_CREATE = $2;<br>&nbsp; SHCNE_DELETE = $4;<br>&nbsp; SHCNE_MKDIR = $8;<br>&nbsp; SHCNE_RMDIR = $10;<br>&nbsp; SHCNE_MEDIAINSERTED = $20;<br>&nbsp; SHCNE_MEDIAREMOVED = $40;<br>&nbsp; SHCNE_DRIVEREMOVED = $80;<br>&nbsp; SHCNE_DRIVEADD = $100;<br>&nbsp; SHCNE_NETSHARE = $200;<br>&nbsp; SHCNE_NETUNSHARE = $400;<br>&nbsp; SHCNE_ATTRIBUTES = $800;<br>&nbsp; SHCNE_UPDATEDIR = $1000;<br>&nbsp; SHCNE_UPDATEITEM = $2000;<br>&nbsp; SHCNE_SERVERDISCONNECT = $4000;<br>&nbsp; SHCNE_UPDATEIMAGE = $8000;<br>&nbsp; SHCNE_DRIVEADDGUI = $10000;<br>&nbsp; SHCNE_RENAMEFOLDER = $20000;<br>&nbsp; SHCNE_FREESPACE = $40000;<br>&nbsp; SHCNE_ASSOCCHANGED = $8000000;<br>&nbsp; SHCNE_DISKEVENTS = $2381F;<br>&nbsp; SHCNE_GLOBALEVENTS = $C0581E0;<br>&nbsp; SHCNE_ALLEVENTS = $7FFFFFFF;<br>&nbsp; SHCNE_INTERRUPT = $80000000;<br><br>&nbsp; SHCNF_IDLIST = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;LPITEMIDLIST<br>&nbsp; SHCNF_PATHA = $1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // path name<br>&nbsp; SHCNF_PRINTERA = $2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// printer friendly name<br>&nbsp; SHCNF_DWORD = $3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // DWORD<br>&nbsp; SHCNF_PATHW = $5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // path name<br>&nbsp; SHCNF_PRINTERW = $6; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// printer friendly name<br>&nbsp; SHCNF_TYPE = $FF;<br><br>&nbsp; SHCNF_FLUSH = $1000;<br><br>&nbsp; SHCNF_FLUSHNOWAIT = $2000;<br>&nbsp; SHCNF_PATH = SHCNF_PATHW;<br>&nbsp; SHCNF_PRINTER = SHCNF_PRINTERW;<br><br>&nbsp; WM_SHNOTIFY = $401;<br>&nbsp; NOERROR = 0;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure WMShellReg(var Message:TMessage);message WM_SHNOTIFY;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>type PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT;<br>&nbsp; SHNOTIFYSTRUCT = record<br>&nbsp; &nbsp; dwItem1 : PItemIDList;<br>&nbsp; &nbsp; dwItem2 : PItemIDList;<br>&nbsp; end;<br><br>Type PSHFileInfoByte=^SHFileInfoByte;<br>&nbsp; _SHFileInfoByte = record<br>&nbsp; &nbsp; hIcon :Integer;<br>&nbsp; &nbsp; iIcon :Integer;<br>&nbsp; &nbsp; dwAttributes : Integer;<br>&nbsp; &nbsp; szDisplayName : array [0..259] of char;<br>&nbsp; &nbsp; szTypeName : array [0..79] of char;<br>&nbsp; end;<br>&nbsp; SHFileInfoByte=_SHFileInfoByte;<br><br>Type PIDLSTRUCT = ^IDLSTRUCT;<br>&nbsp; _IDLSTRUCT = record<br>&nbsp; &nbsp; pidl : PItemIDList;<br>&nbsp; &nbsp; bWatchSubFolders : Integer;<br>&nbsp; end;<br>&nbsp; IDLSTRUCT =_IDLSTRUCT;<br><br><br>function SHNotify_Register(hWnd : Integer) : Bool;<br>function SHNotify_UnRegister:Bool;<br>function SHEventName(strPath1,strPath2:string;lParam:Integer):string;<br><br>Function SHChangeNotifyDeregister(hNotify:integer):integer;stdcall;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;external 'Shell32.dll' index 4;<br>Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2;<br>Function SHGetFileInfoPidl(pidl : PItemIDList;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dwFileAttributes : Integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;psfib : PSHFILEINFOBYTE;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cbFileInfo : Integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;uFlags : Integer):Integer;stdcall;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;external 'Shell32.dll' name 'SHGetFileInfoA';<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; m_hSHNotify:Integer;<br>&nbsp; m_pidlDesktop : PItemIDList;<br><br>implementation<br><br>{$R *.DFM}<br><br>function SHEventName(strPath1,strPath2:string;lParam:Integer):string;<br>var<br>&nbsp; sEvent:String;<br>begin<br>&nbsp; case lParam of &nbsp; &nbsp; &nbsp; &nbsp;file://根据参数设置提示消息<br>&nbsp; &nbsp; SHCNE_RENAMEITEM: sEvent := '重命名文件'+strPath1+'为'+strpath2;<br>&nbsp; &nbsp; SHCNE_CREATE: sEvent := '建立文件 文件名:'+strPath1;<br>&nbsp; &nbsp; SHCNE_DELETE: sEvent := '删除文件 文件名:'+strPath1;<br>&nbsp; &nbsp; SHCNE_MKDIR: sEvent := '新建目录 目录名:'+strPath1;<br>&nbsp; &nbsp; SHCNE_RMDIR: sEvent := '删除目录 目录名:'+strPath1;<br>&nbsp; &nbsp; SHCNE_MEDIAINSERTED: sEvent := strPath1+'中插入可移动存储介质';<br>&nbsp; &nbsp; SHCNE_MEDIAREMOVED: sEvent := strPath1+'中移去可移动存储介质'+strPath1+' '+strpath2;<br>&nbsp; &nbsp; SHCNE_DRIVEREMOVED: sEvent := '移去驱动器'+strPath1;<br>&nbsp; &nbsp; SHCNE_DRIVEADD: sEvent := '添加驱动器'+strPath1;<br>&nbsp; &nbsp; SHCNE_NETSHARE: sEvent := '改变目录'+strPath1+'的共享属性';<br><br>&nbsp; &nbsp; SHCNE_ATTRIBUTES: sEvent := '改变文件目录属性 文件名'+strPath1;<br>&nbsp; &nbsp; SHCNE_UPDATEDIR: sEvent := '更新目录'+strPath1;<br>&nbsp; &nbsp; SHCNE_UPDATEITEM: sEvent := '更新文件 文件名:'+strPath1;<br>&nbsp; &nbsp; SHCNE_SERVERDISCONNECT: sEvent := '断开与服务器的连接'+strPath1+' '+strpath2;<br>&nbsp; &nbsp; SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE';<br>&nbsp; &nbsp; SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI';<br>&nbsp; &nbsp; SHCNE_RENAMEFOLDER: sEvent := '重命名文件夹'+strPath1+'为'+strpath2;<br>&nbsp; &nbsp; SHCNE_FREESPACE: sEvent := '磁盘空间大小改变';<br>&nbsp; &nbsp; SHCNE_ASSOCCHANGED: sEvent := '改变文件关联';<br>&nbsp; else<br>&nbsp; &nbsp; sEvent:='未知操作'+IntToStr(lParam);<br>&nbsp; end;<br>&nbsp; Result:=sEvent;<br>end;<br><br>function SHNotify_Register(hWnd : Integer) : Bool;<br>var<br>&nbsp; ps:PIDLSTRUCT;<br>begin<br>&nbsp; {$R-}<br>&nbsp; Result:=False;<br>&nbsp; If m_hSHNotify = 0 then begin<br>&nbsp; &nbsp; file://获取桌面文件夹的Pidl<br>&nbsp; &nbsp; if SHGetSpecialFolderLocation(0, CSIDL_DESKTOP,<br>&nbsp; &nbsp; &nbsp; &nbsp; m_pidlDesktop)&lt;&gt; NOERROR then<br>&nbsp; &nbsp; &nbsp; &nbsp; Form1.close;<br>&nbsp; &nbsp; if Boolean(m_pidlDesktop) then begin<br>&nbsp; &nbsp; &nbsp; ps.bWatchSubFolders := 1;<br>&nbsp; &nbsp; &nbsp; ps.pidl := m_pidlDesktop;<br><br>&nbsp; &nbsp; &nbsp; // 利用SHChangeNotifyRegister函数注册系统消息处理<br>&nbsp; &nbsp; &nbsp; m_hSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE Or SHCNF_IDLIST),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (SHCNE_ALLEVENTS Or SHCNE_INTERRUPT),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WM_SHNOTIFY, 1, ps);<br>&nbsp; &nbsp; &nbsp; Result := Boolean(m_hSHNotify);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; Else<br>&nbsp; &nbsp; &nbsp; // 如果出现错误就使用 CoTaskMemFree函数来释放句柄<br>&nbsp; &nbsp; &nbsp; CoTaskMemFree(m_pidlDesktop);<br>&nbsp; End;<br>&nbsp; {$R+}<br>end;<br><br>function SHNotify_UnRegister:Bool;<br>begin<br>&nbsp; Result:=False;<br>&nbsp; If Boolean(m_hSHNotify) Then<br>&nbsp; &nbsp; file://取消系统消息监视,同时释放桌面的Pidl<br>&nbsp; &nbsp; If Boolean(SHChangeNotifyDeregister(m_hSHNotify)) Then begin<br>&nbsp; &nbsp; &nbsp; {$R-}<br>&nbsp; &nbsp; &nbsp; m_hSHNotify := 0;<br>&nbsp; &nbsp; &nbsp; CoTaskMemFree(m_pidlDesktop);<br>&nbsp; &nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; &nbsp; {$R-}<br>&nbsp; &nbsp; End;<br>end;<br><br>procedure TForm1.WMShellReg(var Message:TMessage); &nbsp; &nbsp; &nbsp;file://系统消息处理函数<br>var<br>&nbsp; strPath1,strPath2:String;<br>&nbsp; charPath:array[0..259]of char;<br>&nbsp; pidlItem:PSHNOTIFYSTRUCT;<br>begin<br>&nbsp; pidlItem:=PSHNOTIFYSTRUCT(Message.wParam);<br>&nbsp; file://获得系统消息相关得路径<br>&nbsp; SHGetPathFromIDList(pidlItem.dwItem1,charPath);<br>&nbsp; strPath1:=charPath;<br>&nbsp; SHGetPathFromIDList(pidlItem.dwItem2,charPath);<br>&nbsp; strPath2:=charPath;<br><br>&nbsp; Memo1.Lines.Add(SHEvEntName(strPath1,strPath2,Message.lParam)+chr(13)+chr(10));<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; file://在程序退出的同时删除监视<br>&nbsp; if Boolean(m_pidlDesktop) then<br>&nbsp; &nbsp; SHNotify_Unregister;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject); file://Button1的Click消息<br>begin<br>&nbsp; m_hSHNotify:=0;<br>&nbsp; if SHNotify_Register(Form1.Handle) then begin file://注册Shell监视<br>&nbsp; &nbsp; ShowMessage('Shell监视程序成功注册');<br>&nbsp; &nbsp; Button1.Enabled := False;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; ShowMessage('Shell监视程序注册失败');<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Button1.Caption := '打开监视';<br>end;<br><br>end.<br>
 
FindFirstChangeNotification第二个参数,我改为True就有问题,我只是想监控整个目录和<br>他的子目录而已啊,请高手指点。
 
file 是什么啊,为什么我用不起啊,只要有file:出现的地方就有问题
 
后退
顶部