是否可以通过c#将SafeHandles数组发送到本机函数?

问题描述

我有一个本机(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数组还是有更好的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)