c# - 编码不当 : C# array to a C++ unmanaged array

标签 c# c++ pinvoke marshalling

我有以下 C# 代码,其中包含结构定义 (CInput)、obj 定义和初始化,以及对 C++( native )DLL 函数的调用(这也是我编写的)。

//C# code

 public struct CInput
 {

  [MarshalAsAttribute(UnmanagedType.R8)] 
  public double Time;

  [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_R8)]
  public double[] Database;

  /* other similar fields*/

}     

CInput Inputs = new CInput();

/* init of Inputs fields*/

int bfr = Example(ref Inputs); //'Example' being the C++ DLL call

Messagebox.Show(bfr.ToString());

第二个参数的编码有错误,不知道在哪里。然后:

//C++ code

struct CInput {

  double Time;                       
  double Database[3650];     
  /*etc*/   
}

int Example(CInput& ObjIn) {

    return ObjIn.Database[0];        // just an example
}

如果我不小心并在数据库编码中仅指定“SafeArray”,我会收到“读/写内存错误,可能已损坏”等。

如果“数据库”被编码为 ByValArray 一切都很好,值会正确显示。不幸的是,我得到了一个内部大小异常,因为我有很多那个大小的数组,因此我必须去寻找指针——但是任何带有“SizeArray”的东西都会带来以下结果(刚刚发布的代码):

(来自 C++):

Database[0] = **0**

Database[1..etc] = values of the next parameters in the struct marshaled with ByValArray.

我想我应该提到我需要从 C# 到 C++ 的相同结构,我不是在寻找任何花哨的东西。所以结构中的数组 >>> 结构中的数组。

任何 对此的见解都将非常有值(value)。我已经找了几个小时了,但我还没有找到解决方案。

非常感谢。

最佳答案

据我了解你的问题,你不能将 ByValArraySizeConst 一起使用,因为你的真实结构有大量这样的数组,这会导致堆栈溢出。

您认为您可能需要在结构中使用指针,我同意您的看法。以下是操作方法。

在 C++ 方面,您应该将每个数组声明为指向元素类型的指针:

struct CInput {
  double *array;
}

您可能还希望在结构中包含数组的长度,以避免过多的硬编码常量。

所有艰苦的工作都发生在 C# 端。

public struct CInput
{
    public IntPtr array;
}
...
double[] theArray = new double[3650];
CInput input = new CInput();
input.array = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double))*theArray.Length);
try 
{
    Marshal.Copy(theArray, 0, input.array, theArray.Length);
    //call your C++ function here
}
finally
{
    Marshal.FreeHGlobal(input.array);
}

关于c# - 编码不当 : C# array to a C++ unmanaged array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7309838/

相关文章:

c++ - 在特定位置向字符串添加空格

c# - 使用 pinvoke 将结构数组中的嵌入式 C 结构数组传递给 C#

C# 编码 bool

c# - 如何强制泛型类使用带参数的 T 构造函数?

C# WaitCallBack - 线程池

c++ - 传递 constexpr 对象

c# - DllImport - ANSI 与 Unicode

c# - EntityFramework、DbContextScope 和 Effort - 异常 : DbContext has been disposed inside unit test

c# - 我可以在普通虚拟机上部署我的 Azure 功能吗?

c++ - 重载 operator[] 的使用不明确