none
【VB6】請問Hook(WH_CBT-systemwide)的callback如何寫? RRS feed

  • 一般討論

  •  

    最近學到了Hook,也嘗試過讓自己的vb程式去hook( KeyBoard之類的)
    但是如果要hook其他程式,那麼就得用systemwide的hook(dll注入的方式……)
    vb6的activeX dll不適合做注入的dll,於是我參考了網上的一些方法,就是複製幾個obj檔案,然後去用Link.exe自行編譯dll。

    假設有兩個obj檔案,一個是class1.obj (3.46KB),一個是wndHook.obj (21.6KB)

    另外我還複製了VB6資料夾裏的VBAEXE6.LIB,和Link.exe擺在同一個資料夾裏。

    我寫了個bat去調用Link.exe編譯:

     

    Bat檔的Code:

    link.exe "Class1.obj" "wndHook.OBJ" "VBAEXE6.LIB" /ENTRY:__vbaS /EXPORT:HookFunction  /OUT:"hk.dll" /BASE:0x11000000 /SUBSYSTEM:WINDOWS,4.0 /VERS

     
    另外,我的Class檔案寫了如下的Code:

     

    Class1.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 Const HCBT_ACTIVATE = 5
     
    Public Function HookFunction(ByVal iCode As Long, _
         ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim pId As Long
        If wParam <> 0 Then
            If iCode = HCBT_ACTIVATE Then
                '視窗Active時要做的事
            End If
        End If
        'HookFunction = CallNextHookEx(hHook, nCode, wParam, lParam)
       Call CallNextHookEx(hHook, iCode, wParam, lParam)
     End Function

     

    就這樣,把從class1提取出來的obj檔案都用bat檔編譯出hk.dll來了

    還有,也看看這裡,是我Set Hook的Code:

     

    Module1.bas

    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

     

    Declare Function UnhookWindowsHookEx Lib "user32" _
            (ByVal hHook As Long) As Long

     

    Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" _
            (ByVal lpLibFileName As String) As Long
           
    Declare Function GetProcAddress Lib "kernel32" _
            (ByVal hModule As Long, ByVal lpProcName As String) As Long
     
    Public Const WH_CBT = 5
     
    Public hHook As Long
     
    Public Sub Hook()
        Dim hHookDLL As Long
        Dim pHookFunction As Long
       
        hHookDLL = LoadLibrary("hk.dll")
        If hHookDLL = 0 Then
            End
        End If

        pHookFunction = GetProcAddress(hHookDLL, "HookFunction")
        hHook = SetWindowsHookEx(WH_CBT, pHookFunction, hHookDLL, 0)
      
    End Sub
     
    Public Sub NonHook()
    If hHook <> 0 Then
        suc = UnhookWindowsHookEx(hHook)
        hHook = 0
    End If
    End Sub

     

    Form1.frm

    Private Sub Form_Load()

    Hook
    End Sub

     

    Private Sub Form_Unload(Cancel As Integer)
    NonHook
    End Sub

     

    我的問題是(對不起,上面內容比較長,辛苦了大家~):

    參看class1的code,有一句

    引用自class1.cls
    Call CallNextHookEx(hHook, iCode, wParam, lParam)

     

    請問hHook該填什麽?

    如果不需要注入dll的話,可以用bas裡面的hHook,但是在dll裏,callnexthookex無法取得hook handle(hHook在bas裏),那該怎麼辦?

    忽略不寫這句,又hook不了(程式自動掛。我這裡什麽都不寫,只寫一行,希望能夠注入各windows程式內、不掛就行了,如能注入,以後再寫code),其實我的目的只是要監視windows的窗口焦點的轉變而已,因為不想用timer,所以就嘗試hook了,除了timer和hook的方法以外,若有其他方法可以達到這個目的(視窗焦點轉變>觸發事件),小弟必洗耳恭聽,煩請指教、相助。

     

    謝謝大家。

    2008年4月8日 上午 08:13