使用 PInvoke 时,包含 bool 与 uint 的结构有什么区别?

发布于 2024-08-09 01:46:40 字数 1615 浏览 4 评论 0原文

好吧,我现在很困惑。 在我的最后一个问题之后,有几个人评论关于将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南笙 2024-08-16 01:46:40

尝试将字段类型设置为 bool 并添加属性 [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;
}

MarshalAsAttribute 的文档
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文