您好,欢迎您访问阳光问答网。

阳光问答网

首页 > 问答大全

问答大全

一种利用Windows未公开函数实现机器上所做的各种操作

发布时间:2021-11-24 问答大全
这儿详细介绍一种运用Windows对外公布涵数完成这一作用的方式。
在Windows下有一个对外公布涵数SHChangeNotifyRegister能够 将你的对话框加上到系统软件的消息推送监控链中...

  这儿详细介绍一种运用Windows对外公布涵数完成这一作用的方式。

  在Windows下有一个对外公布涵数SHChangeNotifyRegister能够 将你的对话框加上到系统软件的消息推送监控链中,该涵数在Delphi中的界定以下:

  Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll ' index 2;

  在其中主要参数hWnd界定了监控系统实际操作的对话框得返回值,主要参数uFlags dwEventID界定监控实际操作主要参数,主要参数uMsg界定实际操作信息,主要参数cItems界定额外主要参数,主要参数lpps特定一个PIDLSTRUCT构造,该构造特定监控的文件目录。

  当调用函数取得成功以后,涵数会回到一个监控实际操作返回值,另外系统软件便会将hWnd特定的对话框添加到实际操作监控链中,当有文档实际操作产生时,系统软件会向hWnd推送uMsg特定的信息,大家只需在程序流程中添加该信息的处理函数就可以完成系统对实际操作的监控了。

  假如要撤出程序流程监控,就需要启用此外一个对外公布得涵数SHChangeNotifyDeregister来撤销程序流程监控。

  下边是应用Delphi撰写的实际程序代码案例,最先创建一个新的工程文件,随后在Form1中添加一个Button控制和一个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 //依据基本参数提醒信息 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+ '中插进可挪动移动存储设备 '; SHCNE_MEDIAREMOVED: sEvent := strPath1+ '中移去可挪动移动存储设备 '+strPath1+ ' '+strpath2; SHCNE_DRIVEREMOVED: sEvent := '移去控制器 '+strPath1; SHCNE_DRIVEADD: sEvent := '加上控制器 '+strPath1; SHCNE_NETSHARE: sEvent := '更改文件目录 '+strPath1+ '的共享资源特性 '; SHCNE_ATTRIBUTES: sEvent := '更改文件名称特性 文件夹名称 '+strPath1; SHCNE_UpdateDIR: sEvent := '更新目录 '+strPath1; SHCNE_UpdateITEM: sEvent := '升级文档 文件夹名称: '+strPath1; SHCNE_SERVERDISCONNECT: sEvent := '断掉与网络服务器的联接 '+strPath1+ ' '+strpath2; SHCNE_UpdateIMAGE: sEvent := 'SHCNE_UpdateIMAGE '; SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI '; SHCNE_RENAMEFOLDER: sEvent := '重新命名文件夹名称 '+strPath1+ '为 '+strpath2; SHCNE_FREESPACE: sEvent := '储存空间尺寸更改 '; SHCNE_ASSOCCHANGED: sEvent := '更改文件关联 '; else sEvent:= '不明实际操作 '+IntToStr(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涵数注册系统信息解决 m_hSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE or SHCNF_IDLIST), (SHCNE_ALLEVENTS or SHCNE_INTERRUPT), WM_SHNOTIFY, 1, ps); Result := Boolean(m_hSHNotify); end Else // 假如发生不正确就应用 CoTaskMemFree涵数来释放出来返回值 CoTaskMemFree(m_pidlDesktop); End; {$R+} end; function SHNotify_UnRegister:Bool; begin Result:=False; If Boolean(m_hSHNotify) Then //撤销消息推送监控,另外释放出来桌面上的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); //消息推送处理函数 var strPath1,strPath2:String; charPath:array[0..259]of char; pidlItem:PSHNOTIFYSTRUCT; begin pidlItem:=PSHNOTIFYSTRUCT(Message.wParam); //得到消息推送有关得途径 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 //在程序流程撤出的另外删掉监控 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 //申请注册Shell监控 ShowMessage( 'Shell监控程序流程取得成功申请注册 '); Button1.Enabled := False; end else ShowMessage( 'Shell监控程序流程申请注册不成功 '); end; procedure TForm1.FormCreate(Sender: TObject); begin Button1.Caption := '开启监控 ';