Discussion:
[Swig-user] [C#] - Member array typemaps
schullq
2017-05-23 12:39:24 UTC
Permalink
Hello,

I have multiple arrays of unsigned char in mutliple structures and classes,
like this:

struct Test {
unsigned char myArray[5];
};

I wrote a macro of multiple typemaps to handle them array in C#. Here are my
typemaps:

%define CSHARP_MEMBER_ARRAYS( CTYPE, CSTYPE )

%typemap(ctype) CTYPE MBINOUT[] "CTYPE*"
%typemap(cstype) CTYPE MBINOUT[] "CSTYPE[]"
%typemap(imtype, out="System.IntPtr")
CTYPE MBINOUT[] "CSTYPE[]"
%typemap(csin) CTYPE "$csinput"
%typemap(in) CTYPE MBINOUT[] "$1 = $input;"
%typemap(csout, excode=SWIGEXCODE)
CTYPE MBINOUT[] {
CSTYPE[] ret = new CSTYPE[$1_dim0];
System.IntPtr data = $imcall;
System.Runtime.InteropServices.Marshal.Copy(data, ret, 0, $1_dim0);$excode
return ret;
}
%typemap(csvarout, excode=SWIGEXCODE2)
CTYPE MBINOUT[] %{
get {
CSTYPE[] ret = new CSTYPE[$1_dim0];
System.IntPtr data = $imcall;
System.Runtime.InteropServices.Marshal.Copy(data, ret, 0,
$1_dim0);$excode
return ret;
}
%}
%typemap(csvarin, excode=SWIGEXCODE2)
CTYPE MBINOUT[] %{
set {
if ($csinput.Length > $1_dim0) {
throw new System.IndexOutOfRangeException();
}
$imcall;$excode
}
%}
%typemap(freearg) CTYPE MBINOUT[] ""
%typemap(argout) CTYPE MBINOUT[] ""

%enddef // CSHARP_MEMBER_ARRAYS

CSHARP_MEMBER_ARRAYS(unsigned char, byte)

Everything is working pretty good, except in the variable set property
(csvarin typemap):

public byte[] myArray {
set {
if (value.Length > 3) {
throw new System.IndexOutOfRangeException();
}
examplePINVOKE.myClass_myStruct_myArray_set(swigCPtr,
SWIGTYPE_p_unsigned_char.getCPtr(value));
}

SWIG still tries to create an HandleRef as the second argument of the
imcall, but it just needs a byte[], which is the current value. It should
understand that, but I can't get why it does'nt. I'm pretty sure I failed
somewhere in the typemaps but I can't get where. If anyone could help, it
would be great.

Quentin.



--
View this message in context: http://swig.10945.n7.nabble.com/C-Member-array-typemaps-tp15170.html
Sent from the swig-user mailing list archive at Nabble.com.
William S Fulton
2017-07-01 21:13:27 UTC
Permalink
Post by schullq
Hello,
I have multiple arrays of unsigned char in mutliple structures and classes,
struct Test {
unsigned char myArray[5];
};
I wrote a macro of multiple typemaps to handle them array in C#. Here are my
%define CSHARP_MEMBER_ARRAYS( CTYPE, CSTYPE )
%typemap(ctype) CTYPE MBINOUT[] "CTYPE*"
%typemap(cstype) CTYPE MBINOUT[] "CSTYPE[]"
%typemap(imtype, out="System.IntPtr")
CTYPE MBINOUT[] "CSTYPE[]"
%typemap(csin) CTYPE "$csinput"
The above line should be:
%typemap(csin) CTYPE MBINOUT[] "$csinput"

William

Loading...