这里解决的问题是把C的struct传给Java,并读写其中的内容。仅仅是读写C的struct,不需要把C的数据结构映射到java。
注:仅是一个通路,未考虑内存管理。
定义C的数据结构
c_demo.h
typedef struct testStruct {
int id;
char* name;
} testStruct;
定义Java的访问接口
JNIDemo.java
public class JNIDemo {
static {
System.loadLibrary("c_demo"); // load c library
}
/* Program entry function */
public static void main(String args[]) {
// New a C struct, and get pointer as handle
long handle = create();
System.out.println(handle); //print the pointer
// Set ID field to 99
setID(handle, 99);
int new_id = getID(handle);
System.out.println(new_id); //print ID setted
// Set NAME field
setName(handle, "asdfBB");
String new_name = getName(handle);
System.out.println(new_name); //print NA