求API Hook 例子(VB的)
我2年前从网上疯狂搜索找到的全局键盘钩子的代码,当时研究了一下,反正是VB都没学全的菜鸟,头都晕了,到最后总算得了个知其然,当时自己用来编了一个拦截组合键并实行操作的程序。幸好钩子部分的代码还保存着,给你研究研究吧:
'|------------------------------|
'|下面是全局键盘钩子程序段的声明|
'|------------------------------|
Public LocalhHook As Long '局部键盘钩子变量
Public m_hDllKbdHook As Long '全局键盘钩子变量
Public Const WH_KEYBOARD_LL As Long = 13 'enables monitoring of keyboard
'input events about to be posted
'in a thread input queue
Public Const WM_KEYDOWN = &H100
Public Const WM_SYSKEYDOWN = &H104
Private Const HC_ACTION As Long = 0 'wParam and lParam parameters
'contain information about a 'keyboard message
Private Const VK_CONTROL = &H11 'virtual key constants
Private Const VK_ESCAPE = &H1B
Private Const VK_F1 = &H70
Private Const VK_F2 = &H71
Private Const VK_F3 = &H72
Private Const VK_F4 = &H73
Private Const LLKHF_EXTENDED = &H1& 'test the extended-key flag
Private Const LLKHF_INJECTED = &H10& 'test the event-injected flag
Private Const LLKHF_ALTDOWN = &H20& 'test the context code
Private Const LLKHF_UP As Long = &H80
Public Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Private Type KBDLLHOOKSTRUCT
vkCode As Long 'a virtual-key code in the range 1 to 254
scanCode As Long 'hardware scan code for the key
flags As Long 'specifies the extended-key flag,
'event-injected flag, context code,
'and transition-state flag
time As Long 'time stamp for this message
dwExtraInfo As Long 'extra info associated with the message
End Type
Public Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal cb As Long)
Public Declare Function GetKeyboardState Lib "user32" _
(kbArray As KeyboardBytes) As Long
Public Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'
'下面的是全局键盘钩子程序段的代码
'
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Public Function LowLevelKeyboardProc(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Static kbdllhs As KBDLLHOOKSTRUCT
If nCode = HC_ACTION Then
If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs))
'F1 --------------
If kbdllhs.vkCode = VK_F1 Then
If Form1.Text1(0).Text = "" Then
LowLevelKeyboardProc = 0 '值为 0 表示不拦截该信息
Exit Function
Else
LowLevelKeyboardProc = 1 '值为 1 表示拦截该信息
Clipboard.Clear '清空剪贴板
Clipboard.SetText (Form1.Text1(0).Text)
SendKeys "^(V)" '向当前活动窗口发送 Ctrl+V 组合键
Exit Function
End If
End If 'kbdllhs.vkCode = VK_F1
'F2 --------------
If kbdllhs.vkCode = VK_F2 Then
If Form1.Text1(1).Text = "" Then
LowLevelKeyboardProc = 0
Exit Function
Else
LowLevelKeyboardProc = 1
Clipboard.Clear
Clipboard.SetText (Form1.Text1(1).Text)
SendKeys "^(V)"
Exit Function
End If
End If 'kbdllhs.vkCode = VK_F2
'F3 --------------
If kbdllhs.vkCode = VK_F3 Then
If Form1.Text1(2).Text = "" Then
LowLevelKeyboardProc = 0
Exit Function
Else
LowLevelKeyboardProc = 1
Clipboard.Clear
Clipboard.SetText (Form1.Text1(2).Text)
SendKeys "^(V)"
Exit Function
End If
End If 'kbdllhs.vkCode = VK_F3
End If 'wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN
End If 'nCode = HC_ACTION
LowLevelKeyboardProc = CallNextHookEx(m_hDllKbdHook, nCode, wParam, lParam)
End Function
========补充一下,实现全局键盘hook的代码就这么多了=======
上面的全是模块里面的代码,下面的是Form里面的代码
Private Sub Form_Load()
Dim kbdState As KeyboardBytes
'装载钩子
Call GetKeyboardState(kbdState)
'set and obtain the handle to the keyboard hook
m_hDllKbdHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, App.hInstance, 0&)
If m_hDllKbdHook = 0 Then
MsgBox "Failed to install low-level keyboard hook."
End If
End Sub
'还有卸载钩子的:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If m_hDllKbdHook <> 0 Then
Call UnhookWindowsHookEx(m_hDllKbdHook)
End If
End Sub