Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 2nd, 2006, 2:37 PM   #1
MisterPoppy
Newbie
 
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0 MisterPoppy is on a distinguished road
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.
MisterPoppy is offline   Reply With Quote
Old Aug 2nd, 2006, 3:08 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270

Last edited by Dameon; Aug 2nd, 2006 at 3:19 PM.
Dameon is offline   Reply With Quote
Old Aug 2nd, 2006, 4:27 PM   #3
MisterPoppy
Newbie
 
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0 MisterPoppy is on a distinguished road
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 !
MisterPoppy is offline   Reply With Quote
Old Aug 5th, 2006, 5:20 PM   #4
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
You should check our the website www.pinvoke.net great resource for looking up signatures.
__________________
Clifford Matthew Roche <geek@cliffordroche.com>
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD is not detected in media player EverLearning Coder's Corner Lounge 2 Jan 30th, 2006 6:39 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:40 PM.

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