鉤子教程 - 原理(四) : 鉤子子程

原文地址:http://www.zdexe.com/program/201004/578.htmlhtml

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:app

爲了利用某種特定類型的鉤子,開發者提供了鉤子子程。可使用SetWindowsHookEx方法將該鉤子子程安裝到和該鉤子相關聯的鉤子鏈表中。鉤子子程必須具備下面的語法:less

LRESULT CALLBACK HookProc
(
  int nCode, 
  WPARAM wParam, 
  LPARAM lParam
);

HookProc is a placeholder for an application-defined name.ide

HookProc是應用程序定義的名字的佔位符。即,HookProc是應用程序定義的名字。post

----------------------------------------------------------------------------------------------------------------------------------------------------性能

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 lParamparameters depend on the hook code, but they typically contain information about a message that was sent or posted..spa

參數nCode:鉤子代碼,鉤子子程經過該代碼來決定執行什麼動做。該值取決於鉤子的類型,每種類型都擁有本身特有的鉤子代碼集合。線程

參數wParam和lParam的值,都取決於鉤子代碼。可是通常都包含發送或者傳遞的消息的信息。debug

----------------------------------------------------------------------------------------------------------------------------------------------------調試

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方法,都將把消息傳遞給每一個鉤子子程,

----------------------------------------------------------------------------------------------------------------------------------------------------

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.

注意:只應爲了調試目的而使用全局鉤子;不然應避免使用。全局鉤子會犧牲系統性能。使用了相同類型的全局鉤子的應用程序之間也會有衝突。

相關文章
相關標籤/搜索