JNI: From C code to Java and JNI

Viktor Apoyan :

Background

I am developing an application for android in eclipse and now I have a problem and I need your help. So I must Call function written in C from JAVA application. But on my way of writing code I have some Questions which you can see below. I am waiting for your answers and ideas ...

C Code:

typdef struct blobData_s {
    unsigned long length;
    unsigned char data[1];
} blobData_t;

unsigned int CheckEnrollmentExist ( unsigned long hdevice, blobData_t* pInputInfo ) {
    // Function code goes here
    ..........................
    return some_value;
}

JAVA Code:

In JAVA code instead of unsigned long I use int so I can write.

class jblobData_c {
    public int langth;
    *Question 1.*
}

public class ApplicationMainClass extends Activity {
    // Some code goes here
    ......................

    public native int JCheckEnrollmentExist( int jhdevive, *Question 2.* );

}

Question 1.

  • What I can use instead of unsigned char in JAVA Code ?
  • What I must write in JAVA code instead of unsigned char data[1]; ?

Question 2.

  • How I can use class jblobData_c instead of blobData_t* pInputInfo in the JAVA Code ?
  • What I must write in JAVA instead of blobData_t* pInputInfo ?

JNI Code:

JNIEXPORT jint JNICALL Java_com_Test_JCheckEnrollmentExist(JNIEnv* env, jobject obj, jint jhdevice, *Question 2.* ) {

    // Call the base function from C code.
    return CheckEnrollmentExist( jhdevice, *Question 3.*);
}

Question 3.

  • What I must write in CheckEnrollmentExist function that is C Code Function instead of blobData_t* pInputInfo in order this function works right and given parameter be the same

Reference

  1. How to pass C structs back and forth to Java code in JNI ?
  2. Passing large C structure through JNI efficiently
  3. Return a Structure Object from C to Java Through JNI
  4. Pass data between Java and C
  5. Passing a pointer from JNI to Java using a long
  6. Passing pointers between C and Java through JNI
Edwin Buck :

For question #1:

You can use a jchar. Primitive chars in java are not signed, it's about the only primitive that isn't. Note that jchar is a UTF-16 char, so you will have to "map" the jchar to a regular char, as you would have to with any character conversion issue. For simple conversions, this can typically be done by casting

char c_char = (char)java_char;

because the core ASCII shares the same numeric values between ASCII and UTF-16. However, this is prone to error should anyone actually attempt to pass a "special" character through the interface. A much better way would be to (in the java side, as it is easier) convert the characters to bytes using the appropriate character set for your platform (to ensure platform compatibility in the C layers). Then you only need to pass a byte[] to the JNI call, and the bytes will correctly correspond to the characters that C likely will expect.

For question #2:

If your CheckEnrollmentExists(...) method is the JNI binding entry point, you cannot change data types safely. That means that all entry inputs must be JNI data type values. While you might be able to select the C data type equivalents (and you might be able to get your compiler to do it anyway) such techniques should be frowned upon. This implicitly means that JNI entry points cannot accept struct data structure not defined in the JNI headers. In other words, you can't pass your own struct to the method.

If the method needs access to a C struct across calls, use another means. I've seen people store the pointer to the allocated data structure in a member integer or long (doing correct casting). You can then rewrite the native code side to retrieve the pointer from the "this" object being passed into the call, and the do a dereference to obtain the required data.

For Question #3:

This is actually the same as question #2. In the "binding wrapper" you put, you would retrieve the pointer's stored value in the java object's int or long field, cast it to the appropriate struct pointer and then pass it to the internal method. As the passing of the pointer is a C to C call, no extra magic is required.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using a java socket from JNI / C++ code

From Dev

Return a string from JNI (C++) code to Java

From Java

Rewrite C code in Java or use JNI?

From Java

passing string array from java to C with JNI

From Java

JNI: passing integer array from Java to C

From Dev

JNI conversion from Java to C++

From Dev

How to prevent my JNI C code from exiting while the JVM it launched is still running a Java thread?

From Dev

Integration of Fortran code with Java by JNI

From Dev

Escaping a Java string for JNI code

From Java

How to Use Eclipse to Debug JNI code (Java & C/C++)

From Java

JNI on Android: How to retrieve a string from Java code?

From Dev

Calling a Java Method from the native code using jni

From Java

How to pass C structs back and forth to Java code in JNI?

From Java

Can I reference C++ objects in Java Code using JNI?

From Java

Convert java code that returns object with template into c++ using jni

From Dev

How to debug native jni c++ code in eclipse with java project

From Dev

How to use JNI_GetCreatedJavaVMs in C++ to call Java Code

From Dev

Unable to make JNI call from c++ to java in android lollipop using jni

From Dev

Android Java Access from JNI

From Dev

UnsatisfiedLinkError in Java, coming from JNI

From Dev

Send MotionEvent from Java to JNI

From Dev

Create a Java enum from the JNI

From Dev

Calling RenderScript from C / JNI

From Java

Java C++ without JNI

From Dev

How to call a function with arguments in C++ from JAVA using JNI?

From Dev

Call a function in java from C outside of a JNI function (Android)?

From Java

How to pass List from Java to JNI C++ as vector by ref?

From Java

Can't call java methods from c++ wrapper in JNI

From Java

Passing strings from java to c++ using JNI

Related Related

  1. 1

    Using a java socket from JNI / C++ code

  2. 2

    Return a string from JNI (C++) code to Java

  3. 3

    Rewrite C code in Java or use JNI?

  4. 4

    passing string array from java to C with JNI

  5. 5

    JNI: passing integer array from Java to C

  6. 6

    JNI conversion from Java to C++

  7. 7

    How to prevent my JNI C code from exiting while the JVM it launched is still running a Java thread?

  8. 8

    Integration of Fortran code with Java by JNI

  9. 9

    Escaping a Java string for JNI code

  10. 10

    How to Use Eclipse to Debug JNI code (Java & C/C++)

  11. 11

    JNI on Android: How to retrieve a string from Java code?

  12. 12

    Calling a Java Method from the native code using jni

  13. 13

    How to pass C structs back and forth to Java code in JNI?

  14. 14

    Can I reference C++ objects in Java Code using JNI?

  15. 15

    Convert java code that returns object with template into c++ using jni

  16. 16

    How to debug native jni c++ code in eclipse with java project

  17. 17

    How to use JNI_GetCreatedJavaVMs in C++ to call Java Code

  18. 18

    Unable to make JNI call from c++ to java in android lollipop using jni

  19. 19

    Android Java Access from JNI

  20. 20

    UnsatisfiedLinkError in Java, coming from JNI

  21. 21

    Send MotionEvent from Java to JNI

  22. 22

    Create a Java enum from the JNI

  23. 23

    Calling RenderScript from C / JNI

  24. 24

    Java C++ without JNI

  25. 25

    How to call a function with arguments in C++ from JAVA using JNI?

  26. 26

    Call a function in java from C outside of a JNI function (Android)?

  27. 27

    How to pass List from Java to JNI C++ as vector by ref?

  28. 28

    Can't call java methods from c++ wrapper in JNI

  29. 29

    Passing strings from java to c++ using JNI

HotTag

Archive