[StructLayout(LayoutKind.Explicit)]
struct InputUnion {
[FieldOffset(0)] public MOUSEINPUT mi;
[FieldOffset(0)] public KEYBDINPUT ki;
[FieldOffset(0)] public HARDWAREINPUT hi;
}
struct INPUT
{
public int type;
public InputUnion u;
}
<StructLayout(LayoutKind.Explicit)> _
Structure InputUnion
<FieldOffset(0)> Public mi As MOUSEINPUT
<FieldOffset(0)> Public ki As KEYBDINPUT
<FieldOffset(0)> Public hi As HARDWAREINPUT
End Structure
Structure INPUT
Public type As Integer
Public u As InputUnion
End Structure
MOUSEINPUT, KEYBDINPUT, HARDWAREINPUT
The last 3 fields are a union, which is why they are all at the same memory offset.
On 64-Bit systems, the offset of the mi, ki and hi fields is 8, because the nested struct uses the alignment of its biggest member, which is 8 (due to the 64-bit pointer in dwExtraInfo). By separating the union into its own structure, rather than placing the mi, ki and hi fields directly in the INPUT structure, we assure that the .Net structure will have the correct alignment on both 32 and 64 bit.