将线程ID传递给SetWindowsHookEx [英] Passing Thread ID to SetWindowsHookEx
问题描述
我的两个API调用是:
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long
我的Hook键盘功能:
Public Sub HookKeyboard()
Dim ThreadID As Long
ThreadID = GetCurrentThreadId
KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardCallback, Application.Hinstance, ThreadID)
Call HookedInitalize
End Sub
由于某种原因,我的钩子没有抓住.如果我将ThreadID替换为0,表示该钩子在系统范围内,则该钩子会捕获.但是,当我尝试将其限制为仅在整个应用程序范围内时,它就不会起作用.
For some reason my hook is not catching. If I replace ThreadID with 0, indicating that the hook is to be system-wide, then the hook DOES catch. But when I try to limit it to just application wide, it does not catch.
推荐答案
WH_KEYBOARD_LL
是 global 钩子,因此您必须将0作为线程ID.它不会以任何其他方式起作用.
WH_KEYBOARD_LL
is a global hook, so you must pass 0 as the thread ID. It won't work any other way.
您可以在文档中看到此内容用于 SetWindowsHookExA
函数;具体来说,在备注"部分,其中枚举了钩子类型及其范围.
You can see this in the documentation for the SetWindowsHookExA
function; specifically, in the Remarks section, where it enumerates the hook types and their scopes.
还应注意:如果尝试使用非零线程ID调用 SetWindowsHookEx
后调用 Err.LastDllError
,您将看到错误 ERROR_GLOBAL_ONLY_HOOK
,提供相同的信息.
Also of note: if you were to call Err.LastDllError
after the attempt to call SetWindowsHookEx
with a non-zero thread ID, you would have seen the error ERROR_GLOBAL_ONLY_HOOK
, providing the same information.
这篇关于将线程ID传递给SetWindowsHookEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!