HOOK,钩子,教程Hook Procedures (钩子子程)
To take advantage of a particular type of hook, the developer provides a hook procedure and uses the SetWindowsHookEx function to install it into the chain associated with the hook. A hook procedure must have the following syntax:
为了利用某种特定类型的钩子,开发者提供了钩子子程。可以使用SetWindowsHookEx方法将该钩子子程安装到和该钩子相关联的钩子链表中。钩子子程必须具有下面的语法:
LRESULT CALLBACK HookProc
(
int nCode,
WPARAM wParam,
LPARAM lParam
);
HookProc is a placeholder for an application-defined name.
HookProc是应用程序定义的名字的占位符。即,HookProc是应用程序定义的名字。
The nCode parameter is a hook code that the hook procedure uses to determine the action to perform. The value of the hook code depends on the type of the hook; each type has its own characteristic set of hook codes. The values of the wParam and lParam parameters depend on the hook code, but they typically contain information about a message that was sent or posted..
参数nCode:钩子代码,钩子子程通过该代码来决定执行什么动作。该值取决于钩子的类型,每种类型都拥有自己特有的钩子代码集合。
参数wParam和lParam的值,都取决于钩子代码。但是一般都包含发送或者传递的消息的信息。
The SetWindowsHookEx function always installs a hook procedure at the beginning of a hook chain. When an event occurs that is monitored by a particular type of hook, the system calls the procedure at the beginning of the hook chain associated with the hook. Each hook procedure in the chain determines whether to pass the event to the next procedure. A hook procedure passes an event to the next procedure by calling the CallNextHookEx function.
方法SetWindowsHookEx总是在钩子链的开始位置安装钩子子程。当被某种类型的钩子监视的事件发生时,系统调用和该钩子相关的位于钩子链表开始位置的钩子子程。每个钩子链表中的钩子子程决定是否将该事件传递给下一个钩子子程。钩子子程通过调用方法CallNextHookEx向下一个钩子子程传递事件。
Note that the hook procedures for some types of hooks can only monitor messages. the system passes messages to each hook procedure, regardless of whether a particular procedure calls CallNextHookEx.
注意:某些类型的钩子子程仅仅能够监视消息,系统不管是否有特殊的钩子子程调用CallNextHookEx方法,都将把消息传递给每个钩子子程,
A global hook monitors messages for all threads in the same desktop as the calling thread. A thread-specific hook monitors messages for only an individual thread. A global hook procedure can be called in the context of any application in the same desktop as the calling thread, so the procedure must be in a separate dynamic-link library (DLL) module. A thread-specific hook procedure is called only in the context of the associated thread. If an application installs a hook procedure for one of its own threads, the hook procedure can be in either the same module as the rest of the application's code or in a DLL. If the application installs a hook procedure for a thread of a different application, the procedure must be in a DLL. For information, see Dynamic-Link Libraries.
上面的字面意思为:全局钩子监视同一桌面下所有做为调用线程的线程消息。线程钩子仅仅监视该单个线程的消息。全局钩子子程可以在所在桌面下任何应用程序的上下文中被调用,因此,该钩子子程序须在一个单独的动态链接库DLL中。(译者注:在DLL中,可以映射到内存中,从而被所有程序调用)。线程钩子的钩子子程只能在本线程的上下文中被调用。如果应用程序为他自己线程中的某个安装钩子子程,钩子子程能够放在本模块中、应用程序代码的其它部分、Dll中。如果应用程序为另外一个不同的应用程序安装钩子子程,钩子子程必须放在Dll中。参照 DLL 查找更多信息。
个人认为这里理解起来比较费劲。如果这样理解会更好:请参看钩子初接触(二)最后部分。呵呵。
Note You should use global hooks only for debugging purposes; otherwise, you should avoid them. Global hooks hurt system performance and cause conflicts with other applications that implement the same type of global hook.
注意:只应为了调试目的而使用全局钩子;否则应避免使用。全局钩子会牺牲系统性能。使用了相同类型的全局钩子的应用程序之间也会有冲突。
Helios 2007-4-8