What is the difference between structures containing bool vs uint when using PInvoke?

Try setting the field type to bool and add the attribute MarshalAs(UnmanagedType. Bool) StructLayout(LayoutKind. Sequential) public struct KEY_EVENT_RECORD { MarshalAs(UnmanagedType.

Bool) public bool bKeyDown; public short wRepeatCount; public short wVirtualKeyCode; public short wVirtualScanCode; public char UnicodeChar; public int dwControlKeyState; } Docs for MarshalAsAttribute Docs for UnmanagedType.

Try setting the field type to bool and add the attribute MarshalAs(UnmanagedType. Bool). StructLayout(LayoutKind.

Sequential) public struct KEY_EVENT_RECORD { MarshalAs(UnmanagedType. Bool) public bool bKeyDown; public short wRepeatCount; public short wVirtualKeyCode; public short wVirtualScanCode; public char UnicodeChar; public int dwControlKeyState; } Docs for MarshalAsAttribute Docs for UnmanagedType.

I would hope that UnmanagedType. Bool would be the default marshal type for bool... but I've been wrong before. I tried it anyway and it still fails.

I also tried UnmanagedType. I4 and UnmanagedType. U4 and both of them refuse to allow this decoration on a bool type.

– csharptest. Net Oct 21 '09 at 19:57.

Bools are 1byte --> sizeof (C# Reference) Also, see Default Marshaling for Boolean Types.

The MarshalAs attribute does not apply to what you're trying to do. For P/Invoke you use the MarshalAs attribute inline. E.x.

: DllImport("Kernel32. Dll", CharSet = CharSet. Unicode, SetLastError = true) public static extern IntPtr GetStdHandle(MarshalAs(UnmanagedType.

U4) int nStdHandle); – hjb417 Oct 21 '09 at 21:10 Yes I know a bool is represented in C# as a single byte; however, it is always marshalled by default as a 4-byte int. Thus the question, why does changing bool to uint make a difference? – URL1 Oct 22 '09 at 0:58 I think this is platform dependent.

E.x. : It will Marshal. SizeOf will return 4 on a desktop and 1 on a pocket pc.

Look at Rudedog2's post @ social.msdn.microsoft. Com/Forums/en-US/csharpgeneral/thread/… – hjb417 Oct 22 '09 at 1:58.

Question reworded with a new sample: stackoverflow.com/questions/1703759/bool....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions