C ++中的CvRect *到C#中的Rectangle []

时间:2015-03-18 22:25:04

标签: c# c++

我在C#中开发应用程序,我需要使用C ++ dll中定义的方法。该方法返回CvRect*,我想知道如何以某种方式进行包装以将其用作Rectangle[]。我在C#中使用Emgu Cv。

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

extern "C" DLLEXPORT 
CvRect* MyClass::MyMethod(char* path, int* lenght);    

其中lenght是返回CvRect的数量。

C#代码:

[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));
}

0 个答案:

没有答案
相关问题