0

I want to make an autoclicker that will click when i press "F" button; But each time I try to press "F" I get invoke error.

I tried to run Click thread in another void but it didn't work.

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

UserActivityHook actHook;

static bool autoclickerToggle = false;

private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

public Form1()
{
    InitializeComponent();
    actHook = new UserActivityHook(); // crate an instance with global hooks
                                      // hang on events
    actHook.OnMouseActivity += new MouseEventHandler(MouseMoved);
    actHook.KeyDown += new KeyEventHandler(MyKeyDown);
    actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
    actHook.KeyUp += new KeyEventHandler(MyKeyUp);      
}

public void MouseMoved(object sender, MouseEventArgs e) { }

public void MyKeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.F)
    {
        AutoclickerToggle();                      
    }
}

public void MyKeyPress(object sender, KeyPressEventArgs e) { }

void AutoclickerToggle()
{
    if (autoclickerToggle)
    {
        autoclickerToggle = false;
    }
    else
    {
        autoclickerToggle = true;
        Thread Click = new Thread(() => Clicker());
        Click.Start();
    }
}  

public void MyKeyUp(object sender, KeyEventArgs e) { }

public void Clicker()
{
    while (autoclickerToggle)
    {
        int X = Cursor.Position.X;
        int Y = Cursor.Position.Y;
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
        Thread.Sleep(100);
    }
}

As I said I've got this error when pressing "F" (used translator there, because I had this error in polish language) "Calling PInvoke 'Test! Test.Form1 :: mouse_event 'has upset the balance of the stack. The likely cause is a mismatch between the managed PInvoke signature and the unmanaged target signature. Verify that the called convention and signature parameters of the PInvoke function match the unmanaged destination signature"

itsMasoud
  • 1,335
  • 2
  • 9
  • 25
  • Possibly that you are trying to pass `int` as `long`. Might want to use this as `mouse_event` : `[DllImport("user32.dll")] static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, UIntPtr dwExtraInfo);` – Ryan Wilson Nov 04 '19 at 20:28
  • @RyanWilson still doesn't work – FreshyCfelu Nov 04 '19 at 20:35
  • Did you change your `const` values to `uint` to match the signature? – Ryan Wilson Nov 04 '19 at 20:36
  • Does this answer your question? [Directing mouse events \[DllImport("user32.dll")\] click, double click](https://stackoverflow.com/questions/8739523/directing-mouse-events-dllimportuser32-dll-click-double-click) – Ryan Wilson Nov 04 '19 at 20:38

0 Answers0