Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the difference between structures containing bool vs uint when using PInvoke?
    primarykey
    data
    text
    <p>Ok, I'm now very confused. <a href="https://stackoverflow.com/questions/1602487">After my last question</a> had several people comment about changing bool to uint I verified they are the same size by:</p> <pre><code> Console.WriteLine("sizeof bool = {0}", Marshal.SizeOf(typeof(bool))); Console.WriteLine("sizeof uint = {0}", Marshal.SizeOf(typeof(uint))); </code></pre> <p>Which of course prints:</p> <pre><code>sizeof bool = 4 sizeof uint = 4 </code></pre> <p>That said, I then broke down and gave their suggestions a try anyway... Changing a single bool inside a structure to a uint. What I can't figure out for the life of me is why this made it work...</p> <p>So this works:</p> <pre><code>[StructLayout(LayoutKind.Sequential)] public struct KEY_EVENT_RECORD { public bool bKeyDown; public short wRepeatCount; public short wVirtualKeyCode; public short wVirtualScanCode; public char UnicodeChar; public int dwControlKeyState; } </code></pre> <p>When used in this structure:</p> <pre><code>[StructLayout(LayoutKind.Explicit)] public struct INPUT_RECORD { [FieldOffset(0)] public short EventType; [FieldOffset(4)] public KEY_EVENT_RECORD KeyEvent; } </code></pre> <p>But in this structure it breaks:</p> <pre><code>[StructLayout(LayoutKind.Explicit)] public struct INPUT_RECORD { [FieldOffset(0)] public short EventType; [FieldOffset(4)] public KEY_EVENT_RECORD KeyEvent; [FieldOffset(4)] public MOUSE_EVENT_RECORD MouseEvent; [FieldOffset(4)] public WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent; [FieldOffset(4)] public MENU_EVENT_RECORD MenuEvent; [FieldOffset(4)] public FOCUS_EVENT_RECORD FocusEvent; } </code></pre> <p>Yet when I change bool bKeyDown to uint in the <code>KEY_EVENT_RECORD</code> structure it starts working again... </p> <p><strong>Can someone please explain this behavior?</strong></p> <p>I really would like to know the why of it so that I can avoid this undocumented feature (aka bug) in the future.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload