[VB.net 3.5 framework]
I am having a problem getting hHook to return anything other than 0 using the below code. I am trying to hook the message flow from another running process such as notepad (to my understanding this is how a wow bot is written to interact with the client). As far as I can tell the processid and threadid are returning properly as I can use showwindow and movewindow commands without a problem. Any help is appreciated as I have scoured google, msdn for an answer to my problem.
Code:
Imports System.Runtime.InteropServices
Public Delegate Function CallBack( _
ByVal nCode As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As Integer
Public Class Form1
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'Declare the mouse hook constant.
'For other hook types, obtain these values from Winuser.h in Microsoft SDK.
Dim WH_MOUSE As Integer = 7
Shared hHook As Integer = 0
'Keep the reference so that the delegate is not garbage collected.
Private hookproc As CallBack
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetWindowThreadProcessId(ByVal handle As IntPtr, <Out()> ByRef processId As Integer) As Integer
End Function
'Import for the SetWindowsHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function SetWindowsHookEx _
(ByVal idHook As Integer, ByVal HookProc As CallBack, _
ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function
'Import for the CallNextHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function CallNextHookEx _
(ByVal idHook As Integer, ByVal nCode As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
'Import for the UnhookWindowsHookEx function.
<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Overloads Shared Function UnhookWindowsHookEx _
(ByVal idHook As Integer) As Boolean
End Function
<DllImport("User32")> _
Private Shared Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
End Function
<DllImport("user32", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
Public Shared Function GetWindow(ByVal hwnd As IntPtr, ByVal uCmd As Integer) As IntPtr
End Function
'Point structure declaration.
<StructLayout(LayoutKind.Sequential)> Public Structure Point
Public x As Integer
Public y As Integer
End Structure
'MouseHookStruct structure declaration.
<StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct
Public pt As Point
Public hwnd As Integer
Public wHitTestCode As Integer
Public dwExtraInfo As Integer
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim localByName As Process() = Process.GetProcessesByName("notepad")
Dim ThreadID As Integer = GetWindowThreadProcessId(localByName(0).MainWindowHandle, localByName(0).Id)
If hHook.Equals(0) Then
hookproc = AddressOf MouseHookProc
hHook = SetWindowsHookEx(WH_MOUSE, _
hookproc, _
IntPtr.Zero, _
ThreadID)
If hHook.Equals(0) Then
MsgBox("SetWindowsHookEx Failed")
Return
Else
Button1.Text = "UnHook Windows Hook"
End If
Else
Dim ret As Boolean = UnhookWindowsHookEx(hHook)
If ret.Equals(False) Then
MsgBox("UnhookWindowsHookEx Failed")
Return
Else
hHook = 0
Button1.Text = "Set Windows Hook"
Me.Text = "Mouse Hook"
End If
End If
End Sub
Public Shared Function MouseHookProc( _
ByVal nCode As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As Integer
Dim MyMouseHookStruct As New MouseHookStruct()
Dim ret As Integer
If (nCode < 0) Then
Return CallNextHookEx(hHook, nCode, wParam, lParam)
End If
MyMouseHookStruct = CType(Marshal.PtrToStructure(lParam, MyMouseHookStruct.GetType()), MouseHookStruct)
Dim tempForm As Form
tempForm = Form.ActiveForm()
Dim strCaption As String
strCaption = "x = " & MyMouseHookStruct.pt.x & " y = " & MyMouseHookStruct.pt.y
tempForm.Text = strCaption
Return CallNextHookEx(hHook, nCode, wParam, lParam)
End Function
End Class