Search code examples
javaeclipsetypesjava-native-interface

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


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

1. unsigned long Initialize(void* userData);
2. unsigned long Uninitialize(void);
3. unsigned long DeviceOpen( unsigned long id, unsigned long* device);
4. unsigned long DeviceClose( unsigned long device );

and so on ...

I want to use this library's (functionality.so) functionality in my java application for android. For that I create jni folder in my android application project folder and place there files:

  1. Android.mk

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE           := Test_library
    LOCAL_SRC_FILES        := Test_library.c
    ## Linking functionality library
    LOCAL_LDLIBS := -lfunctionality
    include $(BUILD_SHARED_LIBRARY)
    
  2. Test_library.c

    #include <string.h>
    #include <jni.h>
    #include "Test_library.h"
    
    jint Java_com_Dsm_Test_DsmLibraryTest_vtUninitialize(JNIEnv* env, jobject thiz) {
    return Uninitialize( );
    }
    
    jint Java_com_Dsm_Test_DsmLibraryTest_vtDeviceClose(JNIEnv* env, jobject thiz, jint hDevice) {
    return DeviceClose( hDevice );
    }
    
  3. Test_library.h

    A header file where Initialize, Uninitialize, DeviceOpen, DeviceClose functions are declared.

After this I run ndk-build and create a Test_library.so library and load it in my java application and use them like this:

// Some code

public native int Uninitialize( );

public native int DeviceClose( int hDevice );

static {
    System.loadLibrary("Test_library");
}

Everything Runs fine. After I want to add other two functions

1. unsigned long Initialize(void* userData);
2. unsigned long DeviceOpen( unsigned long id, unsigned long* device);

`

Now Questions:

  1. How I can write this two functions as native in java ? As there are no void* or unsigned long* types in java
  2. How I can write same functions in Test_library.c as in jni.h there are no void** or unsigned long* types

Thanks for help.


Solution

  • 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.