将 C# 锯齿状数组编组到 C++ [英] Marshalling C# Jagged Array to C++

查看:25
本文介绍了将 C# 锯齿状数组编组到 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**.

但是,我收到消息:

没有编组支持嵌套数组.

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 Method (Double[], Int32, IntPtr, Int32) 方法将数组成员复制到其中.AllocHGlobal 返回 IntPtr 类型,可以作为指针传递给 C++ 方法,本例中为 double*.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