# Re: App.Instance 传回 0

Posted by cww on January 19, 1998 at 22:59:07:

In Reply to: App.Instance 传回 0 posted by 小吴 on January 19, 1998 at 17:31:54:

: 解决方式就是主程式要传这个参数进去。你再试看看。

我依您的建议修改了一下EnableKBDHook，改传参数进入,
而在另一个启动KBD hook程式的专案中用以下方式呼叫

EnableKBDHook(App.HInstance)

而后将专案制成.exe档
结果，在这.exe档中按keyboard, 一切ok, 但切至其他window中
按keyboard, 则该程式出现无法预期的错误，而结束掉，但启动hook的.exe之 process 仍活得很好，直到我unhook keyboard后，其他的程式按keyboard时，才不会有错
以下是新版程式
>
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(Byval hAppHandle as long)
'___________________________________^^^^^^^^^^
'___________________________________改成由外部传入
'只是纳闷，该值是指向行程中被对映进来的.dll之位址，我觉得
'我传App.HInstance，似乎不太对，但如何做实在不知
If hnexthookproc =0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(WH_KEYBOARD, AddressOf _ KeyboardHookHandler, hAppHandle, 0)

If hnexthookproc = 0 Then
EnableKBDHook = hnexthookproc
End If
End Function

以下是 hook.bas 为keyboard hook procedure, 与KBhook.cls同一专专案

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: