需要从 C# 调用 C++ dll

问题描述

我有以下 C++ 代码

   DLLExport int _CDECL nncpu_evaluate_incremental(
  int player,int* pieces,int* squares,NNcpudata** nncpu)
{
  assert(nncpu[0] && uint64_t(&nncpu[0]->accumulator) % 64 == 0);

  Position pos;
  pos.nncpu[0] = nncpu[0];
  pos.nncpu[1] = nncpu[1];
  pos.nncpu[2] = nncpu[2];
  pos.player = player;
  pos.pieces = pieces;
  pos.squares = squares;
  return nncpu_evaluate_pos(&pos);
}

使用这个数据结构

typedef struct DirtyPiece {
  int dirtyNum;
  int pc[3];
  int from[3];
  int to[3];
} DirtyPiece;

typedef struct Accumulator {
#if defined(USE_FLOAT)
  alignas(64) float accumulation[2][256];
#else
  alignas(64) int16_t accumulation[2][256];
#endif
  int computedAccumulation;
} Accumulator;

typedef struct NNcpudata {
  Accumulator accumulator;
  DirtyPiece dirtyPiece;
} NNcpudata 

我在 C# 中定义了相同类型的结构层次结构,但是如果我调用代码,它总是在断言时失败。更具体地说,它在断言的第二部分失败 assert(nncpu[0] && uint64_t(&nncpu[0]->accumulator) % 64 == 0);这是检查内存是否正确对齐。

  [StructLayout(LayoutKind.Sequential,Pack =64)]
public  unsafe struct Accumulator
{
    public int[] accumulation;
    public int computedAccumulation;
    public Accumulator(int state)
    {
        accumulation = new int[512];
        computedAccumulation = state;
    }
}

有人可以指出我的解决方案,或者关于这个问题的一些好的阅读材料吗? 谢谢 JB

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)