C# C中的结构大小无效#

C# C中的结构大小无效#,c#,C#,我有以下C#struct: 我有一个Windows Server 2008 R2 64位平台,uint的大小是4字节,IntPtr的大小是8字节 那么,为什么我调用Marshal.SizeOf(typeof(MY_STRUCT))时得到的是24而不是16???请提供任何帮助。您需要设置StructLayout属性的属性 我假设默认值依赖于平台,在x64上等于8。要获取16个字节,请执行以下操作: [StructLayout(LayoutKind.Sequential, Pack=4)] inte

我有以下C#struct:

我有一个Windows Server 2008 R2 64位平台,
uint
的大小是4字节,
IntPtr
的大小是8字节

那么,为什么我调用
Marshal.SizeOf(typeof(MY_STRUCT))
时得到的是24而不是16???请提供任何帮助。

您需要设置StructLayout属性的属性

我假设默认值依赖于平台,在x64上等于8。要获取16个字节,请执行以下操作:

[StructLayout(LayoutKind.Sequential, Pack=4)]
internal struct MY_STRUCT
{
     public uint A;
     public IntPtr B;
     public uint C;
}
看见
[StructLayout(LayoutKind.Sequential, Pack=4)]
internal struct MY_STRUCT
{
     public uint A;
     public IntPtr B;
     public uint C;
}