Convert a member of structure of type signed char * to byte array in Java (byte[]) using SWIG

假装没事ソ 提交于 2019-12-05 17:01:40

That's pretty close to what you want. You don't want the [ANY] since the size of the array is not "fixed" in C (it's specified by an int, but that's not part of its type).

You can make your typemap work with:

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

I might be missing something here, but I can't see a better way of getting hold of the "self" pointer from within an out typemap than this - arg$argnum doesn't work and neither does $self. There aren't any other typemaps that get applied to this function that would help.

(Note you probably also want to write a memberin for signed char * content or make it immutable. I'd be tempted to %ignore the contentLength member entirely too).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!