我写了下面这个hook,但是在取得hInst的时候总是失败(hinst为0),请大家帮忙看看 Public Sub Hook() Dim hInst As Long Dim Thread As Long Dim hwnd As Long
'Set up the CBT hook hwnd = FindWindow("XLMAIN", Application.Caption) hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
If (hInst = 0) Then MsgBox "GetWindowLong 失敗" End If
Thread = GetCurrentThreadId()
If IsHooked Then MsgBox "Don't hook it twice without " & _ "unhooking, or you will be unable to unhook it." Else glngHwndProc = SetWindowsHookEx(WH_KEYBOARD, AddressOf ワード暗誦, hInst, Thread) IsHooked = True End If End Sub
Public Sub Hook()
Dim hInst As Long
Dim Thread As Long
Dim hwnd As Long
'Set up the CBT hook
hwnd = FindWindow("XLMAIN", Application.Caption)
hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
If (hInst = 0) Then
MsgBox "GetWindowLong 失敗"
End If
Thread = GetCurrentThreadId()
If IsHooked Then
MsgBox "Don't hook it twice without " & _
"unhooking, or you will be unable to unhook it."
Else
glngHwndProc = SetWindowsHookEx(WH_KEYBOARD, AddressOf ワード暗誦, hInst, Thread)
IsHooked = True
End If
End Sub
追加一个const定义就可以了。
但是有个新问题:
WH_KEYBOARD,GWL_HINSTANCE是不应该由我来定义的,是不是要在程序中包含某个定义文件阿?
这些好象是Win32 API规定的常量,在VB的API函数浏览器中可以查到它们的声明。
在MSDN中也可以查到相应的宏定义。