痛饮 - 生成包装来传递结构数组 [英] Swig - generate wrapper to pass an array of struct
问题描述
我刚刚发现了关于%适用
指令(<一个href=\"http://stackoverflow.com/questions/14134598/how-to-pass-double-from-c-sharp-to-c-using-swig?rq=1\">see这里实例)。不幸的是,我不能使它与结构工作:
I have just found out about the %apply
directive (see here for instance). Unfortunately I can't make it work with structs:
%module packer_cs
%include "carrays.i"
%{
#include "packer.h"
%}
%include "typemaps.i"
%include "arrays_csharp.i"
%apply image_t INPUT[] { image_t *images }
%include "packer.h"
其结果是:
swig -csharp -outdir bin\csharp packer\packer.i
Warning 453: Can't apply (image_t INPUT[]). No typemaps are defined.
我无法找到任何关于这一警告的任何文件。它看起来像我必须定义一个定制的类型映射,但是我不知道从哪里开始。我长大pretty厌倦了这种缺乏文档和pretty绝望的一天写我的包装成功的:(
I can't find any documentation whatsoever about this warning. It looks like I have to define a custom typemap, but I have no idea where to start. I'm growing pretty tired of this lack of documentation, and pretty desperate of one day succeeding in writing my wrapper :(
随着我的<一个href=\"http://stackoverflow.com/questions/24434869/compile-a-c-library-with-visual-studio-2010\">two <一href=\"http://stackoverflow.com/questions/24436262/linker-errors-in-c-program-lnk2019-and-lnk1120\">$p$pvious文章中,我仍然在C#中使用C库有问题。
Following my two previous posts, I still have problems using a C library in C#.
要回顾一下:我有一个C库,和我想要编写使用痛饮一个C#包装
To recap: I have a C library, and I'm trying to write a C# wrapper using Swig.
在h文件中,该方法被声明这样:
In the .h file, the method is declared such:
int pack(image_t *images, int nb_images, parameters_t params);
其实, *图片
总是通过一个数组。如果我只是产生这样痛饮文件,在C#中的文件,该函数需要一个实例:
Actually the *images
is always passed an array. If I just generate swig files like this, in the C# files, the function expects a single instance:
public static int pack(image_t images, int nb_images, parameters_t arg2) {
int ret = packer_csPINVOKE.pack(image_t.getCPtr(images), nb_images, parameters_t.getCPtr(arg2));
if (packer_csPINVOKE.SWIGPendingException.Pending) throw packer_csPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
所以,下面的此评论,我已经修改了我的.i文件,例如:
So, following this comment, I have modified my .i file such :
%module packer_cs
%include <carrays.i>
%{
#include "packer.h"
%}
%include "packer.h"
%array_functions(image_t, image_t_array);
现在我可以使用 new_image_t_array
和 image_t_array_setitem
方法建立自己的打假阵列,但是当我跑我测试程序,它抛出一个 System.AccessViolationException
(它试图读取或写入受保护的内存)在包
方法调用(C#程序):
Now I can build my "fake" array using the new_image_t_array
and image_t_array_setitem
methods, but when I run my test program, it throws a System.AccessViolationException
(it's trying to "read or write protected memory") on the pack
method call (C# program):
// the "images" variable is a List<image_t>
var images_array = packer_cs.new_image_t_array(images.Count);
for (var i = 0; i < images.Count; i++)
{
packer_cs.image_t_array_setitem(images_array, i, images[i]);
}
// this throws the exception
var result = packer_cs.pack(images_array, images.Count, param);
其实,异常是在 INT RET = packer_csPINVOKE.pack抛出(image_t.getCPtr(图像),nb_images,parameters_t.getCPtr(ARG2));
行由痛饮生成的C#文件。
Actually, the exception is thrown at the int ret = packer_csPINVOKE.pack(image_t.getCPtr(images), nb_images, parameters_t.getCPtr(arg2));
line in the C# file generated by Swig.
任何想法,为什么发生这种情况?正如我在其他职位说,我什么都不知道的C / C ++,指针的东西,所以它可能是显而易见的......
Any idea as to why this is happening ? As I said in my other posts, I know nothing about C/C++, pointers and stuff, so it may be obvious...
谢谢!
推荐答案
我不知道,如果它很重要,不能测试,但你的。我可能要导出包
的array_functions后功能:
I don't know if it matters, can't test, but Your .i might have to export the pack
function after the array_functions:
%module packer_cs
%include <carrays.i>
%{
#include "packer.h"
%}
%array_functions(image_t, image_t_array);
%include "packer.h"
此外,没有强迫你声明完全相同的签名。你也许可以有这样的事情:
Also, nothing forces you to declare the exact same signature. You can probably have something like this:
%module packer_cs
%include <carrays.i>
%{
#include "packer.h"
%}
// export image_t and parameters_t, then:
int pack(image_t images[], int nb_images, parameters_t params);
该包装code将调用包的C版本,给它的数组,这是确定的,因为函数需要一个指向image_t和C知道如何与数组指针的作品。
The wrapper code will call the C version of pack, giving it the array, this is ok because the function takes a pointer to image_t and C knows how to works with array as pointer.
更新:由于上述没有帮助,我把在看 arrays_csharp.i
:调用 CSHARP_ARRAYS
和 CSHARP_ARRAYS_FIXED
宏观上所有的基本类型,并附带SWIG阵列的例子的。我调用了适用
尽管如此所以它可能是%适用
不会自动执行此操作。事实上,它看起来像这两个宏是的的typemaps,所以我认为这是值得一试:
Update: Since the above didn't help, I took at look at arrays_csharp.i
: it calls CSHARP_ARRAYS
and CSHARP_ARRAYS_FIXED
macro on all basic types, and the .i of the arrays example that is included with SWIG calls the apply
nonetheless so it may be that %apply
does not do this automatically. In fact, it looks like these two macros are the typemaps, so I think it is worth a try:
%module packer_cs
%include "carrays.i"
%{
#include "packer.h"
%}
%include "typemaps.i"
%include "arrays_csharp.i"
CSHARP_ARRAYS(image_t, image_t)
CSHARP_ARRAYS_FIXED(image_t, image_t)
%apply image_t INPUT[] { image_t *images }
%include "packer.h"
这篇关于痛饮 - 生成包装来传递结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!