无符号long和&;之间有什么区别;unsigned int以及这两种类型应该如何在c#中封送?

无符号long和&;之间有什么区别;unsigned int以及这两种类型应该如何在c#中封送?,c#,c++,c,C#,C++,C,所以,我在将一些结构从C/C++“翻译”成C#时,遇到了一些我不太理解的东西 这是第一个结构: typedef struct _MOUSE_EVENT_RECORD { COORD dwMousePosition; DWORD dwButtonState; DWORD dwControlKeyState; DWORD dwEventFlags; } 我写的是: [StructLayout(LayoutKind.Sequential)] public struct MouseEventRecor

所以,我在将一些结构从C/C++“翻译”成C#时,遇到了一些我不太理解的东西

这是第一个结构:

typedef struct _MOUSE_EVENT_RECORD {
COORD dwMousePosition;
DWORD dwButtonState;
DWORD dwControlKeyState;
DWORD dwEventFlags;
}
我写的是:

[StructLayout(LayoutKind.Sequential)]
public struct MouseEventRecord
{
    [MarshalAs(UnmanagedType.LPStruct)]
    public Coord dwMousePosition; //struct with (short X) & (short Y)
    [MarshalAs(UnmanagedType.U4)]
    public ButtonState dwButtonState; //A uint enum
    [MarshalAs(UnmanagedType.U4)]
    public ControlKeyState dwControlKeyState; //A uint enum
    [MarshalAs(UnmanagedType.U4)]
    public MouseEventFlags dwEventFlags; //A uint enum
}
    [StructLayout(LayoutKind.Sequential)]
    public struct ConsoleReadConsoleControl
    {
        [MarshalAs(UnmanagedType.U4)]
        public uint nLength;
        [MarshalAs(UnmanagedType.U4)]
        public uint nInitialChars;
        [MarshalAs(UnmanagedType.U4)]
        public uint dwCtrlWakeupMask;
        [MarshalAs(UnmanagedType.U4)]
        public ControlKeyState dwControlKeyState; //A uint enum
    }
然后是这个结构:

typedef struct _CONSOLE_READCONSOLE_CONTROL {
    ULONG nLength;
    ULONG nInitialChars;
    ULONG dwCtrlWakeupMask;
    ULONG dwControlKeyState;
}
我写的是:

[StructLayout(LayoutKind.Sequential)]
public struct MouseEventRecord
{
    [MarshalAs(UnmanagedType.LPStruct)]
    public Coord dwMousePosition; //struct with (short X) & (short Y)
    [MarshalAs(UnmanagedType.U4)]
    public ButtonState dwButtonState; //A uint enum
    [MarshalAs(UnmanagedType.U4)]
    public ControlKeyState dwControlKeyState; //A uint enum
    [MarshalAs(UnmanagedType.U4)]
    public MouseEventFlags dwEventFlags; //A uint enum
}
    [StructLayout(LayoutKind.Sequential)]
    public struct ConsoleReadConsoleControl
    {
        [MarshalAs(UnmanagedType.U4)]
        public uint nLength;
        [MarshalAs(UnmanagedType.U4)]
        public uint nInitialChars;
        [MarshalAs(UnmanagedType.U4)]
        public uint dwCtrlWakeupMask;
        [MarshalAs(UnmanagedType.U4)]
        public ControlKeyState dwControlKeyState; //A uint enum
    }
现在,我不确定这是否正确,我之所以将
ULONG
写为
public uint
,是因为
ULONG
(intern
unsigned long
)的大小是4字节(did sizeof(unsigned long)和sizeof(unsigned int),两者都返回4),但是
DWORD(unsigned int)之间的区别是什么
&
ULONG(无符号长)
如果它们都是4字节大小?“翻译”的结构写得正确吗

我确实在GNU文档中读到,
无符号长
取决于系统架构,因此,
无符号长
可以是64位,与
无符号长整型
相同。我不太明白这一点,因为我的电脑运行的是64位Windows,所以
sizeof(unsigned long)
不应该返回8而不是4吗?

这让人困惑

首先,在Windows上,
unsigned long
unsigned int
之间没有区别(除了它们是不同的类型,因此可以在它们上重载函数)。你的编组代码很好


在大多数其他平台上,
int
是32位,
long
是64位,
long
在32位程序中是32位,在64位程序中是64位。(在这种情况下,IntPtr将是一个合适的编组声明-它正确地切换大小)。

如果您在64位系统上,sizeof(unsigned long)确实应该返回8。您可能为32位系统目标编译了它。@BenjaminBarrois-如果他在Windows box上,
long
为32位,则不会编译它-即使对于64位程序也是如此。Windows在64位系统上仍然使用32位长*Nix在64位系统上使用64位长的。如果你想要C++中已知的宽度类型,请使用 Itnnnt类型,其中代码> NN<代码>是位宽度。@马丁伯纳好,我不知道!我习惯于只在Unix系统上编码。@MartinBonner-Windows-Box?我第一次听说它时,我通过媒体创建工具(在微软的网站上找到)获得了windows,在16位时代,
int
是16位,
long
是32位。C和C++标准从未定义整数类型的精确位大小,它们总是特定于实现。该标准仅保证当封送采用指针大小的整数(例如Unix上的长整数,可能是32位或64位)的方法时,
sizeof(char),您可以/应该在C#中使用IntPtr。这允许您在支持32位和64位环境的同时将程序集保留在任何CPU上。@Zastal:Ooh!说得好。我会修改(即使它在128位平台上可能有问题——我不希望在50年内看到任何问题)。