Syntax
HHOOK SetWindowsHookEx(
int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId );
Parameters
idHook
- [in] 指定要安装的钩子的类型.可以是以下一个值:
lpfnWH_CALLWNDPROC
- WH_CALLWNDPROCRET
安装 之前 , 系统会将 它们 发送 到 目标 窗口 过程 监视 消息的 钩子。 更多的 信息 , 请参阅 CallWndProc 挂钩 过程。- WH_CBT
安装 一个 钩子 监视 消息 后 他们 已 由 目标 窗口 过程 处理。 更多的 信息 , 请参阅 CallWndRetProc 挂钩 过程。- WH_DEBUG
安装 一个 钩子 接收 通知 对 基于计算机 培训 (CBT) 的 应用程序 很有用。 更多的 信息 , 请参阅 CBTProc 挂钩 过程。- WH_FOREGROUNDIDLE
安装 一个 钩子 调试 其他 钩子 程序 非常有用。 有关 详细 信息 , 请参阅 挂钩 过程 DebugProc- Installs a hook procedure that will be called when the application's foreground thread is about to become idle. This hook is useful for performing low priority tasks during idle time. For more information, see the ForegroundIdleProc hook procedure.
WH_GETMESSAGE- WH_JOURNALPLAYBACK
安装 一个 钩子 监视 消息 发送 到 消息 队列。 更多的 信息 , 请参阅 GetMsgProc 挂钩 过程。- WH_JOURNALRECORD
安装 一个 钩子 的 员额 由 WH_JOURNALRECORD 挂接 程序 以前 记录的 消息。 更多的 信息 , 请参阅 指定 挂接 程序。- WH_KEYBOARD
安装 一个 钩子 记录 输入 到 系统 消息 队列 发布 消息。 此 钩 可 用于 录制的 宏。 更多的 信息 , 请参阅 JournalRecordProc 挂钩 过程。- WH_KEYBOARD_LL
安装 一个 钩子 监视 键盘 消息。 更多的 信息 , 请参阅 KeyboardProc 挂钩 过程。- Windows NT/2000/XP:
WH_MOUSE安装 一个 钩子 监视 低级 键盘 输入的 事件。 更多的 信息 , 请参阅 LowLevelKeyboardProc 挂钩 过程。- WH_MOUSE_LL
安装 一个 钩子子程用 来 监视 鼠标 消息。 更多的 信息 , 请参阅 MouseProc 挂钩 过程。- Windows NT/2000/XP:
WH_MSGFILTER安装 一个 钩子 监视 低级 鼠标 输入的 事件。 更多的 信息 , 请参阅 LowLevelMouseProc 挂钩 过程。- WH_SHELL
安装 一个 钩子 监视 对话框 、 消息 框 、 菜单 或 滚动 栏 中 输入 事件 生成的 消息。 更多的 信息 , 请参阅 MessageProc 挂钩 过程。- WH_SYSMSGFILTER
安装 一个 钩子 接收 通知 到 外壳 应用程序 非常有用。 更多的 信息 , 请参阅 ShellProc 挂钩 过程。 安装 一个 钩子 监视 对话框 、 消息 框 、 菜单 或 滚动 栏 中 输入 事件 生成的 消息。 挂接 程序 监视 这些 相同的 桌面 作为 调用 线程 中的 所有 应用程序的 消息。 更多的 信息 , 请参阅 SysMsgProc 挂钩 过程。- [in]
hMod挂接 程序的 指针。 如果 dwThreadId 参数 为 零, 或 指定 不同的 进程 创建 的 线程 的 标识符 , lpfn 参数 必须 指向 一个 钩子 DLL 中。 否则 , lpfn 可以 指向 一个 钩子 与 当前 进程 关联 的 代码 中。- [in]
dwThreadId包含 由 lpfn 参数所 指向的 挂接 程序 的 DLL 的 句柄。 如果 参数 dwThreadId 指定 由 当前 进程 创建 一个 线程 , 如果 挂接 程序 内 与 当前 进程 关联 的 代码, 必须 将 hMod 参数 设置 为 NULL。- [in]
包含 由 lpfn 参数所 指向的 挂接 程序 的 DLL 的 句柄。 如果 参数 dwThreadId 指定 由 当前 进程 创建 一个 线程 , 如果 挂接 程序 内 与 当前 进程 关联 的 代码, 必须 将 hMod 参数 设置 为 NULL。
Return Value
如果成功,返回钩子句柄
如果失败,返回NULL
Remarks
SetWindowsHookEx can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names.
An error may occur if the hMod parameter is NULL and the dwThreadId parameter is zero or specifies the identifier of a thread created by another process.
Calling the CallNextHookEx function to chain to the next hook procedure is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.
Before terminating, an application must call the UnhookWindowsHookEx function to free system resources associated with the hook.
The scope of a hook depends on the hook type. Some hooks can be set only with global scope; others can also be set for only a specific thread, as shown in the following table.
Hook Scope WH_CALLWNDPROC Thread or global WH_CALLWNDPROCRET Thread or global WH_CBT Thread or global WH_DEBUG Thread or global WH_FOREGROUNDIDLE Thread or global WH_GETMESSAGE Thread or global WH_JOURNALPLAYBACK Global only WH_JOURNALRECORD Global only WH_KEYBOARD Thread or global WH_KEYBOARD_LL Global only WH_MOUSE Thread or global WH_MOUSE_LL Global only WH_MSGFILTER Thread or global WH_SHELL Thread or global WH_SYSMSGFILTER Global only For a specified hook type, thread hooks are called first, then global hooks.
The global hooks are a shared resource, and installing one affects all applications in the same desktop as the calling thread. All global hook functions must be in libraries. Global hooks should be restricted to special-purpose applications or to use as a development aid during application debugging. Libraries that no longer need a hook should remove its hook procedure.
Windows 95/98/Me: SetWindowsHookEx is supported by the Microsoft Layer for Unicode (MSLU). However, it does not make conversions. To see Unicode messages, notifications, and so forth, you must subclass the window. To use this version of the API, you must add certain files to your application, as outlined in Installing and Releasing Hook Procedures.