使用SWIG将signed char *类型的结构成员转换为Java(byte )中的字节数组

我正在尝试将signed char *类型的结构成员转换为Java中的字节数组。 我有以下结构:

typedef struct { signed char * content; int contentLength; } Foo; 

我试过这个:

 %typemap(jni) signed char *content [ANY] "jbyteArray" %typemap(jtype) signed char *content [ANY] "byte[]" %typemap(jstype) signed char *content [ANY] "byte[]" %typemap(javaout) signed char *content [ANY] { return $jnicall; } %typemap(memberin) int contentLength [ANY] { int length=0; $1 = &length; } %typemap(out) signed char * content [ANY] { $result = JCALL1(NewByteArray, jenv, length); JCALL4(SetByteArrayRegion, jenv, $result, 0, length, $1); } 

但没有结果。 Foo的方法getContent具有以下签名:

 SWIGTYPE_p_signed_char getContent(); 

我希望这个方法返回byte []。 有解决方案吗?

这非常接近你想要的。 你不需要[ANY]因为数组的大小在C中没有“固定”(它由int指定,但它不是它的类型的一部分)。

您可以使您的typemap适用于:

 %module test %typemap(jni) signed char *content "jbyteArray" %typemap(jtype) signed char *content "byte[]" %typemap(jstype) signed char *content "byte[]" %typemap(javaout) signed char *content { return $jnicall; } %typemap(out) signed char * content { $result = JCALL1(NewByteArray, jenv, arg1->contentLength); JCALL4(SetByteArrayRegion, jenv, $result, 0, arg1->contentLength, $1); } // Optional: ignore contentLength; %ignore contentLength; %inline %{ typedef struct { signed char * content; int contentLength; } Foo; %} 

我可能在这里遗漏了一些东西,但我看不出更好的方法是从out类型arg$argnum中获取“self”指针而不是这个 – arg$argnum不起作用,也不是$self 。 没有任何其他类型映射可以应用于此function,这将有所帮助。

(注意,您可能还想为signed char * content编写一个memberin或使其成为不可变的。我很想完全%ignore contentLength成员)。