a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 學(xué)院 > 編程設(shè)計(jì) > 正文

一種利用Windows未公開函數(shù)實(shí)現(xiàn)機(jī)器上所做的各種操作

2020-07-14 13:39:03
字體:
供稿:網(wǎng)友
這里介紹一種利用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來取消程序監(jiān)視。

  下面是使用Delphi編寫的具體程序?qū)崿F(xiàn)范例,首先建立一個(gè)新的工程文件,然后在Form1中加入一個(gè)Button控件和一個(gè)Memo控件,

  程序的代碼如下:

程序代碼

復(fù)制代碼
代碼如下:

  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ù)來釋放句柄
  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)視 ';
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美国产日本 | 日韩福利在线观看 | 国产综合精品一区二区三区 | 一级毛片,一级毛片 | 国产成人午夜精品5599 | 日韩中文一区二区三区 | 男男高h在线观看 | 精品久久一 | 免费黄网址 | 中文字幕在线资源 | 久久品 | 日本一本视频 | 精品国产一区二区三区久久久蜜月 | 99精品国产在热久久 | 国产精品二区三区 | 日韩一级在线免费观看 | 亚洲日韩欧美一区二区在线 | 亚洲视频在线观看网址 | 武道仙尊动漫在线观看 | 国产超碰在线 | 久草在线视频网 | 国产精品视频一二三区 | 能看的毛片 | 99国产精品久久久久久久 | 久久精品亚洲一区二区 | 欧美乱轮| 亚洲国产成人久久一区二区三区 | 色99在线| 久久久久久一区 | 国产乱视频网站 | 四虎免费影视 | 日韩综合网 | 男女免费视频 | 国产在线观 | 久久久久久久久久一本门道91 | 亚洲精品在线免费 | 日日操天天射 | 久久亚洲精品视频 | 国产乱码精品一区二区三区五月婷 | 国产一区在线观看视频 | 欧美视频在线观看不卡 |