Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从C+;导出带有2个数组的结构+;变成C#_Arrays_String_Dllimport - Fatal编程技术网

Arrays 从C+;导出带有2个数组的结构+;变成C#

Arrays 从C+;导出带有2个数组的结构+;变成C#,arrays,string,dllimport,Arrays,String,Dllimport,帮助 在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 SIG

帮助

在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)
#

运行后出现错误: 无法从程序集“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;

   }

由于不太确定如何处理这个错误,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

看起来不允许对未对齐字符串进行标准编组。我会声明这个结构中唯一的byte[]成员,SizeConst=108,并手动进行所有转换,例如,使用位转换器类、字符串(sbyte*)构造函数等。如果用作示例[FieldOffset(4),marshallas(UnmanagedType.ByValArray,SizeConst=50)]public char[]name;我有一个错误:未能打包类型,因为嵌入数组实例的长度与布局中声明的长度不匹配。
[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