SetWindowsHookEx Problems menu

Shout-Out

User Tag List

Results 1 to 1 of 1
  1. #1
    abystus's Avatar Member
    Reputation
    1
    Join Date
    Apr 2008
    Posts
    13
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SetWindowsHookEx Problems

    [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
    Last edited by abystus; 02-01-2010 at 07:52 PM. Reason: Update to VB and Framework Version

    SetWindowsHookEx Problems

Similar Threads

  1. Problem with CE.
    By Eldretch in forum World of Warcraft General
    Replies: 1
    Last Post: 08-08-2006, 06:49 PM
  2. realm list problem
    By robtuner in forum World of Warcraft General
    Replies: 2
    Last Post: 07-21-2006, 09:08 AM
  3. I have problem with BHW 3.0
    By sunrize1 in forum World of Warcraft General
    Replies: 1
    Last Post: 07-17-2006, 08:49 AM
  4. wow emu problem
    By bezike in forum World of Warcraft General
    Replies: 0
    Last Post: 07-09-2006, 04:45 PM
  5. Site problems
    By Shanaar in forum Community Chat
    Replies: 10
    Last Post: 05-14-2006, 01:15 AM
All times are GMT -5. The time now is 04:57 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search