From: David P. <dpi...@me...> - 2007-12-27 23:21:52
|
> I am attempting to call a function like >=20 > int sum(int inArray[], unsigned int length) > { > int retval =3D 0; > unsigned int i =3D 0; > for ( i =3D 0; i<length; ++i) > { > retval +=3D inArray[i]; > } > return retval; > } >=20 > Swig generates >=20 > public static int sum(SWIGTYPE_p_int inArray, int length) { > int ret =3D examplePINVOKE.sum(SWIGTYPE_p_int.getCPtr(inArray), length); > return ret; > } >=20 > but I am a bit confused about what to do with a SWIGTYPE_p_int >=20 > I was expecting to call it with something like this: >=20 > int[] addArray =3D { 1, 2, 3 }; > Console.WriteLine("Sum =3D " + sum(addArray, add.Length)); >=20 > Here is my interface file: >=20 > /* File : example.i */ > %module example >=20 > %inline %{ > extern int gcd(int x, int y); > extern double Foo; > extern int sum(int inArray[], int length); >=20 > %} >=20 > Thanks I don't know whether SWIG has any specific feature for doing this, but in my project I cooked up a macro that will let you call sum() just as you suggest. However, in my experience so far, it looks like the .NET framework creates a copy of the array before passing it to the C code, so you might want to look for a more efficient method (perhaps involving a call to GCHandle.AddrOfPinnedObject... but I'm not sure how to integrate such a technique into SWIG.) %define %cs_marshal_array(TYPE, CSTYPE) %typemap(ctype) TYPE[] "void*" %typemap(imtype, inattributes=3D"[MarshalAs(UnmanagedType.LPArray)]") TYPE[] "CSTYPE[]" %typemap(cstype) TYPE[] "CSTYPE[]" %typemap(in) TYPE[] %{ $1 =3D (TYPE*)$input; %} %typemap(csin) TYPE[] "$csinput" %enddef %cs_marshal_array(bool, bool) %cs_marshal_array(short, short) %cs_marshal_array(unsigned short, ushort) %cs_marshal_array(int, int) %cs_marshal_array(unsigned int, uint) %cs_marshal_array(long, int) %cs_marshal_array(unsigned long, uint) %cs_marshal_array(long long, long) %cs_marshal_array(unsigned long long, ulong) %cs_marshal_array(float, float) %cs_marshal_array(double, double) |