帮助
在C ++中
#pragma pack(push,1)
typedef struct SIGMPGroup{
int temp;
char name[50]; //name of group
int port;
char addr[50]; //network address "229."
//int ttl; //
//int loop; // 0 - no loop back, 1 - loop back
SIGMPGroup():
temp(0),
port(0)
//ttl(-1),
//loop(0)
{}
};
#pragma pack(pop)
C#
public static SIGMPGroup DTIGMPGroup = new SIGMPGroup { temp = 56, name = "Example",port = 4000 };
[StructLayout(LayoutKind.Explicit, Pack = 1, CharSet = CharSet.Ansi, Size = 108)]
public struct SIGMPGroup{
[FieldOffset(0)]
public int temp;
[ FieldOffset(4), MarshalAs(UnmanagedType.ByValTStr,SizeConst=50)]
public string name; //name of group
[FieldOffset(54)]
public int port;
[FieldOffset(58), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string addr;
}
[DllImport(Eth_DLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void Leave(ref SIGMPGroup data);
ImportFunctionEth.Leave(ref ImportFunctionEth.DTIGMPGroup);
运行后我有错误: 无法从程序集“ConsoleApplication1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”加载类型“SIGMPGroup”,因为它包含偏移58处的对象字段,该字段未正确对齐或重叠字段不表示对象。
如果代码
[FieldOffset(58), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string addr;
删除,全部工作。为什么?
只能像这样工作
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct SIGMPGroup{
public int temp;
[ MarshalAs(UnmanagedType.ByValTStr,SizeConst=50)]
public string name; //name of group
public int port;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string addr;
}
答案 0 :(得分:1)
不太确定该错误是什么,CLR似乎想要保持结构blittable出于某种神秘的原因。它肯定不是,字符串破坏了。最终错误是由端口字段引起的,它是错误的,违反了.NET内存模型的承诺,即 int 等简单类型的更新是原子的。它的偏移量被推到56,并与第二个字符串重叠。
StructLayout.Explicit可能很古怪,你可以提供很多帮助。通过不帮助简单解决:
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct SIGMPGroup {
public int temp;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string name; //name of group
public int port;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string addr;
}
...
Debug.Assert(Marshal.SizeOf(typeof(SIGMPGroup) == 108); // fine