相关疑难解决方法(0)

将托管代码中的多维数组传递给非托管代码

我想做以下事情:

  1. 在c#代码中创建三个像素数组,如下所示:

    var myArray = new short[x,y,z];
    UnanagedFunction(myArray);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将它传递给非托管代码(c ++),如下所示:

    void UnmanagedFunction(short*** myArray)
    {
        short first = myArray[0][0][0];
    }
    
    Run Code Online (Sandbox Code Playgroud)

更新 当我尝试以下代码时,我遇到运行时错误:

尝试读取或写入受保护的内存.

谢谢!!!

c# c++ marshalling dllimport

8
推荐指数
1
解决办法
3842
查看次数

将 double[,] 转换为 IntPtr C#

我需要将 c# 中的双数组转换为 IntPtr 以将其正确发送到我的 c DLL。我已经成功地使用这个答案中的方法从IntPtr 转换为 double[,] 。我还找到了另一个接近我需要但处理一维数组的答案

我在我的逻辑中遗漏了一些东西并在崩溃后得到错误代码:Managed ' has exited with code -1073740940 (0xc0000374)。

这是我到目前为止

            IntPtr p = Marshal.AllocHGlobal(rows * columns);
            for (int i = 0; i < rows; i++) //rows
            {
                double[] temp = new double[columns];

                for (int x = 0; x < columns; x++)
                    temp[x] = input.cells[i, x];

                Marshal.Copy(temp, 0, p, columns);

                p = (IntPtr)(p.ToInt64() + IntPtr.Size);
            }

            toReturn.cells = p;
            Marshal.FreeHGlobal(p);
            return toReturn;
Run Code Online (Sandbox Code Playgroud)

toReturn.cells 是我返回的结构内的 IntPtr。单元格的结构为单元格[行,列] …

c# double intptr

3
推荐指数
1
解决办法
87
查看次数

标签 统计

c# ×2

c++ ×1

dllimport ×1

double ×1

intptr ×1

marshalling ×1