C# -- PInvokeStackImbalance detected on a well documented function? - c#

Here is my code for a ClickMouse() function:
[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);
private const long MOUSEEVENTF_LEFTDOWN = 0x02;
private const long MOUSEEVENTF_LEFTUP = 0x04;
private const long MOUSEEVENTF_RIGHTDOWN = 0x08;
private const long MOUSEEVENTF_RIGHTUP = 0x10;
private void ClickMouse()
{
long X = Cursor.Position.X;
long Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
For some reason, when my program comes to this code, it throws this error message:
PInvokeStackImbalance was detected Message: A call to PInvoke function
'WindowsFormsApplication1!WindowsFormsApplication1.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.
Please help?

Looks like your DllImport declaration is wrong. In particular the use of Int64 (longs), instead of UInt32.
Here's some detail from the PInvoke reference:
http://www.pinvoke.net/default.aspx/user32.mouse_event
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy,
uint dwData,UIntPtr dwExtraInfo);

I found this declaration
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags,
uint dx,
uint dy,
uint dwData,
IntPtr dwExtraInfo);

I was using RealGetWindowClass using the following format.
[DllImport("User32", CallingConvention=CallingConvention.Cdecl)]
public static extern int RealGetWindowClass(IntPtr hwnd, StringBuilder pszType, int cchType);
and i was geting the 'PInvokeStackImbalance' exception/error. But when i changed the CallingConvention to
[DllImport("User32", CallingConvention=CallingConvention.StdCall)]
the error went away. I am using VS2010 on a 64 bit machine.

To fix PInvokeStackImbalance error:
Press: CTRL + ALT + E
Under "Managed Debugging Assistants" uncheck PInvokeStackImbalance

I changed "Cdecl" to "StdCall" but it didn't work.
As Scott Weinstein mentioned we should use int type.
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[Flags]
public enum MouseEventFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
public static void LeftClick(int x, int y)
{
Cursor.Position = new System.Drawing.Point(x, y);
mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}

Related

c# mouse_event does not fire on certain application

I am creating a console application that should simulate a click inside a certain application.
Im currently playing around with the external methods, for now i only want the program to click at the current mouse position every second:
public class Navigation
{
private const uint MOUSEEVENTF_MOVE = 0x0001;
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
private const uint MOUSEEVENTF_LEFTUP = 0x0004;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern bool SetCursorPos(uint x, uint y);
[DllImport("user32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo);
private string processName;
public Navigation(string processName)
{
this.processName = processName;
}
public void test()
{
while (true)
{
//SetForegroundWindow(Process.GetProcessesByName(processName)[0].MainWindowHandle);
Thread.Sleep(1000);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, 0, 0, 0, UIntPtr.Zero);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, 0, 0, 0, UIntPtr.Zero);
}
}
}
When starting the program and running the test() method the mouse indeed clicks every second, i can move the mouse around the screen and it will click at everything, notepad, spotify etc... EXECPT for the one application that i want to target ...
That is, when i hover the mouse above the particular application i want to click on, nothing happens. I read about putting the target process in the foreground, if i do that the application is put in the foreground but it still does not recieve any clicks.
For example if i hover the mouse above a button inside the application i want to target my program does not click it. When moving the mouse back outside it starts simulating clicks again. Any thoughts?
EDIT: I am following this tutorial

What makes this click through?

I'm trying to figure out how this code works but I just can not figure out what makes it click through.
Yes, this code is not mine since i'm trying to learn/understand it.
Assume I want the tranparancy but not the click through what needs to be changed and why?
I have been over the Windows styles pages over and over and still can not get my head around the click through part.
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class TransparentWindow : MonoBehaviour
{
[SerializeField]
private Material m_Material;
private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
[DllImport("user32.dll")]
static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern int SetWindowPos(IntPtr hwnd, int hwndInsertAfter, int x, int y, int cx, int cy, int uFlags);
[DllImport("Dwmapi.dll")]
private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
const int GWL_STYLE = -16;
const uint WS_POPUP = 0x80000000;
const uint WS_VISIBLE = 0x10000000;
const int HWND_TOPMOST = -1;
void Start()
{
#if !UNITY_EDITOR // You really don't want to enable this in the editor..
int fWidth = Screen.width;
int fHeight = Screen.height;
var margins = new MARGINS() { cxLeftWidth = -1 };
var hwnd = GetActiveWindow();
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
// Transparent windows with click through
SetWindowLong(hwnd, -20, 524288 | 32);//GWL_EXSTYLE=-20; WS_EX_LAYERED=524288=&h80000, WS_EX_TRANSPARENT=32=0x00000020L
SetLayeredWindowAttributes(hwnd, 0, 255, 2);// Transparency=51=20%, LWA_ALPHA=2
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, 32 | 64); //SWP_FRAMECHANGED = 0x0020 (32); //SWP_SHOWWINDOW = 0x0040 (64)
DwmExtendFrameIntoClientArea(hwnd, ref margins);
#endif
}
void OnRenderImage(RenderTexture from, RenderTexture to)
{
Graphics.Blit(from, to, m_Material);
}
}
This function:
SetWindowLong(hwnd, -20, 524288 | 32);
does the trick. Windows implements the rule that Mircosoft made which is that a window that is transparent to the user must be transparent to the mouse.
With the transparency bit set WS_EX_TRANSPARENT the window becomes transparent to mouse too and click passes to the painted layer behind the transparent window.
You need not understand but make use of this 'OS feature' which was probably implemented to cover for something else.
Read this article about the subject and this answer that explains the parameters

