深入解析钩子和动态链接库[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 i_like_cpp 的 blog

深入解析钩子和动态链接库

ashao1981(翻译)

下载源代码 - 22 kb

  对于如何使用和创建钩子有许多的争议,这篇文章试图澄清这些问题。

  注意:如果你只是在自己的进程内使用钩子则不会有下面的问题, 这只发生在你使用系统钩子的时候。

关键问题在于 地址空间,dll函数中的代码所创建的任何对象(包括变量)都归调用它的线程或进程所有。当进程在载入dll时,操作系统自动把dll地址映射到该进程的私有空间,也就是进程的虚拟地址空间,而且也复制该dll的全局数据的一份拷贝到该进程空间。也就是说每个进程所拥有的相同的dll的全局数据,是私有的,dll成为进程的一部分,以这个进程的身份执行,使用这个进程的堆栈。这意味着数据会被重新初始化。典型地,它们将是零。

有人建议在dll上存放数据的地址。 这是不可能的。有人反对? 那好,这不是不可能的,但这是不可能有什么 用途 的。既使你创建的是对dll 的所有实例可见的共享内存变量,这一变量只有在储存它的进程中才有实际的意义。 对于所有其它的进程,这仅仅是一串比特位,并且如果你设法使用它作为地址,对于事件被拦截的进程而言,这个地址是完全无用甚至导致程序崩溃。

这个分开的地址空间的概念是一个难以掌握的概念。 让我使用图片说明它。





 我们这里有三个进程。 你的进程被显示在左边。 dll 中有代码,有数据,并且有一个共享的数据段。 现在当钩子dll 执行一次对进程a的事件拦截 ,系统自动把dll地址映射到该进程的私有空间,也就是进程的虚拟地址空间,而且也复制该dll的全局数据的一份拷贝到该进程空间。巧合地话,他们会被迁入到进程a 同样的虚拟地址。 进程a有它自己的私有的拷贝 数据段,那么进程a 在"data"中看见的要么是自己私有的 ,要么是无法影响其他进程的(或由其它进程影响的!). 这里麻烦的是 共享 数据段,(显示红色的。)在你的进程和进程a中指示的是同样的内存页面。 注意,巧合地话,这些内存页出现在同样的虚拟地址上。 如果对你的进程和进程a同时进行调试,并且注意看在共有的数据段中的 &something ,同时看看在进程中a中同样的 &something ,你会看到同样的数据,甚至于它们是在在同样的地址。 如果你使用调试器去改变,或许你会看到程序改变了 &something 的值,你可以转到另一进程,检查它,看看在那里出现的新值。


让我们看一看在进程b会发生什么 。 当事件在进程中b中被钩时,dll 被映射。代码被迁入到进程中b中另外的一个地址。如果你调试进程中b ,留意在共有的区域中的 &something,你会发现 &something 的地址是不同的,但 &something 的内容会是同样的; 在你的进程中或进程a中对&something的内容做的改变立刻就能在进程b中看见,即使进程b是在另獾囊桓龅刂罚ㄐ槟獾刂罚┛醇摹#?i>这是在同样的物理内存地点)。当我提到巧合时,"巧合" 是指被策划; windows总是试图将dll映射入同样的虚拟地址, 它试图这么干,但它很少成功。


这就意味着,如果你在dll中存放了一个指向回调函数的指针,但在实际运行进程a 或进程b时,它可能会指向别的地址。这也意味着你将不能在dll中使用mfc--它不能是一个扩展mfc dll或mfc dll,因为这些dll(动态链接库)会调用mfc 函数。


 那么mfc 函数在哪里? 他们是在你的地址空间, 而不是在进程a或进程b的 地址空间! 因为他们可能是用visual.basic ,java或其他语言写的 , 所以你必须写straight-c dll ,并且我建议你忽略整个c runtime library.,只使用api 。 用lstrcpy 代替 strcpy 或 tcscpy,用 lstrcmp 代替 strcmp 或 tcscmp,等等。

如何让你的dll与其controlling server 通信


 一种解答将使用 ::postmessage 或 ::sendmessage 函数。(我这里提到的是原始api 的调用,不是mfc 的调用!) 每当可能使用 ::postmessage时,尽可能使用它优先于使用 ::sendmessage。否则,如果你的进程不幸停止,因为大家都被阻拦在一个永远不会返回的::sendmessage,其他进程也将停止,然后是整个系统都停止。


你也可以考虑在共享内存区域使用信息队列,但那个题目在这篇文章范围之外。

在 ::sendmessage 或 ::postmessage中,你无法传回一个指针 (我们将忽略传回一个相对指针进入共享内存区域的问题; 那也是在这篇文章范围之外). 这是因为你能使用的任一指针指示的地址要么是在dll 中, 要么是在在被钩的进程中。(进程a 或进程b) 因此在你的进程中,这个指针是完全无用的。 你只能通过在 wparam 或 lparam中的信息传回地址空间。

i 我强烈 建议为此使用登记的窗口消息。 你能发送消息到 message_map(消息映射) 窗口,并在此使用 on_registered_message 宏指令。

现在关键是要得到 那个窗口的hwnd(句柄)。 幸运的是,这很容易。

你必须做的第一件事是创建共有的数据段。 所以我们使用 # pragma data_seg 声明。 使用某一好记的数据段名字(它必须是没有比8 个字符长) 。我想强调名字是任意的,这里使用了我自己的名字。 我发现如果我使用好的名字象 .share 或.shr 或.shrdata,别人会认为名字有特殊的意义。 但是,我要说no。



# pragma data_seg(".joe")

handle hwnd = null;

# pragma dta_seg()

# pragma comment(linker ,"/ section:.joe,rws ")



# pragma 声明一个数据段,在此范围内声明的变量在初始化后将被指派到该数据段, 假设他们初始化. 如未初始化,变量将被分配到缺省数据段,而# pragma不起作用。



初看起来, 这将阻止你在共有的数据段使用一些c++ 对象,因为你无法初始化c++中用户定义的对象。 这看来是一个根本局限。



# pragma comment 使连接器有命令行开关被显示增加到链接步骤。 你可以进入vc++ 项目| 设置 并且改变连接器命令行。



你可以预定某一机制设置窗口句柄,例如

void setwindow(hwnd w) {hwnd = w; }



但更经常的是如下所示的与钩子结合。

sample: a mouse hook
header file (myhook.h)


函数 setmyhook 并且 clearmyhook 必须在此被声明。这在我的另一文章中有详细论述。“the ultimate dll header file.”

#define uwm_mousehook_msg \
 _t("umw_mousehook-" \
"{b30856f0-d3dd-11d4-a00b-006067718d04}")
source file (myhook.cpp)
#include "stdafx.h"
#include "myhook.h"

#pragma data_seg(".joe")
hwnd hwndserver = null;
#pragma data_seg()
#pragma comment("linker, /section:.joe,rws")

hinstance hinstance;
uint hwm_mousehook;
hhook hook;

// forward declaration
static lresult callback msghook(int ncode, wparam wparam, lparam lparam);
/****************************************************************
* dllmain
* inputs:
* hinstance hinst: instance handle for the dll
* dword reason: reason for call
* lpvoid reserved: ignored
* result: bool
* true if successful
* false if there was an error (never returned)
* effect:
* initializes the dll.
****************************************************************/

bool dllmain(hinstance hinst, dword reason, lpvoid reserved)
{
switch(reason)
 { /* reason */
//**********************************************
// process_attach
//**********************************************
case dll_process_attach:
 // save the instance handle because we need it to set the hook later
 hinstance = hinst;
 // this code initializes the hook notification message
 uwm_mousehook = registerwindowmessage(uwm_mousehook_msg);
 return true;

//**********************************************
// process_detach
//**********************************************
case dll_process_detach:
 // if the server has not unhooked the hook, unhook it as we unload
 if(hwndserver != null)
clearmyhook(hwndserver);
 return true;
 } /* reason */
/****************************************************************
* setmyhook
* inputs:
* hwnd hwnd: window whose hook is to be set
* result: bool
* true if the hook is properly set
* false if there was an error, such as the hook already
* being set
* effect:
* sets the hook for the specified window.
* this sets a message-intercept hook (wh_getmessage)
* if the setting is successful, the hwnd is set as the
* server window.
****************************************************************/

__declspec(dllexport) bool winapi setmyhook(hwnd hwnd)
{
 if(hwndserver != null)
return false;
 hook = setwindowshookex(
 wh_getmessage,
 (hookproc)msghook,
 hinstance,
 0);
 if(hook != null)
 { /* success */
hwndserver = hwnd;
return true;
 } /* success */
 return false;
} // setmyhook

/****************************************************************
* clearmyhook
* inputs:
* hwnd hwnd: window whose hook is to be cleared
* result: bool
* true if the hook is properly unhooked
* false if you gave the wrong parameter
* effect:
* removes the hook that has been set.
****************************************************************/
__declspec(dllexport) bool clearmyhook(hwnd hwnd)
 {
if(hwnd != hwndserver)
 return false;
bool unhooked = unhookwindowshookex(hook);
if(unhooked)
 hwndserver = null;
return unhooked;
 }
/****************************************************************
*msghook
* inputs:
* int ncode: code value
* wparam wparam: parameter
* lparam lparam: parameter
* result: lresult
*
* effect:
* if the message is a mouse-move message, posts it back to
* the server window with the mouse coordinates
* notes:
* this must be a callback function or it will not work!
****************************************************************/

static lresult callback msghook(int ncode, wparam wparam, lparam lparam)
 {
// if the value of ncode is < 0, just pass it on and return 0
// this is required by the specification of hook handlers
if(ncode < 0)
{ /* pass it on */
 callnexthookex(hook, ncode,
 wparam, lparam);
 return 0;
} /* pass it on */

// read the document.tion to discover what wparam and lparam
// mean. for a wh_message hook, lparam is specified as being
// a pointer to a msg structure, so the code below makes that
// structure available

lpmsg msg = (lpmsg)lparam;

// if it is a mouse-move message, either in the client area or
// the non-client area, we want to notify the parent that it has
// occurred. note the use of postmessage instead of sendmessage
if(msg->message == wm_mousemove ||
 msg->message == wm_ncmousemove)
postmessage(hwndserver,
uwm_mousemove,
0, 0);

// pass the message on to the next hook
return callnexthookex(hook, ncode,
 wparam, lparam);
 } // msghook
the server application
在头文件中,将下面的增加到类的protected段:

afx_msg lresult onmymousemove(wparam,lparam);
在application 文件中, 增加以下代码到文件前部。

uint uwm_mousemove = ::registerwindowmessage(uwm_mousemove_msg);
在 message_map, 增加以下代码

//{afx_msg comments:

on_registered_message(uwm_mousemove, onmymousemove)
in your application file, add the following function:

lresult cmyclass::onmymousemove(wparam, lparam)
 {
// ...do stuff here
return 0;
 }




上面是我写的一个小程序。既然我为了钩子花了n+1st 时间,我干脆给它一个好的用户界面。 猫在窗口之内盯着老鼠。小心! 当老鼠足够接近猫时并且它将捉住老鼠!


你可以下载这个项目并建立它。 真正的关键是dll 子工程项目; 其他的都不过是陪衬。有几个其它的技术被用在这个例子里,包括各种各样的图画技术, clipcursor 和 setcapture的用法,区域选择、屏幕更新等等。,因此除了展示钩子函数的使用以外,对初级程序员掌握窗口样式设计编程也有一些价值。

本文关键:深入解析钩子和动态链接库
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top