前言: 博友 "鵬" 來信**關於鉤子的問題, 核心困難是: dll 中的資料如何傳遞出來. 在接下來的兩個例子中**這個問題.
本例建立乙個全域性的滑鼠鉤子, 然後把滑鼠的相關資訊通過乙個自定義的 getinfo 函式傳遞給呼叫鉤子的程式.
本例效果圖:
dll 檔案:
library myhook;測試**:uses
sysutils,
windows,
messages,
classes;
var hook: hhook;
info: string;
function getinfo: pchar;
begin
result := pchar(info);
end;
function mousehook(ncode: integer; wparam: wparam; lparam: lparam): lresult; stdcall;
begin
case wparam of
wm_mousemove : info := '滑鼠位置';
wm_lbuttondown : info := '按下';
wm_lbuttonup : info := '放開';
end;
info := format('%s: %d,%d', [info, pmousehookstruct(lparam)^.pt.x, pmousehookstruct(lparam)^.pt.y]);
result := callnexthookex(hook, ncode, wparam, lparam);
end;
function sethook: boolean; stdcall;
const
wh_mouse_ll =14;
begin
hook := setwindowshookex(wh_mouse_ll, @mousehook, hinstance, 0);
result := hook <> 0;
end;
function delhook: boolean; stdcall;
begin
result := unhookwindowshookex(hook);
end;
exports sethook, delhook, mousehook, getinfo;
begin
end.
unit unit1;測試窗體:inte***ce
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls, extctrls;
type
tform1 = class(tform)
button1: tbutton;
button2: tbutton;
timer1: ttimer;
procedure formcreate(sender: tobject);
procedure button1click(sender: tobject);
procedure button2click(sender: tobject);
procedure timer1timer(sender: tobject);
procedure formdestroy(sender: tobject);
end;
function sethook: boolean; stdcall;
function delhook: boolean; stdcall;
function getinfo: pchar; stdcall;
var form1: tform1;
implementation
function sethook; external 'myhook.dll';
function delhook; external 'myhook.dll';
function getinfo; external 'myhook.dll';
procedure tform1.button1click(sender: tobject);
begin
sethook;
timer1.enabled := true;
end;
procedure tform1.button2click(sender: tobject);
begin
timer1.enabled := false;
delhook;
end;
procedure tform1.formcreate(sender: tobject);
begin
button1.caption := '安裝鉤子';
button2.caption := '載卸鉤子';
timer1.interval := 100;
timer1.enabled := false;
formstyle := fsstayontop;
end;
procedure tform1.formdestroy(sender: tobject);
begin
delhook;
end;
procedure tform1.timer1timer(sender: tobject);
begin
text := getinfo;
end;
end.
object form1: tform1left = 0
top = 0
caption = 'form1'
clientheight = 78
clientwidth = 271
color = clbtnface
font.charset = default_charset
font.color = clwindowtext
font.height = -11
font.name = 'tahoma'
font.style =
oldcreateorder = false
oncreate = formcreate
ondestroy = formdestroy
pixelsperinch = 96
textheight = 13
object button1: tbutton
left = 48
top = 32
width = 75
height = 25
caption = 'button1'
taborder = 0
onclick = button1click
endobject button2: tbutton
left = 144
top = 32
width = 75
height = 25
caption = 'button2'
taborder = 1
onclick = button2click
endobject timer1: ttimer
ontimer = timer1timer
left = 128
top = 8
endend
鉤子函式使用小結
鉤子函式使用小結 首先讓我們看看hook函式是怎麼安裝 呼叫和刪除的。應用程式通常是呼叫setwindowshookex 函式來進行安裝的,其函式的原型如下 setwindowshookex int idhook,hookproc lpfn,hinstance hmod,dword dwthread...
flask 鉤子函式使用
一 鉤子函式的概念 鉤子函式是windows訊息處理機制的一部分,通過設定 鉤子 應用程式可以在系統級對所有訊息 事件進行過濾,訪問在正常情況下無法訪問的訊息。鉤子的本質是一段用以處理系統訊息的程式,通過系統呼叫,把它掛入系統。二 常用的三大鉤子函式 註冊乙個函式,在處理第乙個請求之前執行.def ...
C 鉤子函式使用
hook 鉤子 是windows提供的一種訊息處理機制平台,是指在程式正常執行中接受資訊之前預先啟動的函式,用來檢查和修改傳給該程式的資訊,鉤子 實際上是乙個處理訊息的程式段,通過系統呼叫,把它掛入系統。每當特定的訊息發出,在沒有到達目的視窗前,鉤子程式就先捕獲該訊息,亦即鉤子函式先得到控制權。這時...