Quantcast

Passing an array of a C struct as function argument

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Passing an array of a C struct as function argument

ThomasB
I'm using SWIG 3 to generate a C# wrapper around a C library called "packer".

My packer.h looks like this (simplified) :

#ifndef INCLUDE_PACKER_H
#define INCLUDE_PACKER_H
typedef struct {
        int width;
        int height;
        frame_t view;
        frame_t dest;
} image_t;
int pack(image_t *images, int nb_images, parameters_t params);
#endif

I have tried with a "naïve" packer.i file:

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

It generates a C# method like this:

  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;
  }

That I use like this (C#):

// images is a List<image_t>
var images_array = new image_t_array(images.Count);
for (var i = 0; i < images.Count; i++)
{
        images_array.setitem(i, images[i]);
}
var result = packer_cs.pack(images_array.cast(), images.Count, param);

Two questions:

* The last line throws an AccessViolationException (trying to read or write from/to protected memory). Any idea why, or how I can debug it?

* This looks unnatural to me (and the library wrapper might be used by, let's say, not-top-notch developers). I would rather pass a C# array of image_t to my method. I've tried adding %apply image_t INPUT[] { image_t *images } to my packer.i file, but now SWIG tells me : "Warning 453: Can't apply (image_t INPUT[]). No typemaps are defined." I have a very hard time finding proper documentation for this thing. Any help ?

Thank you!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Passing an array of a C struct as function argument

ThomasB
So I finally found someone who could help me : http://stackoverflow.com/a/24463274/6776

So now I can actually pass a managed array as parameter for the method:

// packer.i
typedef struct {
  int width;
  int height;
  frame_t view;
  frame_t dest;
} 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
public static int pack(image_t[] images, int nb_images, parameters_t arg2)

-------------------

But I still have a protected memory access problem. Actually, in the the image_t structure, the frame_t view and frame_t dest are output properties, but I'm guessing the wrapper uses them as read-only.

How can I tell SWIG that the unmanaged DLL can modify the two frame_t members of the image_t structure ?

Thanks !


2014-06-30 14:48 GMT+02:00 ThomasB <[hidden email]>:
I'm using SWIG 3 to generate a C# wrapper around a C library called "packer".

My packer.h looks like this (simplified) :

#ifndef INCLUDE_PACKER_H
#define INCLUDE_PACKER_H
typedef struct {
        int width;
        int height;
        frame_t view;
        frame_t dest;
} image_t;
int pack(image_t *images, int nb_images, parameters_t params);
#endif

I have tried with a "naïve" packer.i file:

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

It generates a C# method like this:

  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;
  }

That I use like this (C#):

// images is a List<image_t>
var images_array = new image_t_array(images.Count);
for (var i = 0; i < images.Count; i++)
{
        images_array.setitem(i, images[i]);
}
var result = packer_cs.pack(images_array.cast(), images.Count, param);

Two questions:

* The last line throws an AccessViolationException (trying to read or write
from/to protected memory). Any idea why, or how I can debug it?

* This looks unnatural to me (and the library wrapper might be used by,
let's say, not-top-notch developers). I would rather pass a C# array of
image_t to my method. I've tried adding %apply image_t INPUT[] { image_t
*images } to my packer.i file, but now SWIG tells me : "Warning 453: Can't
apply (image_t INPUT[]). No typemaps are defined." I have a very hard time
finding proper documentation for this thing. Any help ?

Thank you!



--
View this message in context: http://swig.10945.n7.nabble.com/Passing-an-array-of-a-C-struct-as-function-argument-tp13870.html
Sent from the swig-user mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user


------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...