SetWindowsHookEx的用法(100分)

  • 主题发起人 主题发起人 Bahl
  • 开始时间 开始时间
B

Bahl

Unregistered / Unconfirmed
GUEST, unregistred user!
将dll注入进程技术在实现api函数的监视程序中不可缺少的一项工作。其中最常见的就是用<br>setwindowshookex函数实现了。<br>那么怎样使用SetWindowsHookEx将某个Dll注入某个进程?
 
function KeyboardHook(nCode: Integer; wParam: WPARAM;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lParam: LPARAM): LResult; stdcall;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; WinHook: HHOOK; &nbsp; &nbsp; // a handle to the keyboard hook function<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br><br>&nbsp; {install the keyboard hook function into the keyboard hook chain}<br>&nbsp; WinHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyboardHook, 0, GetCurrentThreadID);<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>&nbsp; {remove the keyboard hook function from the keyboard hook chain}<br>&nbsp; UnhookWindowsHookEx(WinHook);<br>end;<br><br>function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LResult;<br><br>begin<br>&nbsp; {if we can process the hook information...}<br>&nbsp; if (nCode&gt;-1) then<br>&nbsp; &nbsp; {...was the TAB key pressed?}<br>&nbsp; &nbsp; if (wParam=VK_TAB) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; {if so, output a beep sound}<br>&nbsp; &nbsp; &nbsp; MessageBeep(0);<br><br>&nbsp; &nbsp; &nbsp; {indicate that the message was processed}<br>&nbsp; &nbsp; &nbsp; Result := 1;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; {...was the RETURN key pressed?}<br><br>&nbsp; &nbsp; if (wParam=VK_RETURN) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; {if so, and if the key is on the up stroke, cause<br>&nbsp; &nbsp; &nbsp; &nbsp;the focus to move to the next control}<br>&nbsp; &nbsp; &nbsp; if ((lParam shr 31)=1) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Form1.Perform(WM_NEXTDLGCTL, 0, 0);<br><br>&nbsp; &nbsp; &nbsp; {indicate that the message was processed}<br>&nbsp; &nbsp; &nbsp; Result := 1;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; {otherwise, indicate that the message was not processed.}<br><br>&nbsp; &nbsp; &nbsp; Result := 0<br>&nbsp; else<br>&nbsp; &nbsp; {we must pass the hook information to the next hook in the chain}<br>&nbsp; &nbsp; Result := CallNextHookEx(WinHook, nCode, wParam, lParam);<br>end;
 
给你个以前写的完整的程序。<br>library Project2;<br><br>{ MouseHook.Dll }<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Windows,<br>&nbsp; Messages,Dialogs,<br>&nbsp; Classes;<br><br>var<br>&nbsp; HookProc: HHook;<br>function SetHook(nCode: Integer;wParam: WPARAM;lParam: LPARAM):LRESULT;stdcall;<br>begin<br>&nbsp; result := 0 ;<br>&nbsp; if nCode &lt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; result := CallNextHookEx(HookProc, nCode, wParam, lParam);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>&nbsp; if wParam = WM_RBUTTONUP then<br>&nbsp; begin<br>&nbsp; &nbsp; ShowMessage('Can not run this program!');<br>&nbsp; &nbsp; result := 1;<br>&nbsp; end;<br>end;<br>function InstallHook(AHandle: THandle):Boolean;<br>var<br>&nbsp; id: DWord;<br>begin<br>&nbsp; result := false;<br>&nbsp; id := GetWindowThreadProcessID(AHandle, nil);<br>&nbsp; HookProc := SetWindowsHookEx(WH_MOUSE, SetHook, HInstance, id);<br>&nbsp; result := HookProc &lt;&gt; 0;<br>end;<br>function UnInstallHook:Boolean;<br>begin<br>&nbsp; if HookProc &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; UnHookWindowsHookEx(HookProc);<br>&nbsp; &nbsp; HookProc := 0;<br>&nbsp; end;<br>&nbsp; result := HookProc = 0;<br>end;<br>exports InstallHook, UnInstallHook;<br>begin<br>&nbsp; HookProc := 0;<br>end.<br>/////////////////////////////////////////////////////////////<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; DllHandle: THandle;<br>&nbsp; InstallHook: function (AHandle: THandle):Boolean;<br>&nbsp; UnInstallHook: function :Boolean;<br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; AHandle: THandle;<br>begin<br>&nbsp; AHandle := FindWindow('progman',nil);<br>&nbsp; if InstallHook(AHandle) then<br>&nbsp; &nbsp; ShowMessage('Hook Installed successfully on ' + IntToStr(AHandle));<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; DllHandle := LoadLibrary('Project2.dll');<br>&nbsp; InstallHook := GetProcAddress(DllHandle, 'InstallHook');<br>&nbsp; UnInstallHook := GetProcAddress(DllHandle, 'UnInstallHook');<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; if UnInstallHook then<br>&nbsp; &nbsp; ShowMessage('hook has been uninstalled successfully!');<br>end;<br><br>end.
 
多人接受答案了。
 
后退
顶部