Hi,
im tyring to use the windows api to hook another process (by injecting the function into the process) but the callback function does not fire.
ive been looking for .net versions of the calls but couldnt find any - so these are my conversions from vb6 - maybe i have some of the datatypes/structures wrong?
heres the code
im tyring to use the windows api to hook another process (by injecting the function into the process) but the callback function does not fire.
ive been looking for .net versions of the calls but couldnt find any - so these are my conversions from vb6 - maybe i have some of the datatypes/structures wrong?
heres the code
Code:
#Region " Delegates "
Private Delegate Function HookProcDelegate( _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As CWPSTRUCT) As Integer
Private Delegate Function enumWindowsDelegate(ByVal hWnd As Integer, _
ByVal lParam As Integer) As Boolean
#End Region
#Region "Declarations - Constants"
Private Const HC_ACTION As Integer = 0
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYDOWN As Integer = &H104
Private Const WM_SYSKEYUP As Integer = &H105
Private Const WH_CALLWNDPROC = 4
Private Const WM_CREATE = &H1
Private Const EM_REPLACESEL = &HC2
Public Structure CWPSTRUCT
Dim lParam As Integer
Dim wParam As Integer
Dim message As Integer
Dim hwnd As Integer
End Structure
Public Enum HookConstants
HC_ACTION = 0
HC_GETNEXT = 1
HC_SKIP = 2
HC_NOREMOVE = 3
HC_NOREM = HC_NOREMOVE
HC_SYSMODALOFF = 5
HC_SYSMODALON = 4
End Enum
#End Region
#Region "Declarations - Win32 APIs"
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" ( _
ByVal idHook As Integer, _
ByVal lpfn As HookProcDelegate, _
ByVal hmod As Integer, _
ByVal dwThreadId As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As CWPSTRUCT) As Integer
Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As Integer) As Integer
Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
Private Declare Function EnumWindows Lib "user32.dll" ( _
ByVal lpEnumFunc As enumWindowsDelegate, ByVal lParam As Integer) As Integer
#End Region
Public hModule As Integer
Public hParent As Integer
Public dwThreadID As Integer
Public hHook As Integer
Private hp As HookProcDelegate
Public Function startCapture() As Boolean
Dim processID As Integer
hModule = Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32
find the exe process threadid
Dim p As Process
For Each p In Process.GetProcesses()
If p.ProcessName = "myExeToBeHooked.exe" Then
processID = p.Id
Exit For
End If
Next
pass the process id to the enumWindows
EnumWindows(AddressOf EnumWindowsProc, processID)
hp = AddressOf CallWndProc
hHook = SetWindowsHookEx(WH_CALLWNDPROC, hp, hModule, dwThreadID)
Return True
End Function
Public Function EnumWindowsProc(ByVal HWND As Integer, ByVal LPARAM As Integer) As Boolean
Dim dwProcessID As Integer
dwThreadID = GetWindowThreadProcessId(HWND, dwProcessID)
If dwProcessID = LPARAM Then
hParent = HWND
Return False
End If
Return True
End Function
Public Function CallWndProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As CWPSTRUCT) As Integer
this does not fire
Return (CallNextHookEx(0, nCode, wParam, lParam))
End Function
Public Function stopCapture() As Boolean
Remove the hook
If hHook <> 0 Then
UnhookWindowsHookEx(hHook)
End If
Return True
End Function