简体   繁体   English

如何通过JNI C代码将void **传递给C库?

[英]How to pass void ** to a C library from JNI C code?

1) image_api.h defines following method - int process_image(const char *svgData, void **mapData) ; 1)image_api.h定义以下方法-int process_image(const char * svgData,void ** mapData) ;

2) now I need to call this method and pass proper values to process_image which is loaded from image_api.so file 2)现在我需要调用此方法并将正确的值传递给从image_api.so文件加载的process_image

-- Whats the correct approach for creating instance of void** in JNI C wrapper Code ? -在JNI C包装代码中创建void **实例的正确方法是什么?

3) 3)

JNIEXPORT jint JNICALL Java_JNITest_process_image(JNIEnv *env, jstring svgData, jobject mapData, jint status) {

    const char *str;
    str = (*env)->GetStringUTFChars(env, svgData, NULL);
    **status = process_image(str,  (void**)&mapData);** 

return status;
}

//////////// ////////////

I am facing UnsatisfiedLinkError while invoking process_image as the method signature not matching 由于方法签名不匹配而调用process_image时,我面临UnsatisfiedLinkError

In your code 在你的代码中

JNIEXPORT jint JNICALL 
Java_JNITest_process_image(JNIEnv *env, 
    jstring svgData, 
    jobject mapData, // this is some Java object, you need to access it
                     // take a look here: 
                     // http://jnicookbook.owsiak.org/recipe-No-020/
    jint status      // you don't need that, and you can't return value
                     // like this in JNI
    ) {

    const char *str;
    str = (*env)->GetStringUTFChars(env, svgData, NULL);
    // Question is ... what exactly process_image does?
    // without any additional knowledge about process_image
    // it is hard to guess what goes here
    int status = process_image(str, &pointer_to_some_memory_region ); 
    return status;
}

Based on the updates, without Java the code would have looked like: 基于更新,如果没有Java,代码将如下所示:

void * mapData; 
int status = process_image(svgData, &mapData);
...
int result = process_MapData(mapData);

Now we want to call process_image and process_MapData from separate native Java methods: 现在,我们要从单独的本机Java方法中调用process_image和process_MapData:

processImage(svgData, ?);
...
int result = processMapData(?);

Note that Java does not have pointers, so we must find some way to wrap void* . 请注意,Java没有指针,因此我们必须找到某种包装void* Luckily, size of such pointer is 64 bits or less. 幸运的是,此类指针的大小为64位或更小。 Java has a standard data type long that is just the right size. Java具有为适当大小的标准数据类型。

So, we can use 因此,我们可以使用

native static long processImage(String svgData);
native static int porocessMapData(long mapPtr);
...
long mapPtr = processImage(svgData);
...
int result = processMapData(mapPtr);

Here is the C side: 这是C面:

JNIEXPORT jlong JNICALL Java_JNITest_processImage(JNIEnv *env, jclass clazz, jstring svgData) {

    char *str = (*env)->GetStringUTFChars(env, svgData, NULL);
    void* mapData;
    process_image(str, &mapData);
    (*env)->ReleaseStringUTFChars(env, svgData, str);
    return (jlong)mapData;
}

JNIEXPORT jint JNICALL Java_JNITest_processMapData(JNIEnv *env, jlcass clazz, jlong mapData) {
    return process_mapData((void *)mapData);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM