使用 PInvoke 时,包含 bool 与 uint 的结构有什么区别?
好吧,我现在很困惑。 在我的最后一个问题之后,有几个人评论关于将 bool 更改为 uint 我验证了它们的大小相同:
Console.WriteLine("sizeof bool = {0}", Marshal.SizeOf(typeof(bool)));
Console.WriteLine("sizeof uint = {0}", Marshal.SizeOf(typeof(uint)));
当然打印:
sizeof bool = 4
sizeof uint = 4
那说,然后我崩溃了,无论如何都尝试了他们的建议......将结构内的单个 bool 更改为 uint。我一生都无法弄清楚为什么这使它起作用......
所以这起作用了:
[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;
}
但是在这个结构中它打破了:
[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;
}
然而当我在 中将 bool bKeyDown 更改为 uint 时>KEY_EVENT_RECORD
结构它再次开始工作...
有人可以解释一下这种行为吗?
我真的很想知道它的原因,这样我就可以避免这个未记录的功能(又名错误)将来。
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
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...
So this works:
[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;
}
When used in this structure:
[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;
}
Yet when I change bool bKeyDown to uint in the KEY_EVENT_RECORD
structure it starts working again...
Can someone please explain this behavior?
I really would like to know the why of it so that I can avoid this undocumented feature (aka bug) in the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将字段类型设置为 bool 并添加属性 [MarshalAs(UnmanagedType.Bool)]。
MarshalAsAttribute 的文档
UnmanagedType 的文档
Try setting the field type to bool and add the attribute [MarshalAs(UnmanagedType.Bool)].
Docs for MarshalAsAttribute
Docs for UnmanagedType
bool 是 1byte --> sizeof(C# 参考)
另请参阅 布尔类型的默认封送处理
bools are 1byte --> sizeof (C# Reference)
Also, see Default Marshaling for Boolean Types
使用新示例重新表述问题:
布尔值使用 LayoutKind.Explicit 编组,这是否按设计损坏或失败?
Question reworded with a new sample:
Boolean Marshalling with LayoutKind.Explicit, Is this broken or failing as designed?