我在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));
}