如何在DELPHI 4里写HOOK?(100分)

  • 主题发起人 主题发起人 Larry
  • 开始时间 开始时间
L

Larry

Unregistered / Unconfirmed
GUEST, unregistred user!
我如何在DELPHI 4里写MESSAGE HOOK函数,我从别的地方DOWN下来的CODE在
DELPHI 4里不行,一运行就出现启动程序错。请指点
 
什么错误, 可否说清楚点,
另外, DELPHI 32深度历险中有个例子, 可以DOWN来看看.
 
>>一运行就出现启动程序错

如果不是exception,而是根本不能运行的话,
一般是DLL声明外部函数需要的的DLL找不到。

例如:procedure aaa(...); stdcall;external 'mydll.dll';
而mydll.dll不存在于搜索路径中。
 
论坛里这个问题已经讨论过了.
CJ送给我了一个HOOK的例子.
发过去了!
 
//请试一试该程序
var
YourHook: HHOOK;

//定义用于HOOK的消息,也可以是windows的标准消息
const
WM_YourMessage = WM_USER + 2000;

//发送你需要的消息
procedure TForm1.Button1Click(Sender: TObject);
begin
PostMessage(self.Handle,WM_YourMessage,0,0);
end;

//你的HOOK处理函数
function YourHookProc(Code: Integer; WParam: Longint; var Msg: TMsg): Longint; stdcall;
begin
// if (Code = HC_ACTION) then
if Msg.Message = WM_YourMessage then
//调用你自己的HOOK函数
begin
showMessage('截获该消息');
end;
Result := CallNextHookEx(YourHook, Code, WParam, Longint(@Msg));
end;

//安装HOOK
procedure TForm1.FormCreate(Sender: TObject);
begin
YourHook := SetWindowsHookEx(WH_GETMESSAGE, @YourHookProc, 0, GetCurrentThreadID);
end;
 
//请试一试该程序
var
YourHook: HHOOK;

//定义用于HOOK的消息,也可以是windows的标准消息
const
WM_YourMessage = WM_USER + 2000;

//发送你需要的消息
procedure TForm1.Button1Click(Sender: TObject);
begin
PostMessage(self.Handle,WM_YourMessage,0,0);
end;

//你的HOOK处理函数
function YourHookProc(Code: Integer; WParam: Longint; var Msg: TMsg): Longint; stdcall;
begin
// if (Code = HC_ACTION) then
if Msg.Message = WM_YourMessage then
//调用你自己的HOOK函数
begin
showMessage('截获该消息');
end;
Result := CallNextHookEx(YourHook, Code, WParam, Longint(@Msg));
end;

//安装HOOK
procedure TForm1.FormCreate(Sender: TObject);
begin
YourHook := SetWindowsHookEx(WH_GETMESSAGE, @YourHookProc, 0, GetCurrentThreadID);
end;
 
我最近也在写一个关于键盘钩子的程序,以下是从我的程序改过来的,相信是
通用于钩子函数的.
//SETHOOK.PAS
VAR HK:HHOOK;

procedure SetHookFunc();
var
GetMsgProc:tfnhookproc;
hlib:thandle;
begin
if not isHooked then begin
hlib:=loadlibrary('YOURHOOKFILE.DLL');
if hlib<32 then begin
messagedlg('Not found DLL',mterror,[mbok],0);
exit;
end;
GetMsgProc:=tfnhookproc(getprocaddress(hlib,'GetMsgProc'));

hk:=setwindowshookex(WH_GETMESSAGE,GetMsgProc,hlib,0);
if hk=null then
messagedlg('Set hook error',mterror,[mbok],0)
else
isHooked:=true;

end;

end;

procedure UnHookFunc();
var
result:boolean;
begin
if isHooked then begin
result:=unhookwindowshookex(hk);
if result=false then
messagedlg('Unhook error',mterror,[mbok],0)
else
isHooked:=false;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
isHooked:=false;
SetHookFunc();

end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnHookFunc();
end;

//YOURHOOKFILE.DPR
library YOURHOOKFILE;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes,
MESSAGEMAP in 'MESSAGEMAP.pas';

exports
GetMsgProc name'GetMsgProc';

begin
end.
//MESSAGEMAP.PAS
//函数定义
function GetMsgProc(ncode:integer;wp:wparam;lp:lparam):lresult;stdcall;export;
//函数实现。
function GetMsgProc (ncode:integer;wp:wparam;lp:lparam):lresult;
begin
result:=0;//The return value should be zero.
if ncode=HC_ACTION then begin
//Write your code here.
end;
end;
 
多人接受答案了。
 
后退
顶部