将C#锯齿状数组编组为C ++ [英] Marshalling C# Jagged Array to C++
问题描述
我正试图将2D C#锯齿状数组(double[][] jaggedArray
)编组到C ++ dll中,其中我已将接收变量指定为double**
.
I'm trying to marshal a 2D C# jagged array (double[][] jaggedArray
) to a C++ dll where i've specified the receiving variable to be a double**
.
但是,我收到的消息是:
However, i'm getting the message:
没有针对以下内容的封送处理支持 嵌套数组.
There is no marshaling support for nested arrays.
平坦化锯齿状数组的快捷方式是否可以在C ++ dll中使用C#中的锯齿状数组?
Short of flattening the jagged array is there a way to use jagged arrays from C# in a C++ dll?
推荐答案
使用低级的Marshal类方法,可以将任何类型的数据封送到非托管内存.例如,对于jaggedArray中的每个double []数组,请使用Marshal.AllocHGlobal分配非托管内存块,并使用Marshal.Copy方法(Double [],Int32,IntPtr,Int32)方法向其复制数组成员. AllocHGlobal返回IntPtr类型,在这种情况下,它可以作为Poiner(double *)传递给C ++方法.
Using low level Marshal class methods, it is possible to marshal any type to unmanaged memory. For example, for every double[] array in jaggedArray, allocate unmanaged memory block with Marshal.AllocHGlobal, and copy array members to it using Marshal.Copy Method (Double[], Int32, IntPtr, Int32) method. AllocHGlobal returns IntPtr type, which can be passed to C++ method as poiner, double* in this case.
这篇关于将C#锯齿状数组编组为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!