做个API拦截
如何做个API拦截呢```谁可以告诉我一下 做好是用软件做个API的拦截
参考答案:是用API作键盘信息的拦截吗?那要应用到键盘钩子,你要我可以把代码发给你(代码是网上摘录的)。但是你要求“用软件”,那就有点问题了,除非你说明要拦截什么按键,否则只有你自己修改代码了。
'.bas模块中
Public m_hDllKbdHook As Long 'public variable holding
'the handle to the hook procedure
Public Const WH_KEYBOARD_LL As Long = 13 'enables monitoring of keyboard
'input events about to be posted
'in a thread input queue
Private Const HC_ACTION As Long = 0 'wParam and lParam parameters
'contain information about a
'keyboard message
Public Const VK_CAPITAL As Long = &H14
Public Const VK_NUMLOCK As Long = &H90
Public Const VK_SCROLL As Long = &H91
Private Const VK_TAB = &H9 'virtual key constants
Private Const VK_CONTROL = &H11
Private Const VK_ESCAPE = &H1B
Private Const VK_F1 = &H70
Private Const VK_F2 = &H71
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 'test the transition-state flag
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
Call CopyMemory(kbdllhs, ByVal lParam, Len(kbdllhs))
'Ctrl+F1 --------------
If (kbdllhs.vkCode = VK_F1) And _
CBool(GetAsyncKeyState(VK_CONTROL) _
And &H8000) Then
LowLevelKeyboardProc = 1
Clipboard.Clear
Clipboard.SetText (Form1.Text1.Text)
Exit Function
End If 'kbdllhs.vkCode = VK_F1
'Ctrl+F2 --------------
If (kbdllhs.vkCode = VK_F2) And _
CBool(GetAsyncKeyState(VK_CONTROL) _
And &H8000) Then
LowLevelKeyboardProc = 1
Clipboard.Clear
Clipboard.SetText (Form1.Text2.Text)
Exit Function
End If 'kbdllhs.vkCode = VK_F2
'Alt+Esc --------------
If (kbdllhs.vkCode = VK_ESCAPE) And _
CBool(kbdllhs.flags And _
LLKHF_ALTDOWN) Then
LowLevelKeyboardProc = 1
Exit Function
End If 'kbdllhs.vkCode = VK_ESCAPE
End If 'nCode = HC_ACTION
LowLevelKeyboardProc = CallNextHookEx(m_hDllKbdHook, _
nCode, _
wParam, _
lParam)
End Function
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