使用的PInvoke当包含布尔VS UINT结构之间的区别是什么?
好了,现在我很迷茫。 后,我的最后一个问题有几个人评论有关更改布尔为uint我证实它们是相同大小:
Ok, I'm now very confused. After my last question had several people comment about changing bool to uint I verified they are the same size by:
Console.WriteLine("sizeof bool = {0}", Marshal.SizeOf(typeof(bool)));
Console.WriteLine("sizeof uint = {0}", Marshal.SizeOf(typeof(uint)));
这当然打印的:
Which of course prints:
sizeof bool = 4
sizeof uint = 4
这是说,我再崩溃了,给他们的建议一试反正...更改结构内的单个布尔为uint。我想不通,我的生活就是为什么,这使得它的工作...
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...
所以这个作品:
[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;
}
当在此结构中使用的:
[StructLayout(LayoutKind.Explicit)]
public struct INPUT_RECORD
{
[FieldOffset(0)] public short EventType;
[FieldOffset(4)] public KEY_EVENT_RECORD KeyEvent;
}
但是,在这种结构中它打破了:
But in this structure it breaks:
[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;
}
然而,当我改变布尔bKeyDown在 KEY_EVENT_RECORD
结构,它开始再次合作为uint ...
Yet when I change bool bKeyDown to uint in the KEY_EVENT_RECORD
structure it starts working again...
有人可以解释这种现象?
我真的想知道为什么会这样,我能避免这种无证功能(又名错误)的未来。
I really would like to know the why of it so that I can avoid this undocumented feature (aka bug) in the future.
问改写了新的样本:
http://*.com/questions/1703759/boolean-marshalling-with-layoutkind-explicit-is-this-broken-or-failing-as-design