请问哪有关于HOOK的资料及源码下载呀?
解决方案 »
- delphi7简单通信问题?
- 我的程序为什么不能运行,看不到啊,
- 关于QRdbtext是否有trim()这样的功能吗?如果我那个字段的字符数为20,但记录里只有2个字符,能否将后面的18个空白字符trim掉呢?
- 怎样判断ADOCOnnection是否处在zombie状态?并结束这种状态?
- 高分请教此问题.
- 还有一个问题,如何制作供网页程序asp、vb.net、C#调用的网页用的控件
- Delphi6.0和 C++ builder 6.0有什么重要的区别吗?(回复即有分)
- 天气热,近来坐坐!!
- 我写了个文档,看的人不少,可怎么没人提意见?
- 急急请问各位高手,delphi5怎样定义和使用游标???
- 3层问题交流
- 迷惑:3层结构到底是什么概念?有什么意义?
Hook是应用程序在Microsoft Windows 消息处理过程中设置的用来监控消息流并且处理系统中尚未到达目的窗口的某一类型消息过程的机制。如果Hook过程在应用程序中实现,若应用程序不是当前窗口时,该Hook就不起作用;如果Hook在DLL中实现,程序在运行中动态调用它,它能实时对系统进行监控。根据需要,我们采用的是在DLL中实现Hook的方式。1.新建一个导出两个函数的DLL文件,在hookproc.pas中定义了钩子具体实现过
程。代码如下:
library keyspy;
uses
windows, messages, hookproc in 'hookproc.pas';
exports
setkeyhook,
endkeyhook;
begin
nexthookproc:=0;
procsaveexit:=exitproc;
exitproc:=@keyhookexit;
end.2.在Hookproc.pas中实现了钩子具体过程:
unit hookproc;
interface
uses
Windows, Messages, SysUtils, Controls, StdCtrls;
var
nexthookproc:hhook;
procsaveexit:pointer;
function keyboardhook(icode:integer;wparam:wparam;
lparam:lparam):lresult;stdcall;export;
function setkeyhook:bool;export;//加载钩子
function endkeyhook:bool;export;//卸载钩子
procedure keyhookexit;far;
const
afilename='c:\debug.txt';//将键盘输入动作写入文件中
var
debugfile:textfile;
implementation
function keyboardhookhandler(icode:integer;wparam:wparam;
lparam:lparam):lresult;stdcall;export;
begin
if icode<0 then
begin
result:=callnexthookex(hnexthookproc,icode,wparam,lparam);
exit;
end;
assignfile(debugfile,afilename);
append(debugfile);
if getkeystate(vk_return)<0 then
begin
writeln(debugfile,'');
write(debugfile,char(wparam));
end
else
write(debugfile,char(wparam));
closefile(debugfile);
result:=0;
end;
function endkeyhook:bool;export;
begin
if nexthookproc<>0 then begin
unhookwindowshookex(nexthookproc);
nexthookproc:=0;
messagebeep(0); end;
result:=hnexthookproc=0;
end;
procedure keyhookexit;far;
begin
if nexthookproc<>0 then endkeyhook;
exitproc:=procsaveexit; end;
end.二、Win95/98使用任务栏右方指示区来显示应用程序或工具图标对指示区图标的操作涉及了一个API函数Shell_NotifyIcon,它有两个参数,一个是指向TnotifyIconData结构的指针,另一个是要添加、删除、改动图标的标志。通过该函函数将应用程序的图标添加到指示区中,使其作为图标运行,增加专业特色。当程序起动后,用鼠标右键点击图标,则弹出一个菜单,可选择sethook或endhook。unit kb;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms,
Dialogs,
StdCtrls, Menus,shellapi;
const
icon_id=1;
MI_iconevent=wm_user+1;//定义一个用户消息
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
sethook1: TMenuItem;
endhook1: TMenuItem;
N1: TMenuItem;
About1: TMenuItem;
Close1: TMenuItem;
Gettext1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure sethook1Click(Sender: TObject);
procedure endhook1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Close1Click(Sender: TObject);
private
{ Private declarations }
nid:tnotifyicondata;
normalicon:ticon;
public
{ Public declarations }
procedure icontray(var msg:tmessage);
message mi_iconevent;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function setkeyhook:bool;external 'keyspy.dll';
function endkeyhook:bool;external 'keyspy.dll';procedure tform1.icontray(var msg:tmessage);
var
pt:tpoint;
begin
if msg.lparam=wm_lbuttondown then
sethook1click(self);
if msg.LParam=wm_rbuttondown then
begin
getcursorpos(pt);
setforegroundwindow(handle);
popupmenu1.popup(pt.x,pt.y);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
normalicon:=ticon.create;
application.title:=caption;
nid.cbsize:=sizeof(nid);
nid.wnd:=handle;
nid.uid:=icon_id;
nid.uflags:=nif_icon or nif_message or nif_tip;
nid.ucallbackmessage:=mi_iconevent;
nid.hIcon :=normalicon.handle;
strcopy(nid.sztip,pchar(caption));
nid.uFlags:=nif_message or nif_icon or nif_tip;
shell_notifyicon(nim_add,@nid);
SetWindowLong(Application.Handle,
GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;procedure TForm1.sethook1Click(Sender: TObject);
begin
setkeyhook;
end;procedure TForm1.endhook1Click(Sender: TObject);
begin
endkeyhook;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
nid.uFlags :=0;
shell_notifyicon(nim_delete,@nid);
end;procedure TForm1.Close1Click(Sender: TObject);
begin
application.terminate;
end;
以下是一个按键地勾子:
Intercepting The TAB and ENTER Keys{the prototype for the new keyboard hook function}
function KeyboardHook(nCode: Integer; wParam: WPARAM;
lParam: LPARAM): LResult; stdcall;var
Form1: TForm1;
WinHook: HHOOK; // a handle to the keyboard hook functionimplementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin {install the keyboard hook function into the keyboard hook chain}
WinHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyboardHook, 0, GetCurrentThreadID);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
{remove the keyboard hook function from the keyboard hook chain}
UnhookWindowsHookEx(WinHook);
end;function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LResult;begin
{if we can process the hook information...}
if (nCode>-1) then
{...was the TAB key pressed?}
if (wParam=VK_TAB) then
begin
{if so, output a beep sound}
MessageBeep(0); {indicate that the message was processed}
Result := 1;
end
else
{...was the RETURN key pressed?} if (wParam=VK_RETURN) then
begin
{if so, and if the key is on the up stroke, cause
the focus to move to the next control}
if ((lParam shr 31)=1) then
Form1.Perform(WM_NEXTDLGCTL, 0, 0); {indicate that the message was processed}
Result := 1;
end
else
{otherwise, indicate that the message was not processed.} Result := 0
else
{we must pass the hook information to the next hook in the chain}
Result := CallNextHookEx(WinHook, nCode, wParam, lParam);
end;
Hook Win32 API 是一项有趣而实用的WINDOWS系统编程技术,应用领域十分广泛。虽然已经有不少的文章介绍过 Hook Win32 API 的方法了,我还是来作些简单的介绍,以便大家了解其工作原理。 Hook Win32 API 是什么意思?就是钩住Win32 API;那又何谓“钩”呢?就是绕弯的意思,让Win32 API函数的调用先绕一个弯路,在它执行实际功能之前,我们可以先做一些“预处理”,这样我们可以监视或定制某个Win32 API的调用,以实现一些特殊的功能。至于具体可以实现些什么样的功能,那就取决于程序设计者的想象力了。 为什么要Hook Win32 API呢?因为在很多情况下,我们想监视或改变某个应用程序的一些特定的行为,但是那个应用程序却没有提供相应的接口,而我们又几乎不可能得到其源代码,怎么办呢?因为大多数WINDOWS引用程序的行为很大程度上依赖于Win32 API,所以我们可以采用Hook Win32 API的方式来试图监视和改变应用程序的行为。 如何Hook Win32 API呢?实际上Win32 API是由一组动态链接库实现的,使用动态链接库是为了尽可能的共享内存。由于动态链接库是动态装入的,所以Win32 API函数的入口点也是动态确定的。当WINDOWS应用程序在调用Win32 API的时候,并不是直接调用某个函数地址,而是调用某处所存储的一个动态确定的函数地址来实现间接调用地,该处被命名为Import Address Table(简称IAT)。知道了这一点,接下来要做的就是想办法找到这个存储单元的位置,然后将其内容替换为接管函数的入口地址,不过得事先保存原函数的入口地址,以便执行了接管函数的代码后,可以在适当的地方以适当的方式再调用原函数。最后退出的时候或是不想再钩着它的时候,再将其恢复为原函数的入口地址。这就是Hook Win32 API的基本步骤,具体实现过程这里就不赘述了,可以参阅《WINDOWS 核心编程》(Jeffrer Richter著)