c# - Swig - generate wrapper to pass an array of struct -


update

i have found out %apply directive (see here instance). unfortunately can't make work 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" 

this results in:

swig -csharp -outdir bin\csharp packer\packer.i warning 453: can't apply (image_t input[]). no typemaps defined. 

i can't find documentation whatsoever warning. looks have define custom typemap, have no idea start. i'm growing pretty tired of lack of documentation, , pretty desperate of 1 day succeeding in writing wrapper :(

original post

following two previous posts, still have problems using c library in c#.

to recap: have c library, , i'm trying write c# wrapper using swig.

in .h file, method declared such:

int pack(image_t *images, int nb_images, parameters_t params); 

actually *images passed array. if generate swig files this, in c# files, function expects 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;   } 

so, following this comment, have modified .i file such :

%module packer_cs %include <carrays.i> %{     #include "packer.h"  %} %include "packer.h" %array_functions(image_t, image_t_array); 

now can build "fake" array using new_image_t_array , image_t_array_setitem methods, when run test program, throws system.accessviolationexception (it's trying "read or write protected memory") on pack method call (c# program):

// "images" variable list<image_t> var images_array = packer_cs.new_image_t_array(images.count); (var = 0; < images.count; i++) {     packer_cs.image_t_array_setitem(images_array, i, images[i]); }  // throws exception var result = packer_cs.pack(images_array, images.count, param); 

actually, exception thrown @ int ret = packer_cspinvoke.pack(image_t.getcptr(images), nb_images, parameters_t.getcptr(arg2)); line in c# file generated swig.

any idea why happening ? said in other posts, know nothing c/c++, pointers , stuff, may obvious...

thanks !

i don't know if matters, can't test, .i might have export pack function after array_functions:

%module packer_cs %include <carrays.i> %{     #include "packer.h"  %} %array_functions(image_t, image_t_array); %include "packer.h" 

also, nothing forces declare exact same signature. can have this:

%module packer_cs %include <carrays.i> %{     #include "packer.h"  %}  // export image_t , parameters_t, then:  int pack(image_t images[], int nb_images, parameters_t params); 

the wrapper code call c version of pack, giving array, ok because function takes pointer image_t , c knows how works array pointer.

update: since above didn't help, took @ @ arrays_csharp.i: calls csharp_arrays , csharp_arrays_fixed macro on basic types, , .i of arrays example included swig calls apply nonetheless may %apply not automatically. in fact, looks these 2 macros are typemaps, think worth 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" 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -