![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0
![]() |
It worked. I replaced
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 cButtons, Int32 dwExtraInfo); Thanks alot Dameon ! |
|
|
|
|
|
#4 |
|
Expert Programmer
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |