telerik - C#: The unmanaged method SendInput does not work with different representation of the structure -


this problem come me telerik testing framework.
telerik testing framework try simulate mouse click using method sendinput interact application, mouse click not occur.

under hood telerik code looks like

       [structlayout(layoutkind.explicit)]     public struct input     {         [fieldoffset(0)]         public int type;         [fieldoffset(4)]         public mouseinput mi;     }      [structlayout(layoutkind.sequential)]     public struct mouseinput     {         public int dx;         public int dy;         public uint mousedata;         public uint dwflags;         public uint time;         public intptr dwextrainfo;     }      public static void mouseclick()     {         var leftdown = new input();         leftdown.type = 0;         leftdown.mi.dwflags = 2;         sendinput(1, ref leftdown, marshal.sizeof(leftdown));           var leftup = new input();         leftup.type = 0;         leftup.mi.dwflags = 4;         sendinput(1, ref leftup, marshal.sizeof(leftup));     }     [dllimport("user32.dll", setlasterror = true)]     static extern uint sendinput(uint ninputs, ref input pinputs, int cbsize); 

but if change input structure layoutkind.explicit layoutkind.sequential works fine, example:

[structlayout(layoutkind.sequential)] public struct input {     public int type;     public mouseinput mi; } 

in opinion both variant must work, because in memory represented same.

any idea why input layoutkind.explicit not work?

on 64 bit system, mouseinput has alignment of 8 because dwextrainfo has alignment of 8. means correct offset on 64 bit system 8 rather 4. if switch sequential layout c# compiler able place mouseinput struct @ correct location.

that said, don't care version either. input struct size field, followed union of mouseinput, keybdinput , hardwareinput. including mouseinput relies on mouseinput having same alignment other 2 members of union, , having alignment greater or equal of 2 omitted structs.

i suggest use declaration of input found here: https://stackoverflow.com/a/6833311/505088


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -