首页 > 代码库 > VB6的HOOK技术
VB6的HOOK技术
代码背景,自身程序的窗口上有一个TextBox,Hook住WH_CALLWNDPROC用来截获EN_CHNAGE即文本变更的消息。
*这个其实用SetWindowLong和CallWindowProc也能做到,原理是一样的就是预处理窗口消息。
Form1.frm
Private Sub Command1_Click() hHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf CallBackHookProc, App.hInstance, App.ThreadID) Debug.Print hHookEnd SubPrivate Sub Command2_Click() Call UnhookWindowsHookEx(hHook)End Sub
Module1.bas
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 LongPublic Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As LongPublic Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _ ByVal nCode As Long, _ ByVal wParam As Long, _ lParam As Any) As LongPublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _ Source As Any, _ ByVal Length As Long)Public Const WH_CALLWNDPROC = 4Public Const WM_COMMAND = &H111Public Const EN_CHANGE = &H300Public Type CWPSTRUCT lParam As Long wParam As Long message As Long hwnd As LongEnd TypePublic Const HC_ACTION = 0Public hHook As LongPublic Function CallBackHookProc(ByVal nCode As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long If nCode = HC_ACTION Then Dim Msg As CWPSTRUCT Call CopyMemory(Msg, ByVal lParam, Len(Msg)) If Msg.message = WM_COMMAND Then If (Msg.wParam And &HFFFF0000) / &H10000 = EN_CHANGE Then Debug.Print "GOTCHA" End If End If End If CallBackHookProc = CallNextHookEx(hHook, nCode, wParam, lParam)End Function
运行代码,点击Command1后挂载HOOK,当TextBox里的文本改变时,会打印出“GOTCHA”。
由于,我窗体上只有一个TextBox是Edit控件,所以我在代码中就没有判断消息源(hwnd)是不是TextBox。
VB6的HOOK技术
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。