我有一个本机 (c++) 函数,它将一组指向图像的指针作为参数。在我的 c# API 中,指向图像的指针保存为 SafeHandle。现在我想向我的函数发送一个 SafeHandles 数组,但随后出现以下错误。
System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #2': Invalid managed/unmanaged type combination (Arrays of SafeHandles are not supported).
简化后的代码如下所示:
本机端
int concatenate_images(Image **img_ref, Image **images)
{
// Concatenate the images and put the result in img_ref[0]
}
托管方
请注意,ImageHandle 派生自 SafeHandle
Image Concatenate(Image[] images)
{
ImageHandle h = new ImageHandle();
if (!concatenate_images(ref h, handles_array))
throw new Exception("Failed concatenating images.");
return new Image(h);
}
private static extern int concatenate_images(ref ImageHandle img_ref, [In, Out] ImageHandle [] images)
我当然可以通过从 SafeHandle 中提取 IntPtr 并将这些对象的数组发送到我的本机函数来解决问题。但据我了解,我不会获得 SafeHande 提供的一些额外安全性。
那么,我应该如何将我的对象发送到我的本机函数?我必须制作一个 IntPtr 数组还是有更好的方法?