c# - "A call to a PInvoke function has unbalanced the stack" -


i created form application in visual c#, uses function generate mouse click, got following error message:

a call pinvoke function '...form1::mouse_event' has unbalanced stack. because managed pinvoke signature not match unmanaged target signature. check calling convention , parameters of pinvoke signature match  target unmanaged signature. 

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;  ...  void generatemouseclick(int x, int y) {     cursor.position = new point((int)x, (int)y);     mouse_event(mouseeventf_leftdown | mouseeventf_leftup, cursor.position.x, cursor.position.y, 0, 0); } 

your win32 api declaration incorrect: 'long' maps int64 in .net framework, incorrect windows api calls.

replacing long int should work:

public static extern void mouse_event(int dwflags, int dx, int dy, int cbuttons, int dwextrainfo);

for future reference, may want check pinvoke.net whenever you're looking correct way invoke api functions -- although it's not perfect, have shown correct declaration mouse_event.

(edit, 26 march 2012): , although declaration provided indeed works, replacing long uint better, win32's dword 32-bit unsigned integer. in case, you'll away using signed integer instead (as neither flags nor other arguments ever large enough cause sign overflow), not case. pinvoke.net declaration correct, following:

public static extern void mouse_event(uint dwflags, uint dx, uint dy, uint cbuttons, uint dwextrainfo); 

another answer question provided correct declaration, , uint issue pointed out in comments. edited own answer make more obvious; other participants should feel free edit incorrect posts well, btw.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -