如何正确地将缓冲区数组指针从C#传递到非托管dll

问题描述

我在一个非托管库中有一个函数,如下所示:

void fill_arr(int size,struct mystruct **buffer)
{
  /* code that allocates memory for size number of structs and fills the pointer buffer */
}

在C#中,我尝试执行以下操作:

[DllImport("mylib.dll",CallingConvention = CallingConvention.Winapi,EntryPoint = "fill_arr",CharSet = CharSet.Ansi)]
static extern void fill_arr(int count,[MarshalAs(UnmanagedType.LPArray,SizeParamIndex = 0)] ref c_struct[] result);

public static MyObject[] InitObjects(int count)
{
  c_struct[] c_buffer = new c_struct[count];
  MyManagedobject[] result = new MyManagedobject[count];

  fill_arr(count,ref c_buffer);
  for (int i = 0; i < count; i++)
    result[i] = new MyManagedobject(ref c_buffer [i]);

  return result;
}

MyManagedobject是c_struct的包装,它将c_struct分配给私有变量,并具有几种处理c_struct数据的方法

但是,当我尝试运行此方法时,在System.StubHelpers.MngdNativeArrayMarshaler.ConvertSpacetoManaged(IntPtr,System.Object ByRef,IntPtr,Int32)处收到CLR错误0x80131506。 我在做什么错?填充结构缓冲区的正确方法是什么?

解决方法

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

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

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