0

CAndroid で Javaのネイティブ関数を呼び出したいときに、この問題が発生しますJNI。その関数のパラメーターとして構造体へのポインターを渡し、Cそのポインターにメモリを割り当てます。

CJavaでこの関数を呼び出す方法を教えてください。私は長い間グーグルで検索しましたが、報酬が得られません。

次のようなコードで、A は構造体の名前です。

JNIEXPORT int JNICALL
Java_pkgname_NativeTools_create(JNIEnv *env, jclass jclazz, A **ptr) {
    *ptr = (A*) malloc(sizeof(A));
    if (*ptr != NULL ) {
        return 0;
    } else {
        return -1;
    }
}

ネイティブインターフェイスの書き方がわかりません:

public class NativeTools{
    static {
        System.loadLibrary("LibName");
    }

    // TODO don't know what type in JNI should I use to declare this native function?
    public static native int create(?);
}

どうもありがとう、そして私の下手な英語でごめんなさい:(

4

1 に答える 1

0

You can't do it like this. You can use only JNI datatypes as C function parameters. Anyway, there is nothing like structs or even pointers in Java. What was it, that you wanted to create on Java side and then "pass it to C" ? The nearest to pointer you could get is a reference type jobject, but C doesn't understand it as simply as you tried. It's a reference to JVM object, not C struct. It's only meant as a parameter to further JNI calls (which calls back to JVM).

There are tools which could help you improving the datatype interoperability (SWIG,JNA), but given the apparent fundamental misunderstanding, you should start with some basic reading:

http://192.9.162.55/docs/books/jni/html/jniTOC.html

(can't do a proper link, SO forbids IP addresses)

于 2013-01-28T11:01:11.067 に答える