JNI: From C code to Java and JNI(JNI:从 C 代码到 Java 和 JNI)
问题描述
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 ofblobData_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 ofblobData_t* pInputInfo
in order this function works right and given parameter be the same
Reference
- How to pass C structs back and forth to Java code in JNI ?
- Passing large C structure through JNI efficiently
- Return a Structure Object from C to Java Through JNI
- Pass data between Java and C
- Passing a pointer from JNI to Java using a long
- Passing pointers between C and Java through JNI
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.
这篇关于JNI:从 C 代码到 Java 和 JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JNI:从 C 代码到 Java 和 JNI


- 未找到/usr/local/lib 中的库 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 转换 ldap 日期 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01