C+中的CvRect*+;到C中的矩形[]# 我在C开发一个应用程序,我需要使用C++中的一个方法。该方法返回一个CvRect*,我想知道如何以某种方式将其包装为矩形[]。我在C#中使用Emgu Cv

C+中的CvRect*+;到C中的矩形[]# 我在C开发一个应用程序,我需要使用C++中的一个方法。该方法返回一个CvRect*,我想知道如何以某种方式将其包装为矩形[]。我在C#中使用Emgu Cv,c#,c++,C#,C++,.h中的方法签名如下所示: extern "C" DLLEXPORT CvRect* MyClass::MyMethod(char* path, int* lenght); 其中lenght是返回的CvRect数 C#代码: [DllImport(“MyDLL.dll”,EntryPoint=“MyMethod”)] 公共静态外部IntPtr MyMethod(字符串路径,ref int length); 整数长度=0; IntPtr h_结果=MyMethod(路径,参考长度);

.h中的方法签名如下所示:

extern "C" DLLEXPORT 
CvRect* MyClass::MyMethod(char* path, int* lenght);    
其中
lenght
是返回的
CvRect

C#代码:

[DllImport(“MyDLL.dll”,EntryPoint=“MyMethod”)]
公共静态外部IntPtr MyMethod(字符串路径,ref int length);
整数长度=0;
IntPtr h_结果=MyMethod(路径,参考长度);
CvRect[]结果=新的CvRect[长度];
var sizeInBytes=Marshal.SizeOf(typeof(CvRect));
对于(int i=0;i
请提供方法签名。顺便说一句,您如何知道返回数组的大小?@lucastzesniewski.h中的方法签名如下所示:extern“C”DLLEXPORT CvRect*MyClass::MyMethod(char*path,int*lenght);其中lenght是返回的CvRect的编号。请编辑您的原始帖子以包含方法签名,以便更容易阅读。还请包括
CvRect
@networkedspline的定义。方法签名已经包含在我的原始帖子中。CvRect是OpenCv提供的一个结构,它基本上表示一个矩形,具有宽度、高度以及左上角的x和y坐标等属性。我认为你不能直接包装它;您仍然需要在非托管空间(即作为
IntPtr
)中获取结果,然后将其传输到托管空间。这就是你遇到的麻烦吗?
[DllImport("MyDLL.dll", EntryPoint = "MyMethod")]
    public static extern IntPtr MyMethod(string path, ref int lenght);

int lenght = 0;
IntPtr h_result = MyMethod(path, ref lenght);

CvRect[] result= new CvRect[lenght];

var sizeInBytes = Marshal.SizeOf(typeof(CvRect));
for (int i = 0; i < lenght; i++)
{
    IntPtr p = new IntPtr((h_result.ToInt32() + i * sizeInBytes));

    result[i] = (CvRect)Marshal.PtrToStructure(p, typeof(CvRect));
}