Wrap a C structure which has a double array member in C#!

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Wrap a C structure which has a double array member in C#!

Xutuan Zhang
Hello, all,

I am trying to wrap an C structure (see below) which has array members
to C#. I am really new to SWIG and I wrote the following type
maps.

****my structure******************
typedef struct {
   int a;
   int b;
   double arr[100];
} test_struct;


****my C# typemap*************************
%typemap(ctype) double *, double[ANY], double[]   "double *"
%typemap(imtype) double *, double[ANY], double[]   "double []"
%typemap(cstype) double *, double[ANY], double[]   "double []"
%typemap(csin) double *, double[ANY], double[] "$csinput"
%typemap(csout, excode=SWIGEXCODE) double *, double[ANY], double[] {
    double[] ret = $imcall;$excode
    return ret;
  }
%typemap(csvarin, excode=SWIGEXCODE2) double *, double[ANY], double[] %{
    set {
      $imcall;$excode
    } %}
%typemap(csvarout, excode=SWIGEXCODE2) double *, double[ANY], double[] %{
    get {
      double[] ret = $imcall;$excode
      return ret;
    } %}
*********************************


And in the generated test_struct.cs, the properties of the array member is:
**************************************
  public double [] arr {
    set {
      mytestPINVOKE.test_struct_arr_set(swigCPtr, value);
    }
    get {
      double[] ret = mytestPINVOKE.test_struct_arr_get(swigCPtr);
      return ret;
    }
  }
**************************************

But when I try to access the array from C#, I get:
************************************
Unhandled Exception:
System.Runtime.InteropServices.MarshalDirectiveException: Can not mar
shal return value.
..........
************************************

What am I doing wrong? How should I write the typemaps for this
structure so that I can access the array in C# simething like:
test_struct ts;
double[] csarray= ts.arr;
ts.arr = csarray; or ts.arr(csarray); etc.
double a1 = ts.arr[1];

Any hints/recommendations are very much appreciated! Please help!

Thanks,

Xutuan


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user