如何将空指针函数表示为Android JNI?

时间:2019-03-28 06:04:41

标签: java-native-interface typedef void-pointers

我不知道如何正确映射它。

C方法

void* function(char* h, int p, void (*vrecv)(void* , uint8_t *, size_t ), 
               void* d, const char* n, uint8_t p8, uint16_t p16, uint32_t p32);

JNI包装器方法

JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_function( JNIEnv* env, jobject thiz,
     blah-blah ) {
     return blah-blah;
}

Java方法

public native void function();




有几个例子 通过java数组访问,使用方法CallVoidMethod或GetMethodId进行调用,但我不理解它们。很难通过java方法,c / c ++ func和jni包装方法来了解这个想法。

Tutorial for JNI API

Google Android NDK / JNI Samples


这个例子很好用:

HelloJni.java文件。

JNIEXPORT jint JNICALL Java_com_example_hellojni_HelloJni_example(JNIEnv*env,
     jobject obj) {
     return (jint) example();
}

hello-jni.c文件。

void* run_thread(void* num) {
    int* temp = (int*) num;

    while(++(*temp) < 777);

    printf("Hello world! C Style! From pthread and Android NDK(JNI).\n");

    return NULL;
}

int example() {
    int f = 0;
    pthread_t pipe;

    if(pthread_create(&pipe, NULL, run_thread, &x)) {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    sleep(1);

    printf("before x: %d\n", x);

    /* wait for the second thread to finish */
    if(pthread_join(pipe, NULL)) {
        fprintf(stderr, "Error joining thread\n");
        return 2;
    }

    printf("after x: %d\n", x);

    return x;
}

如何访问c数据结构?

public native void example1(/**/);


如何调用typedef struct的元素?

void *指针(长原因为64位的正确表示)的下一步是什么?

public native void example2(long cl);
public native void example3(/**/);
public native void example4(/**/);


void* example1(char* h, int p, void (*vr)(void* , uint8_t *, size_t ), void* vdt,
void (*ar)(void* , uint8_t *, size_t ), void* adt);
void example2(void* cl);
void example3(void* cl, uint8_t is_p, uint8_t num, const char* name, uint8_t btype, uint8_t v, uint8_t prod, uint16_t opts);
void example4(void* cl, uint8_t num, uint16_t t, uint32_t c, uint32_t v);


JNIEXPORT void JNICALL Java_com_example_hellojni_HelloJni_example1 (JNIEnv* env, jobject obj, jchar* h, jint p, jlong vr, jlong vdt, jlong ar, jlong adt) {
    const char *str= (*env)->GetStringUTFChars(env,h,0);

    example1(str, p, vr, vdt, ar, adt);
}
JNIEXPORT void JNICALL Java_com_example_hellojni_HelloJni_example2 (JNIEnv* env, jobject obj, jlong cl) {
    example2(cl);
}
JNIEXPORT void JNICALL Java_com_example_hellojni_HelloJni_example3 (JNIEnv* env, jobject obj, jlong cl, uint8_t is_p, uint8_t num,
        jstring name, uint8_t btype, uint8_t v, uint8_t prod, uint16_t opts) {

    const char *str= (*env)->GetStringUTFChars(env,name,0);

    example3(cl, (jshort)is_p, num, str, (jshort)btype, (jshort)v, (jshort)prod, (jshort)opts);
}
JNIEXPORT void JNICALL Java_com_example_hellojni_HelloJni_example4 (JNIEnv* env, jobject obj, jlong cl, uint8_t num, uint16_t t, uint32_t c, uint32_t v) {
    example4(cl, (jshort)num, (jshort) t, (jlong)(unsigned long long) c, (jlong)(unsigned long long) v);
}

0 个答案:

没有答案