Syntax
HHOOK SetWindowsHookEx(
int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId );
Parameters
- idHook
- [in] 指定要安装的钩子的类型.可以是以下一个值:
- WH_CALLWNDPROC
安装之前,系统会将它们发送到目标窗口过程监视消息的钩子。更多的信息,请参阅 CallWndProc 挂钩过程。- WH_CALLWNDPROCRET
安装一个钩子监视消息后他们已由目标窗口过程处理。更多的信息,请参阅 CallWndRetProc 挂钩过程。- WH_CBT
安装一个钩子接收通知对基于计算机培训 (CBT) 的应用程序很有用。更多的信息,请参阅 CBTProc 挂钩过程。- WH_DEBUG
安装一个钩子调试其他钩子程序非常有用。有关详细信息,请参阅挂钩过程 DebugProc- WH_FOREGROUNDIDLE
- 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
安装一个钩子监视消息发送到消息队列。更多的信息,请参阅 GetMsgProc 挂钩过程。- WH_JOURNALPLAYBACK
安装一个钩子的员额由 WH_JOURNALRECORD 挂接程序以前记录的消息。更多的信息,请参阅指定挂接程序。- WH_JOURNALRECORD
安装一个钩子记录输入到系统消息队列发布消息。此钩可用于录制的宏。更多的信息,请参阅 JournalRecordProc 挂钩过程。- WH_KEYBOARD
安装一个钩子监视键盘消息。更多的信息,请参阅 KeyboardProc 挂钩过程。- WH_KEYBOARD_LL
- Windows NT/2000/XP:
安装一个钩子监视低级键盘输入的事件。更多的信息,请参阅 LowLevelKeyboardProc 挂钩过程。- WH_MOUSE
安装一个钩子子程用来监视鼠标消息。更多的信息,请参阅 MouseProc 挂钩过程。- WH_MOUSE_LL
- Windows NT/2000/XP:
安装一个钩子监视低级鼠标输入的事件。更多的信息,请参阅 LowLevelMouseProc 挂钩过程。- WH_MSGFILTER
安装一个钩子监视对话框、 消息框、 菜单或滚动栏中输入事件生成的消息。更多的信息,请参阅 MessageProc 挂钩过程。- WH_SHELL
安装一个钩子接收通知到外壳应用程序非常有用。更多的信息,请参阅 ShellProc 挂钩过程。- WH_SYSMSGFILTER
安装一个钩子监视对话框、 消息框、 菜单或滚动栏中输入事件生成的消息。挂接程序监视这些相同的桌面作为调用线程中的所有应用程序的消息。更多的信息,请参阅 SysMsgProc 挂钩过程。- lpfn
- [in]
挂接程序的指针。如果 dwThreadId 参数为零,或指定不同的进程创建的线程的标识符,lpfn 参数必须指向一个钩子 DLL 中。否则,lpfn 可以指向一个钩子与当前进程关联的代码中。- hMod
- [in]
包含由 lpfn 参数所指向的挂接程序的 DLL 的句柄。如果参数 dwThreadId 指定由当前进程创建一个线程,如果挂接程序内与当前进程关联的代码,必须将 hMod 参数设置为 NULL。- dwThreadId
- [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.