系統(tǒng)設(shè)置進(jìn)行監(jiān)視
發(fā)表時(shí)間:2023-08-17 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]你是否想為你的Windows加上一雙眼睛,察看使用者在機(jī)器上所做的各種操作(例如建立、刪除文件;改變文件或目錄名字)呢? 這里介紹一種利用Windows未公開函數(shù)實(shí)現(xiàn)這個(gè)功能的方法! ≡赪ind...
你是否想為你的Windows加上一雙眼睛,察看使用者在機(jī)器上所做的各種操作(例如建立、刪除文件;改變文件或目錄名字)呢?
這里介紹一種利用Windows未公開函數(shù)實(shí)現(xiàn)這個(gè)功能的方法。
在Windows下有一個(gè)未公開函數(shù)SHChangeNotifyRegister可以把你的窗口添加到系統(tǒng)的系統(tǒng)消息監(jiān)視鏈中,該函數(shù)在Delphi中的定義如下:
Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;
lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2;
其中參數(shù)hWnd定義了監(jiān)視系統(tǒng)操作的窗口得句柄,參數(shù)uFlags dwEventID定義監(jiān)視操作參數(shù),參數(shù)uMsg定義操作消息,參數(shù)cItems定義附加參數(shù),參數(shù)lpps指定一個(gè)PIDLSTRUCT結(jié)構(gòu),該結(jié)構(gòu)指定監(jiān)視的目錄。
當(dāng)函數(shù)調(diào)用成功之后,函數(shù)會(huì)返回一個(gè)監(jiān)視操作句柄,同時(shí)系統(tǒng)就會(huì)將hWnd指定的窗口加入到操作監(jiān)視鏈中,當(dāng)有文件操作發(fā)生時(shí),系統(tǒng)會(huì)向hWnd發(fā)送uMsg指定的消息,我們只要在程序中加入該消息的處理函數(shù)就可以實(shí)現(xiàn)對(duì)系統(tǒng)操作的監(jiān)視了。
如果要退出程序監(jiān)視,就要調(diào)用另外一個(gè)未公開得函數(shù)SHChangeNotifyDeregister來(lái)取消程序監(jiān)視。
下面是使用Delphi編寫的具體程序?qū)崿F(xiàn)范例,首先建立一個(gè)新的工程文件,然后在Form1中加入一個(gè)Button控件和一個(gè)Memo控件,
程序的代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,shlobj,Activex;
const
SHCNE_RENAMEITEM = $1;
SHCNE_CREATE = $2;
SHCNE_DELETE = $4;
SHCNE_MKDIR = $8;
SHCNE_RMDIR = $10;
SHCNE_MEDIAINSERTED = $20;
SHCNE_MEDIAREMOVED = $40;
SHCNE_DRIVEREMOVED = $80;
SHCNE_DRIVEADD = $100;
SHCNE_NETSHARE = $200;
SHCNE_NETUNSHARE = $400;
SHCNE_ATTRIBUTES = $800;
SHCNE_UPDATEDIR = $1000;
SHCNE_UPDATEITEM = $2000;
SHCNE_SERVERDISCONNECT = $4000;
SHCNE_UPDATEIMAGE = $8000;
SHCNE_DRIVEADDGUI = $10000;
SHCNE_RENAMEFOLDER = $20000;
SHCNE_FREESPACE = $40000;
SHCNE_ASSOCCHANGED = $8000000;
SHCNE_DISKEVENTS = $2381F;
SHCNE_GLOBALEVENTS = $C0581E0;
SHCNE_ALLEVENTS = $7FFFFFFF;
SHCNE_INTERRUPT = $80000000;
SHCNF_IDLIST = 0;
// LPITEMIDLIST
SHCNF_PATHA = $1;
// path name
SHCNF_PRINTERA = $2;
// printer friendly name
SHCNF_DWORD = $3;
// DWORD
SHCNF_PATHW = $5;
// path name
SHCNF_PRINTERW = $6;
// printer friendly name
SHCNF_TYPE = $FF;
SHCNF_FLUSH = $1000;
SHCNF_FLUSHNOWAIT = $2000;
SHCNF_PATH = SHCNF_PATHW;
SHCNF_PRINTER = SHCNF_PRINTERW;
WM_SHNOTIFY = $401;
NOERROR = 0;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure WMShellReg(var Message:TMessage);message WM_SHNOTIFY;
public
{ Public declarations }
end;
type PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT;
SHNOTIFYSTRUCT = record
dwItem1 : PItemIDList;
dwItem2 : PItemIDList;
end;
Type PSHFileInfoByte=^SHFileInfoByte;
_SHFileInfoByte = record
hIcon :Integer;
iIcon :Integer;
dwAttributes : Integer;
szDisplayName : array [0..259] of char;
szTypeName : array [0..79] of char;
end;
SHFileInfoByte=_SHFileInfoByte;
Type PIDLSTRUCT = ^IDLSTRUCT;
_IDLSTRUCT = record
pidl : PItemIDList;
bWatchSubFolders : Integer;
end;
IDLSTRUCT =_IDLSTRUCT;
function SHNotify_Register(hWnd : Integer) : Bool;
function SHNotify_UnRegister:Bool;
function SHEventName(strPath1,strPath2:string;lParam:Integer):string;
Function SHChangeNotifyDeregister(hNotify:integer):integer;stdcall;
external 'Shell32.dll' index 4;
Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;
lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2;
Function SHGetFileInfoPidl(pidl : PItemIDList;
dwFileAttributes : Integer;
psfib : PSHFILEINFOBYTE;
cbFileInfo : Integer;
uFlags : Integer):Integer;stdcall;
external 'Shell32.dll' name 'SHGetFileInfoA';
var
Form1: TForm1;
m_hSHNotify:Integer;
m_pidlDesktop : PItemIDList;
implementation
{$R *.DFM}
function SHEventName(strPath1,strPath2:string;lParam:Integer):string;
var
sEvent:String;
begin
case lParam of //根據(jù)參數(shù)設(shè)置提示消息
SHCNE_RENAMEITEM: sEvent := '重命名文件'+strPath1+'為'+strpath2;
SHCNE_CREATE: sEvent := '建立文件 文件名:'+strPath1;
SHCNE_DELETE: sEvent := '刪除文件 文件名:'+strPath1;
SHCNE_MKDIR: sEvent := '新建目錄 目錄名:'+strPath1;
SHCNE_RMDIR: sEvent := '刪除目錄 目錄名:'+strPath1;
SHCNE_MEDIAINSERTED: sEvent := strPath1+'中插入可移動(dòng)存儲(chǔ)介質(zhì)';
SHCNE_MEDIAREMOVED: sEvent := strPath1+'中移去可移動(dòng)存儲(chǔ)介質(zhì)'+strPath1+' '+strpath2;
SHCNE_DRIVEREMOVED: sEvent := '移去驅(qū)動(dòng)器'+strPath1;
SHCNE_DRIVEADD: sEvent := '添加驅(qū)動(dòng)器'+strPath1;
SHCNE_NETSHARE: sEvent := '改變目錄'+strPath1+'的共享屬性';
SHCNE_ATTRIBUTES: sEvent := '改變文件目錄屬性 文件名'+strPath1;
SHCNE_UPDATEDIR: sEvent := '更新目錄'+strPath1;
SHCNE_UPDATEITEM: sEvent := '更新文件 文件名:'+strPath1;
SHCNE_SERVERDISCONNECT: sEvent := '斷開與服務(wù)器的連接'+strPath1+' '+strpath2;
SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE';
SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI';
SHCNE_RENAMEFOLDER: sEvent := '重命名文件夾'+strPath1+'為'+strpath2;
SHCNE_FREESPACE: sEvent := '磁盤空間大小改變';
SHCNE_ASSOCCHANGED: sEvent := '改變文件關(guān)聯(lián)';
else
sEvent:='未知操作'+I(xiàn)ntToStr(lParam);
end;
Result:=sEvent;
end;
function SHNotify_Register(hWnd : Integer) : Bool;
var
ps:PIDLSTRUCT;
begin
{$R-}
Result:=False;
If m_hSHNotify = 0 then begin
//獲取桌面文件夾的Pidl
if SHGetSpecialFolderLocation(0, CSIDL_DESKTOP,
m_pidlDesktop)<> NOERROR then
Form1.close;
if Boolean(m_pidlDesktop) then begin
ps.bWatchSubFolders := 1;
ps.pidl := m_pidlDesktop;
// 利用SHChangeNotifyRegister函數(shù)注冊(cè)系統(tǒng)消息處理
m_hSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE Or SHCNF_IDLIST),
(SHCNE_ALLEVENTS Or SHCNE_INTERRUPT),
WM_SHNOTIFY, 1, ps);
Result := Boolean(m_hSHNotify);
end
Else
// 如果出現(xiàn)錯(cuò)誤就使用 CoTaskMemFree函數(shù)來(lái)釋放句柄
CoTaskMemFree(m_pidlDesktop);
End;
{$R+}
end;
function SHNotify_UnRegister:Bool;
begin
Result:=False;
If Boolean(m_hSHNotify) Then
//取消系統(tǒng)消息監(jiān)視,同時(shí)釋放桌面的Pidl
If Boolean(SHChangeNotifyDeregister(m_hSHNotify)) Then begin
{$R-}
m_hSHNotify := 0;
CoTaskMemFree(m_pidlDesktop);
Result := True;
{$R-}
End;
end;
procedure TForm1.WMShellReg(var Message:TMessage); //系統(tǒng)消息處理函數(shù)
var
strPath1,strPath2:String;
charPath:array[0..259]of char;
pidlItem:PSHNOTIFYSTRUCT;
begin
pidlItem:=PSHNOTIFYSTRUCT(Message.wParam);
//獲得系統(tǒng)消息相關(guān)得路徑
SHGetPathFromIDList(pidlItem.dwItem1,charPath);
strPath1:=charPath;
SHGetPathFromIDList(pidlItem.dwItem2,charPath);
strPath2:=charPath;
Memo1.Lines.Add(SHEvEntName(strPath1,strPath2,Message.lParam)+chr(13)+chr(10));
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//在程序退出的同時(shí)刪除監(jiān)視
if Boolean(m_pidlDesktop) then
SHNotify_Unregister;
end;
procedure TForm1.Button1Click(Sender: TObject); //Button1的Click消息
begin
m_hSHNotify:=0;
if SHNotify_Register(Form1.Handle) then begin //注冊(cè)Shell監(jiān)視
ShowMessage('Shell監(jiān)視程序成功注冊(cè)');
Button1.Enabled := False;
end
else
ShowMessage('Shell監(jiān)視程序注冊(cè)失敗');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := '打開監(jiān)視';
end;
end.
運(yùn)行程序,點(diǎn)擊“打開監(jiān)視”按鈕,如果出現(xiàn)一個(gè)顯示“Shell監(jiān)視程序成功注冊(cè)”的對(duì)話框,說(shuō)明Form1已經(jīng)加入到系統(tǒng)操作監(jiān)視鏈中了,你可以試著在資源管理器中建立、刪除文件夾,移動(dòng)文件等操作,你可以發(fā)現(xiàn)這些操作都被記錄下來(lái)并顯示在文本框中。
在上面的程序中多次使用到了一個(gè)PItemIDList的結(jié)構(gòu),這個(gè)數(shù)據(jù)結(jié)構(gòu)指定Windows下得一個(gè)“項(xiàng)目”,在Windows下資源實(shí)現(xiàn)統(tǒng)一管理一個(gè)“項(xiàng)目”可以是一個(gè)文件或者一個(gè)文件夾,也可以是一個(gè)打印機(jī)等資源。另外一些API函數(shù)也涉及到了Shell(Windows外殼)操作,各位讀者可以參考相應(yīng)的參考資料。
由于使用到了Windows的未公開函數(shù),沒有相關(guān)得參考資料,所以有一些未知得操作(在Memo1中會(huì)顯示“未知操作”)。如果哪位讀者有興趣, http://member.netease.com/~blackcat 有實(shí)現(xiàn)該功能的VB程序下載。
以上程序在Windows98、Windows2000、Delphi5下運(yùn)行通過(guò)。