【问题标题】:Marshaling C++ struct with fixed size array into C#将具有固定大小数组的 C++ 结构编组到 C#
【发布时间】:2014-07-02 18:49:50
【问题描述】:

我有一个像这样声明的 C# 结构:

public struct AdvertisementData {
    public byte SomeId;
    [MarshalAs(UnmanagedType.LPArray , SizeConst = 12)]
    public byte[] AnArray;
  }

它是 C++ 对应物:

struct AdvertisementData {
  uint8_t SomeId;
  uint8_t AnArray[12];
};

当我尝试将上述结构的堆栈分配实例的引用从 C++ 发送到 C# 时,我得到:“字节 [] 类型的结构字段不能编组为 LPArray。” 知道我做错了什么吗?

【问题讨论】:

  • 你能用SafeArray试试吗?

标签: c# arrays mono marshalling fixed-width


【解决方案1】:

尝试将其编组为 ByValArray:

 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
  public byte[] AnArray;

另外,您确定不需要为您的结构设置任何 LayoutKind 吗?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多