【问题标题】:Passing 2D array from C# to C++将二维数组从 C# 传递到 C++
【发布时间】:2013-08-02 13:47:02
【问题描述】:

我正在尝试将浮点值的二维数组传递给我在 Unity 中的 C++ 插件。

在 C++ 方面我有:

 void process_values(float** tab);

在 C# 方面,我有一个 float[,],但我不知道如何将它传递给我的 c++ 插件。

我该怎么做?

【问题讨论】:

  • 必须是二维数组吗?或者你可以将它作为一个数组内联(即每一行都跟在内存中的前一行之后)?传递单个数组会容易得多。
  • 如果不能修改c++插件...这是别人已经做过的事情
  • 如何知道二维数组的列大小?
  • 我在它之前调用一个函数来初始化并给出数组的大小

标签: c# c++ plugins unity3d


【解决方案1】:

要将数据从 CLR 复制到本机代码,请使用 Marshall 类。

具体来说

public static void Copy(
    float[] source,
    int startIndex,
    IntPtr destination,
    int length
)

在二维情况下,您必须自己计算后续行的地址。 对于每一行,只需将行的浮动时间长度添加到目标指针大小。

public void process(float[][] input)
{
    unsafe
    {
        // If I know how many sub-arrays I have I can just fix them like this... but I need to handle n-many arrays
        fixed (float* inp0 = input[0], inp1 = input[1] )
        {
            // Create the pointer array and put the pointers to input[0] and input[1] into it
            float*[] inputArray = new float*[2];
            inputArray[0] = inp0;
            inputArray[1] = inp1;
            fixed(float** inputPtr = inputArray)
            {
                // C function signature is someFuction(float** input, int numberOfChannels, int length)
                functionDelegate(inputPtr, 2, input[0].length);
            }
        }
    }
}

示例c#:

[DllImport("Win32Project1.dll", EntryPoint = "?Save@@YAXPAPAM@Z", CallingConvention = CallingConvention.Cdecl)]
        static extern void Save(IntPtr arr);
static void Main(string[] args)
        {

            float[][] testA = new float[][] { new float[] { 1.0f, 2.0f }, new float[] { 3.0f, 4.0f } };

                IntPtr initArray = Marshal.AllocHGlobal(8);
                IntPtr arrayAlloc = Marshal.AllocHGlobal(sizeof(float)*4);

                Marshal.WriteInt32(initArray, arrayAlloc.ToInt32());
                Marshal.WriteInt32(initArray+4, arrayAlloc.ToInt32() + 2 * sizeof(float));
                Marshal.Copy(testA[0], 0, arrayAlloc, 2);
                Marshal.Copy(testA[1], 0, arrayAlloc + 2*sizeof(float), 2);

                Save(initArray); // C func call

                Marshal.FreeHGlobal(arrayAlloc);
                Marshal.FreeHGlobal(initArray);

                Console.ReadLine();

        }

【讨论】:

  • 那么我最终得到了什么变量之王,IntPtr 数组?
  • intptr 只是您要将数据复制到的内存地址。在我的选择中,您必须为 c++ 中的浮点数组分配内存。然后你就可以用 Marshal 复制数据了。
  • 您也可以使用不安全的代码。 fixed(float** inputPtr = inputArray) { // C 函数n(float**) function(inputPtr); }
  • @stepandohnal:我也对此感兴趣。您能否提供一个包含 c# 和 c++ 代码的工作示例?
  • 工作前可以从这里下载[链接] (leteckaposta.co/file/831197442.1/…)
猜你喜欢
  • 2013-02-19
  • 1970-01-01
  • 2018-07-28
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 2011-09-15
  • 2013-01-02
相关资源
最近更新 更多