Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   PInvokeStackImbalance was detected. (http://www.programmingforums.org/showthread.php?t=10926)

MisterPoppy Aug 2nd, 2006 2:37 PM

PInvokeStackImbalance was detected.
 
Hello everyone. I've got a problem. I'm trying to simulate mouse clicks but i get this "PInvokeStackImbalance was detected" exception. I've searched google but didn't find any answer.
Here's the 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;     

       



        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr firefoxHandle = FindWindow("MozillaWindowClass", "****** - Mozilla Firefox");
         
            if (firefoxHandle == IntPtr.Zero)
            {
                MessageBox.Show("Could not found the window!");
                return;
            }

            SetForegroundWindow(firefoxHandle);
            /*
            int X = 710;
            int Y = 445;*/
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y , 0, 0);               
     
        }


It's meant to click, for example, a button on a webpage in firefox.
¨
Thanks alot for the help, in advance.

Dameon Aug 2nd, 2006 3:08 PM

You're parameter list for mouse_event is incorrect.

It's a common enough mistake in this case. Often times one will translate an API signature from an example given in C++ or VB. In VB, a Long is 4 bytes. In C++, the parameters are listed as DWORD (a typedef for unsigned long) which is 4 bytes. In C#, a long is 8 bytes. In short, you are passing twice as much data to the function as it expects. Note also that Windows API functions are invoked with stdcall, meaning that the called function cleans up the stack when returning. The exception you are getting is a handy warning from Pinvoke that the stack pointer isn't in the same position as before you called the function. Because your code is advancing it twice as much as the called function is unwinding it.

The type you are looking for to match a DWORD is Int32 or just int. No need to bother with .Net's terrible support for unsigned values in this case since the range you will be using with this particular function is narrow.

MisterPoppy Aug 2nd, 2006 4:27 PM

It worked. I replaced
:

public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
with:
:

public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 cButtons, Int32 dwExtraInfo);

Thanks alot Dameon :) !

kurifu Aug 5th, 2006 5:20 PM

You should check our the website www.pinvoke.net great resource for looking up signatures.


All times are GMT -5. The time now is 12:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC