Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity c intptr到字节数组,无大小信息 我的问题是当你从C++知道UCHAR *的行和COLS信息时,我如何转换它的字节数组。我必须为Marshal.Copy提供正确的参数_C#_C++_Unity3d_Dll_Intptr - Fatal编程技术网

C# Unity c intptr到字节数组,无大小信息 我的问题是当你从C++知道UCHAR *的行和COLS信息时,我如何转换它的字节数组。我必须为Marshal.Copy提供正确的参数

C# Unity c intptr到字节数组,无大小信息 我的问题是当你从C++知道UCHAR *的行和COLS信息时,我如何转换它的字节数组。我必须为Marshal.Copy提供正确的参数,c#,c++,unity3d,dll,intptr,C#,C++,Unity3d,Dll,Intptr,当我没有大小信息时,如何将intptr转换为字节数组?我已经用C++返回了行和COLS信息,但我没能做到。这是代码。当我实现它时,我想unity会崩溃 int rows = 0; int cols = 0; Marshal.Copy(imageData, 0, pnt, imageData.Length); IntPtr returnPtr = cppExternalM

当我没有大小信息时,如何将intptr转换为字节数组?我已经用C++返回了行和COLS信息,但我没能做到。这是代码。当我实现它时,我想unity会崩溃

                int rows = 0;
                int cols = 0;
                Marshal.Copy(imageData, 0, pnt, imageData.Length);
                IntPtr returnPtr = cppExternalMethod(pnt, ref rows, ref cols);
                int size = rows * cols * 3; //i'm not sure about that
                byte[] result = new byte[size];
                Marshal.Copy(returnPtr, result, 0, size);
                Marshal.FreeHGlobal(returnPtr);
                Marshal.FreeHGlobal(pnt);
c++部分:

uchar* cppExternalMethod(uchar* frameData, int* rows, int* cols)
{
     Mat img(HEIGHT, WIDTH, CV_8UC3);

    img.data = (uchar*)(frameData);
    Mat result = doWork(img);

    uchar* resultArray = result.data;

    Size s = result.size();
    int rows_ = s.height;
    int cols_ = s.width;
    *rows = rows_;
    *cols = cols_;
    return resultArray;
}

编辑:实际大小=行*cols*3应该可以,我尝试另一个项目,我现在的大小和这个等式保持不变。

如果你的图像像RGB一样是3通道,那么你的大小是不正确的。此外,cppExternalMethod应该使用“out”参数而不是“ref”。

您有行和列……这是多维数组吗?你能显示你试图从C++返回的变量吗?我只需要看看它是如何定义的我不确定=>如果一个元素是一个字节,那么它是正确的,否则它太小了。我想说,imageData。长度必须与大小匹配-在调试器中检查它是否匹配。异常发生在哪里?PNT是什么?IMADATA是从什么类型来的?C++签名。@ JunWu不,我可以得到行和COLS信息,但是当我把它转换成字节时,问题就出现了。你不能那样做。您需要返回数据的大小。如果你有错误或其他什么,请更改标题和正文以反映这一点。这个问题将被重新打开。如果应该可以的话。是的,它可以很好的工作,但是它没有强调意图。上一个OP的问题谈到了传递变量作为引用。Ref强调,它对于大小=行(CLSS * 3)是一样的,我将尝试“外出”而不是参考。问题是,行和CARS是从C++代码返回的。所以它们是输出参数。ref参数是两种输入和输出参数。因为不需要读取它们的初始值,所以最好使用{out}而不是'ref'