跟随我的three previous posts,我现在可以将托管结构数组传递给我的包装方法。以下是文件的摘录:
// packer.i
typedef struct {
int width; // input
int height; // input
frame_t view; // output
frame_t dest; // output
} image_t;
CSHARP_ARRAYS(image_t, image_t)
%apply image_t INOUT[] { image_t *images }
int pack(image_t *images, int nb_images, parameters_t params);
它生成一个具有此签名的函数:
// packer_cs.cs
public static int pack(image_t[] images, int nb_images, parameters_t arg2)
我这样称呼它:
// Program.cs
var files = Directory.GetFiles("./images");
var images = new image_t[files.Length];
for (var f = 0; f < files.Length; f++)
{
using (var imgInfo = Image.FromFile(files[f]))
{
var imgStruct = new image_t()
{
width = imgInfo.Width,
height = imgInfo.Height,
dest = new frame_t(),
view = new frame_t()
};
images[f] = imgStruct;
}
}
var result = packer_cs.pack(images, images.Length, new parameters_t());
一切都很好,但是当我运行时pack()
方法,我有一个受保护的内存访问问题(System.AccessViolationException
)。值得庆幸的是,我可以访问 C 库的源代码,只要我启用非托管代码调试,Visual Studio 就会自动打开它进行调试和单步执行。
所以,如果我在开始处添加一个断点pack()
功能,我用手表查看images[x]
,我可以看到宽度和高度值与提供的内容无关(有时甚至是 0 或负数)。这是怎么回事 ?如果我在 C# 端检查托管数组,则会正确存储和检索值。为什么 C 没有得到正确的值?其他参数(nb_images和params)没有任何问题。
谢谢 !