将2D数组从C#传递给C ++

时间:2013-08-02 13:47:03

标签: c# c++ plugins unity3d

我正在尝试将一个浮点值的2D数组传递给Unity中的C ++插件。

在C ++方面,我有:

 void process_values(float** tab);

在C#端,我有一个浮动[,],我不知道如何将它传递给我的c ++插件。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

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

特别是

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

在2D情况下,您必须自己计算后续行的地址。 对于每一行,只需添加浮点的目标指针大小乘以行的长度。

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();

        }