如何记录文件什么时候被拷贝、删除。 ( 积分: 100 )

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

fuxiaojie

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用delphi写个程序记录我的系统什么时候开机,开机以后那个文件被拷贝、删除、打开过等。
 
我想用delphi写个程序记录我的系统什么时候开机,开机以后那个文件被拷贝、删除、打开过等。
 
请高手来指点一下我也有这个想法
 
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; external 'Shell32.dll' index 4;<br>Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;<br> &nbsp; &nbsp; &nbsp; &nbsp; lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2;<br>Function SHGetFileInfoPidl(pidl : PItemIDList;<br> &nbsp; &nbsp; &nbsp; &nbsp; dwFileAttributes : Integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; psfib : PSHFILEINFOBYTE;<br> &nbsp; &nbsp; &nbsp; &nbsp; cbFileInfo : Integer;<br> &nbsp; &nbsp; &nbsp; &nbsp; uFlags : Integer):Integer;stdcall;<br> &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><br>///////////////////////////<br>具体例子看我的程序<br>http://www.onlinedown.net/soft/13479.htm <br>中的操作监视。不管什么文件删除改名都可以监视到。给分吧
 
编译不了呀,有个错误。
 
打个记号,不用给分。<br>我能想到的是使用HOOK来HOOK createfile,openfile等等的函数。然后记录!
 
case lParam of &nbsp; &nbsp; &nbsp; &nbsp;file:是什么意思呀,提示有语法错误。
 
file是说明啊把它去了。
 
运行的时候ps.bWatchSubFolders := 1;会出现异常。
 
不好意思啊,上面是D5的代码。D6以上需要增加一句<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;[red]new(ps);//这里要给它空间,/////////////////////////////////////[/red]<br> &nbsp; &nbsp;//获取桌面文件夹的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;
 

Similar threads

后退
顶部