none
I cant figure out this exception

    Question

  • PInvokeStackImbalance occurred
    Message: Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\lanet\Documents\Visual Studio 2015\Projects\AutoClicker\AutoClicker\bin\Debug\AutoClicker.vshost.exe'.
    Additional information: A call to PInvoke function 'AutoClicker!AutoClicker.Form1::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

    Public Class Form1
        Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub
        Private Sub MyMethod()
            Cursor.Position = New System.Drawing.Point(Cursor.Position)
            mouse_event(&H2, 0, 0, 0, 1) 'cursor will go down (like a click)
            mouse_event(&H4, 0, 0, 0, 1) ' cursor goes up again
        End Sub

    does anyone know why i am getting this error

    Tuesday, March 14, 2017 3:47 PM

Answers

  •  That Exception is because you are using the wrong types in your mouse_event api function signature.  It looks like you found it from some old VB6 code which used Long types for most everything.  In VB.Net, the data types need to be changed to best match the unmanaged data types.

     Here is the correct VB.Net signature for the mouse_event api function and the constants for the Left, Middle, and Right mouse button events to use with it.  You will need to add the Import statement below at the top of your code for this signature too.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
        Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
        Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
        Private Const MOUSEEVENTF_RIGHTUP As Integer = &H10
        Private Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
        Private Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
    
        <DllImport("user32.dll", EntryPoint:="mouse_event")>
        Private Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As UInteger)
        End Sub
    
        'Your code....
    
    End Class


    If you say it can`t be done then i`ll try it

    Tuesday, March 14, 2017 6:28 PM

All replies

  • Hi

    Here is my attempt at a 'poor mans' autoclicker.

    *

    This example needs the Timer1.Interval to be chosen VERY carefully to avoid losing control all together. This example sets it to 2 seconds. This allows time to get to the STOP button. There are other ways to effect a STOP, such as keyboard key down etc, but I haven't bothered doing that in this example.

    *

    To try this code, you need a Form1 with Button1, Label1 and Panel1. When you click on the START, you have the time set in Timer.Interval to place cursor where you want it, AND, you only have that same time to get to the STOP button once running.

    ' Form1 with Button1, Label1 and Panel1
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Declare Function mouse_event Lib "user32.dll" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
        Dim mp As Point = Cursor.Position
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Button1.Text = "START"
            Label1.Text = "0"
    
            ' allow enough time to get to STOP button
            ' 2 seconds means you got to be quick
            ' if the form is hidden by other
            ' window(s)
            Timer1.Interval = 2000
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Static clicked As Boolean = False
            If clicked Then
                Timer1.Stop()
                Button1.Text = "START"
            Else
                Button1.Text = "STOP"
                Panel1.BackColor = Color.Red
                Timer1.Start()
            End If
            clicked = Not clicked
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Static pass As Boolean = True
            Static b As Boolean = True
            Static c As Boolean = True
            Static counter As Integer = 0
            If pass Then
                mp = Cursor.Position
                pass = False
            End If
    
            Cursor.Position = mp
            b = mouse_event(&H2, 0, 0, 0, 1)
            If c Then
                Panel1.BackColor = Color.Blue
            Else
                Panel1.BackColor = Color.Red
            End If
            c = Not c
            counter += 1
            Label1.Text = counter.ToString
        End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, March 14, 2017 5:39 PM
  •  That Exception is because you are using the wrong types in your mouse_event api function signature.  It looks like you found it from some old VB6 code which used Long types for most everything.  In VB.Net, the data types need to be changed to best match the unmanaged data types.

     Here is the correct VB.Net signature for the mouse_event api function and the constants for the Left, Middle, and Right mouse button events to use with it.  You will need to add the Import statement below at the top of your code for this signature too.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
        Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
        Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
        Private Const MOUSEEVENTF_RIGHTUP As Integer = &H10
        Private Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
        Private Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
    
        <DllImport("user32.dll", EntryPoint:="mouse_event")>
        Private Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As UInteger)
        End Sub
    
        'Your code....
    
    End Class


    If you say it can`t be done then i`ll try it

    Tuesday, March 14, 2017 6:28 PM
  • ok it works except i cant start and stop it using f4 and f3
    Tuesday, March 14, 2017 6:30 PM
  • ok it works except i cant start and stop it using f4 and f3

    show the code you wrote to handle those two key presses, how could anyone possibly know why without the actual code?

    Wednesday, March 15, 2017 3:34 PM