Data interaction between java and c/c++ - jni

I recently worked on a TIemsten database project using jni technology. In this project, we use java to write the interface and business logic, and use the c language to write the database odbc access. Simple odbc is actually not difficult, but data transfer between java and c is a cumbersome thing. There are several cases of data transfer between the two: the interaction of the basic data types between java and c, java to the object type to c, c to the object type returned to java, c call java class. The following is a description of several cases.

1, java to pass the basic data type to c

For basic data types, java and c correspond to each other, so they can be used directly. Their correspondence is;

Java type local type byte (bit)
Boolean jboolean 8, unsigned
Byte jbyte 8
Char jchar 16, unsigned
Short jshort 16
Int jint 32
Long jlong ​​64
Float jfloat 32
Double jdouble 64
Void void n/a

2.java pass object type to c

For the java object model passed in java, c should load the prototype of the java class, according to the corresponding c object, get the id of the java object method, and then call the java object method. For example: For example, there is a java class customer object passed to the c program as a jni parameter, and the customer has the method String getName().

JNIEXPORT jobject JNICALL Java_com_oracle_estt_sc_db_impl_SCQueryODBC__1getCustomer
(JNIEnv *env, jobject, jobject customer){

jmethodID methodId;
/ / Get the handle of the customer object
Jclass cls_objClass=env->GetObjectClass(customer);
/ / Get the id of the specific method getName in the customer object
methodId=env->GetMethodID(cls_objClass,"getName","()Ljava/lang/String;");
/ / call the specific method of the customer object getName
Jstring js_name=(jstring)env->CallObjectMethod(customer,methodId,NULL);

...

}

3.c return object type to java

In the c program, you first need to create the java object to be returned, get the id of each attribute, then assign a value to each attribute, and finally return. For example: the same customer object, there are attribute values ​​such as name, you need to assign a value to each property in the c program and return.

JNIEXPORT jobject JNICALL Java_com_oracle_estt_sc_db_impl_SCQueryODBC__1getCustomer
(JNIEnv *env, jobject, jobject customer){

......

/ / Found java Customer class, if it fails, the program returns
Jclass clazz = env->FindClass("com/oracle/estt/sc/busi/Customer");
If(clazz == 0)
Return 0;
/ / allocate memory for the new java class object obj
Jobject obj = env->AllocObject(clazz);
/ / Discover the properties in the class, if it fails, the program returns
jfieldID fid_id= env->GetFieldID(clazz,"customerID","I");
If (fid_id == 0)
Return 0;
jfieldID fid_name = env->GetFieldID(clazz,"name","Ljava/lang/String;");
If (fid_name == 0)
Return 0;
......

Env->SeTIntField(obj, fid_id, 1
Env->SetObjectField(obj, fid_name, jname);

......

Return obj;

}

4.c pass an array containing java objects to java

For this case, first get the size of the array, then take the object in the array, get the property value of the object or call the object method, save the obtained value to the local array, and then use the data flexibly. For example: java passes an array containing multiple customer objects to c, and decomposes the array in c and stores it in a local temporary array.

JNIEXPORT void JNICALL Java_com_oracle_estt_sc_db_impl_SCInsertODBC__1insertCustomeRequest___3Lcom_oracle_estt_sc_busi_CustomerRequest_2
(JNIEnv *env, jobject, jobjectArray oa){

......

/ / Declare the customerrequest object
Jobject o_customer;

Int i;
jmethodID methodId;
Jint size=env->GetArrayLength(oa);

_tmp_bind[0]= (char *)malloc(size*sizeof(int));
_tmp_bind[1]= (char )malloc(size*sizeof(char)( 20 + 1));

...

/ / Copy the data of the input array into a temporary array
For(i=0;i

Automotive Diagnostic Connectors And Cables

We make OBD connector with terminal by ourselves, soldering type and crimping type are both available. Such as 16pin obd connector. OBD1, OB2, J1939, J1708, J1962, etc. Also molded by different type, straight type or right-angle type. The OBD connector cables used for Audi, Honda, Toyota, BWM, etc. We have wide range of materials source , also we can support customers to make a customized one to replace the original ones.

Automotive Diagnostic Connectors And Cables,Obd Connectors,Reliable OBD Connector,Black OBD Connector,OBD Diagnostic Cable,OBD2 Connectors

ETOP WIREHARNESS LIMITED , https://www.etopwireharness.com