jni not support types as void*, unsigned int*, … What to do?

前端 未结 4 498
失恋的感觉
失恋的感觉 2021-01-13 22:46

I have .so (shared library) written in C++, lets call it functionality.so in which I implement different functions, here is list of some fu

相关标签:
4条回答
  • 2021-01-13 23:01

    Have you considered using an automatic wrapper generator for this? I have used SWIG a few times now with much success. The reason that I mention it is that it has well developed patterns for dealing with things such as pointers, including void* and so on.

    Wrapper generation can be as easy as passing in the library's header file if the methods you want to wrap are as simple as the ones you list above. Even if you have some methods that are more complex (passing in structs, returning pointers to memory, etc) SWIG allows you to add code to the generated wrappers if needs be.

    Another plus point is that SWIG can wrap into a range of languages, not just Java.

    0 讨论(0)
  • 2021-01-13 23:07

    I consider @pron answer the best one, so far.

    About automatic JNI code generation, consider jnigen It is very simple to use and powerful.

    Get the Javadocs, and look for NativeCodeGenerator class. It has the applied mapping between Java types and native CPP types, as String to char*, int[] to int*, FloatBuffer to float*, etc.

    Jnigen as described on GitHub

    jnigen is a small library that can be used with or without libgdx which allows C/C++ code to be written inline with Java source code.

    jnigen has two parts:

    • Inspect Java source files in a specific folder, detect native methods and the attached C++ implementation, and spit out a C++ source file and header, similar to what you'd create manually with JNI.

    • Provide a generator for Ant build scripts that build the native source for every platform.

    Example: This are your Java native methods, with desired cpp code defined as comment just after the method declaration :

    private static native ByteBuffer newDisposableByteBuffer (int numBytes); /*
       char* ptr = (char*)malloc(numBytes);
       return env->NewDirectByteBuffer(ptr, numBytes);
    */
    
    private native static void copyJni (float[] src, Buffer dst, int numFloats, int offset); /*
       memcpy(dst, src + offset, numFloats << 2 );
    */
    

    After running jnigen generator, you will have the *.cpp file with your C code and bindings. The *.h is also created automagically.

    The cpp will looks like this:

    JNIEXPORT jobject JNICALL 
    Java_com_badlogic_gdx_utils_BufferUtils_newDisposableByteBuffer
    (JNIEnv* env, jclass clazz, jint numBytes) {
    //@line:334
       char* ptr = (char*)malloc(numBytes);
       return env->NewDirectByteBuffer(ptr, numBytes);
    }
    
    JNIEXPORT void JNICALL 
    Java_com_badlogic_gdx_utils_BufferUtils_copyJni___3FLjava_nio_Buffer_2II
    (JNIEnv* env, jclass clazz, jfloatArray obj_src, jobject obj_dst, jint numFloats, jint offset) {
       unsigned char* dst = (unsigned char*)env->GetDirectBufferAddress(obj_dst);
       float* src = (float*)env->GetPrimitiveArrayCritical(obj_src, 0);
    
    //@line:348
       memcpy(dst, src + offset, numFloats << 2 );
    
       env->ReleasePrimitiveArrayCritical(obj_src, src, 0);
    }
    

    This is how would look your methods, with jnigen:

     /**
     * @param userData - a pointer. Assumed position() is Zero.
     **/
    public native static long initialize(Buffer userData);/*
      return (jlong) Initialize( (void*) userData);
    */
    
    public native static long uninitialize();/*
      return (jlong) Uninitialize();
    */
    
    /**
     *  Assumptions : id points to a unique device
     * @param id - id
     * @param device - a long[] with length 1, to return device pointer.
     */
    public native static long deviceOpen(long id, long[] device);/*
      return (jlong) DeviceOpen( (unsigned long) id, (unsigned long*) device);
    */ 
    
    public native static long deviceClose(long device);/*
      return (jlong) DeviceClose( (unsigned long) device);
    */ 
    
    0 讨论(0)
  • 2021-01-13 23:13

    What you want to do is this: For the void* function, use direct byte buffers. On the Java side:

    native long initialize(java.nio.ByteBuffer userData);
    

    When you call the method, make sure to allocate a direct ByteBuffer (see java.nio.ByteBuffer, and JNI nio usage):

    ByteBuffer myData = ByteBuffer.allocateDirect(size);
    long res = initialize(myData);
    

    On the C side, you do this:

    unsigned long res = Initialize(env->GetDirectBufferAddress(env, buffer));
    return (jlong)res;
    

    You read and write from the buffer on the Java side with the ByteBuffer methods.

    You can also allocate the byte buffer on the C side with env->NewDirectByteBuffer(env, ptr, size.

    Now, for the second function, I assume the unsigned long* argument is used to return a result. You can use the same approach (direct ByteBuffers), but I would recommend a different one that wouldn't entail allocating a buffer for such a small value.

    On the Java side:

    native long deviceOpen(long id, long[] device);
    

    On the C side:

    unsigned long c_device;
    unsigned long res = DeviceOpen((unsigned long)j_id, &c_device);
    env->SetLongArrayRegion(env, j_device, 0, 1, &c_device);
    return (jlong)res;
    

    Then you call the method from Java:

    long[] deviceOut = new long[1];
    long res = deviceOpen(id, deviceOut);
    long device = deviceOut[0];
    

    For more information on array access from JNI see JNI array operations

    0 讨论(0)
  • 2021-01-13 23:14

    You can use jlong to pass a pointer (or a pointer to pointer, or whatever) back to Java. Java code won't be able to use it for anything, other than passing it as an argument to one of your other methods; but often that's all you really want. If, on the other hand, you want Initialize() to be called with data set up in Java, then void * isn't appropriate; you'll need to use a Java class, and use reflection in JNI to get the information you need out of it.

    Crudely, you could wrap malloc() and free():

    jlong Java_c_utils_malloc(JNIEnv* env, jclass clazz, jint size) {
        return (jlong) malloc(size);
    }
    
    void Java_c_utils_free(JNIEnv* env, jclass clazz, jlong ptr) {
       free((void *) ptr);
    }
    

    and then use them (to no effect!) in Java:

    long ptr = utils.malloc(100);
    // Store ptr for a while
    utils.free(ptr);
    

    Now, if we wrapped some other functions that needed a block of memory as an argument, we could wrap them too, and let them accept a jlong argument, the same way free() does. The fact that the Java variable ptr represents a memory address is completely opaque in Java, but it's useful nonetheless.

    Window system implementations for Java (i,e., AWT, SWT) use this same sort of thing to associate the native widget handle with the Java component.

    Now, if you want your Initialize() to be able to take useful arguments from Java, then a void * isn't going to cut it. You'd need to write your method to accept a Java object as an argument; that's the only way to allow you to manipulate the object in Java.

    I don't want to duplicate all the code here, but Sun's JNI tutorial is here. This is the section on calling arbitrary methods of a Java object (either the this object, or one passed to your method as an argument) and this is an analogous section on accessing the fields of an object.

    0 讨论(0)
提交回复
热议问题