iczelion tut24[1]

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

本文简介:选择自 jimgreen 的 blog

tutorial 24: windows hooks


we will learn about windows hooks in this tutorial. windows hooks are very powerful. with them, you can poke inside other processes and sometimes alter their behaviors.
download the example here.

theory:

windows hooks can be considered one of the most powerful features of windows. with them, you can trap events that will occur, either in your own process or in other processes. by "hooking", you tell windows about a function, filter function also called hook procedure, that will be called everytime an event you're interested in occurs. there are two types of them: local and remote hooks.
  • local hooks trap events that will occur in your own process.
  • remote hooks trap events that will occur in other process(es). there are two types of remote hooks
    • thread-specific  traps events that will occur in a specific thread in other process. in short, you want to observe events in a specific thread in a specific process.
    • system-wide  traps all events destined for all threads in all processes in the system.
when you install hooks, remember that they affect system performance. system-wide hooks are the most notorious. since all related events will be routed through your filter function, your system may slow down noticeably. so if you use a system-wide hook, you should use it judiciously and unhook it as soon as you don't need it. also, you have a higher chance of crashing the other processes since you can meddle with other processes and if something is wrong in your filter function, it can pull the other processes down to oblivion with it. remember: power comes with responsibility.
you have to understand how a hook works before you can use it efficiently. when you create a hook, windows creates a data structure in memory, containing information about the hook, and adds it to a linked list of existing hooks. new hook is added in front of old hooks. when an event occurs, if you install a local hook, the filter function in your process is called so it's rather straightforward. but if it's a remote hook, the system must inject the code for the hook procedure into the address space(s) of the other process(es). and the system can do that only if the function resides in a dll. thus , if you want to use a remote hook, your hook procedure must reside in a dll. there is two exceptions to this rule: journal record and journal playback hooks. the hook procedures for those two hooks must reside in the thread that installs the hooks. the reason why it must be so is that: both hooks deal with the low-level interception of hardware input events. the input events must be recorded/playbacked in the order they appeared. if the code of those two hooks is in a dll, the input events may scatter among several threads and it is impossible to know the order of them. so the solution: the hook procedure of those two hooks must be in a single thread only i.e. the thread that installs the hooks.
there are 14 types of hooks:
  • wh_callwndproc  called when sendmessage is called
  • wh_callwndprocret  called when sendmessage returns
  • wh_getmessage   called when getmessage or peekmessage is called
  • wh_keyboard  called when getmessage or peekmessage retrieves wm_keyup or wm_keydown from the message queue
  • wh_mouse  called when getmessage or peekmessage retrieves a mouse message from the message queue
  • wh_hardware called when getmessage or peekmessage retrieves some hardware message that is not related to keyboard or mouse.
  • wh_msgfilter  called when a dialog box, menu or scrollbar is about to process a message. this hook is local. it's specifically for those objects which have their own internal message loops.
  • wh_sysmsgfilter  same as wh_msgfilter but system-wide
  • wh_journalrecord  called when windows retrieves message from the hardware input queue
  • wh_journalplayback  called when an event is requested from the system's hardware input queue.
  • wh_shell  called when something interesting about the shell occurs such as when the task bar needs to redraw its button.
  • wh_cbt  used specifically for computer-based training (cbt).
  • wh_foregroundidle used internally by windows. little use for general applications
  • wh_debug  used to debug the hooking procedure
now that we know some theory, we can move on to how to install/uninstall the hooks.

本文关键:iczelion asm
  相关方案
Google
 

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

go top