Simulate mouse down/ up programmatically

I'm trying to hold my mousedown and up using code for a remote desktop sort of deal. I managed to do single and double click using this:
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
uint ClickX = Convert.ToUInt32(Cursor.Position.X);
uint ClickY = Convert.ToUInt32(Cursor.Position.Y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, ClickX, ClickY, 0, 0);
and then double click is just 2 clicks. I found this MSDN on it but this looks like the only mousedown there is but this one is just to click and not hold I'm pretty sure.

Click through a form with transparent background in C#

I have been working on RAD Studio and on that this is performed automatically if the background is fully transparent, but it seems that it isn't as simple to do in Visual Studio. I have read articles and questions of functions GetWindowLong() and SetWindowLong(), but when I try to use them myself, I get error "the name xxx does not exist in the current context".
What I tried was:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int initialStyle = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
}
}
}
So is there some file I have to include in order get it to work, or do I have to place the function elsewhere? I'm also curious to know why does it behave so differently in the RAD Studio and Visula Studio.
The methods you are using are not found in the standard .NET libraries, you need to invoke them through user32.dll.
[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
private static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)]
private static extern IntPtr GetWindowLongPtr64(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong);
public static IntPtr GetWindowLong(HandleRef hWnd, int nIndex)
{
if (IntPtr.Size == 4) return GetWindowLong32(hWnd, nIndex);
else return GetWindowLongPtr64(hWnd, nIndex);
}
Calling them this way will now work correctly:
uint initialStyle = GetWindowLong(this.Handle, -20);
SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
Make sure you are using System.Runtime.InteropServices
EDIT: I found and modified some code from another question (I will add link if I can find it, I closed the tab and can't find it), and came up with this:
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
public enum GWL
{
ExStyle = -20,
HINSTANCE = -6,
ID = -12,
STYLE = -16,
USERDATA = -21,
WNDPROC = -4
}
public enum WS_EX
{
Transparent = 0x20,
Layered = 0x80000
}
public enum LWA
{
ColorKey = 0x1,
Alpha = 0x2
}
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hWnd, GWL nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern int SetWindowLong(IntPtr hWnd, GWL nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte alpha, LWA dwFlags);
private void Form1_Click(object sender, EventArgs e)
{
base.OnShown(e);
int originalStyle = GetWindowLong(this.Handle, GWL.ExStyle);
int wl = GetWindowLong(this.Handle, GWL.ExStyle);
wl = wl | 0x80000 | 0x20;
SetWindowLong(this.Handle, GWL.ExStyle, wl);
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
System.Threading.Thread.Sleep(50);
SetWindowLong(this.Handle, GWL.ExStyle, originalStyle);
TopMost = true;
}
I'm not an expert on how to do this, and I'm not sure if I like that solution, but it works.
Make sure you subscribe to the event with: this.Click += Form1_Click;
Assuming Form name as form1
Set these properties to following.
form1.BackColor = Control;
form1.TransparencyKey = Control;
now you can easily click through the transparent area of the form. hope this is what you were looking for.
Shujaat Siddiqui's answer works just fine for clicking through the transparency. It will not hide the form's title though nor its borders.
To do this you may either set the Opacity=0d or chose a FormBorderStyle=none along with a Text="" and ControlBox=false;
You may also think about cutting holes into the window's region to get precise control where click will go through and where not.
In all these cases you will also have to plan for control of the form's position and size..
But, as long as you don't go for semi-tranparency of the background all can be done..

I don't understand what this error means when working with virtual mouse clicks

I'm trying to be able to have a mouse click on current mouse position. While using code from this site.
I'm getting an error:
A call to PInvoke function 'Program!Program.Program::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.at line:
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);.
I don't understand the problem so what is the cause? How could I fix this?
My code:
[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);
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 void DoMouseClick()
{
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
private void tmrClickInterval_Tick(object sender, EventArgs e)
{
DoMouseClick();
}
Your DllImport signature is invalid.
The type of dwFlags, dx, dy, and dwData is documented as DWORD, which is a 32-bit unsigned integer. In C#, long represents a 64-bit signed integer. As such, you should use uint, which represents a 32-bit unsigned integer .
The last parameter dwExtraInfo is a of type ULONG_PTR (pointer to an unsigned 32-bit integer), which corresponds to UIntPtr in C#.
Try this:
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);

Categories

Resources