# system keyboard hook 再问

Posted by cww on January 18, 1998 at 23:10:12:

您好:
上回有请教过keyboard hook的问题，后来，我发现task hook有成功，于是
想改变成system hook，于是我写了一个activeX .dll Server. 两个method
去enable keyboard hook 与disable掉，而keyboard hook function则放在
.dll project 中的一个.bas中,而后制成hooks.dll。
而我开另一个专案，引用方才的hooks server，发现，在这个专案中，我按
keyboard 后，结果是我想要的，但在其他的window (如:桌面)下，则会产
生错误，是我想做system hook的方向有误，还是可能什么原因?
程式不长，故post上来，有空时，烦请给予指导。谢谢

以下是kbhooks.cls
Private Declare Function SetWindowsHookEx Lib "user32" Alias _ "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal _ hmod As Any, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Const WH_KEYBOARD = 2

Public Sub UnHookKBD()
If hnexthookproc 0 Then
UnhookWindowsHookEx hnexthookproc
hnexthookproc = 0
End If
End Sub

Public Function EnableKBDHook()
If hnexthookproc 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(WH_KEYBOARD, AddressOf _ KeyboardHookHandler, App.hInstance, 0)
'^^^^^^^^^^^^^^
'我怀疑这里有问题，但不知如何更改较对
If hnexthookproc 0 Then
EnableKBDHook = hnexthookproc
End If
End Function

以下是 hook.bas 为keyboard hook procedure

Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Const VK_CONTROL = &H11
Const KeyPressMask = &H80000000
Public hnexthookproc As Long

Public Function KeyboardHookHandler(ByVal iCode As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
KeyboardHookHandler = 0
If iCode < 0 Then
KeyboardHookHandler = CallNextHookEx(hnexthookproc, iCode, _ wParam, lParam)
Exit Function
End If
'侦测 Ctrl + B 组合键
If (lParam And KeyPressMask) = 0 Then
If (GetKeyState(VK_CONTROL) < 0) And (Chr(wParam) = "B") Then
KeyboardHookHandler = 1
Shell "c:\tools\spe3\pe2.exe", vbMaximizedFocus
End If
End If
End Function

Follow Ups:

- App.Instance 传回 0 小吴 17:31:54 1/19/98 (1)
  - Re: App.Instance 传回 0 cww 22:59:07 1/19/98 (0)