Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 294
Viktor Apoyan Avatar asked Dec 04 '22 22:12

Viktor Apoyan


2 Answers

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.

like image 120
Ernest Friedman-Hill Avatar answered Dec 07 '22 10:12

Ernest Friedman-Hill


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

like image 28
pron Avatar answered Dec 07 '22 12:12

pron