Step 4: Write C/C ++ code
When talking about writing C/C ++ functions, note that they must be exactly the same as the jni_javacallc_test.h function declaration.
We will study the complete code for C implementation and C ++ implementation, and then discuss the differences between the two.
C function implementation
The following is jni_javacallc_test.c, which is written in C:
#include <jni.h>/* Header for class test_JNI_javaCallc_test *//* * Class: test_JNI_javaCallc_test * Method: intMethod * Signature: (I)I */JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intMethod(JNIEnv *env, jobject obj, jint num) { return num * num; } /* * Class: test_JNI_javaCallc_test * Method: booleanMethod * Signature: (Z)Z */JNIEXPORT jboolean JNICALL Java_test_JNI_1javaCallc_1test_booleanMethod (JNIEnv *env, jobject obj, jboolean boolean) { return!boolean;}/* * Class: test_JNI_javaCallc_test * Method: stringMethod * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_test_JNI_1javaCallc_1test_stringMethod (JNIEnv *env, jobject obj, jstring string) {const char *str = (*env)->GetStringUTFChars(env, string, 0);char cap[128];strcpy(cap, str);(*env)->ReleaseStringUTFChars(env, string, str);return (*env)->NewStringUTF(env, strupr(cap));}/* * Class: test_JNI_javaCallc_test * Method: intArrayMethod * Signature: ([I)I */JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intArrayMethod (JNIEnv *env, jobject obj, jintArray array) {inti, sum = 0;jsize len = (*env)->GetArrayLength(env, array);jint*body = (*env)->GetIntArrayElements(env, array, 0);for(i=0; i<len; i++){ sum += body[i];} (*env)->ReleaseIntArrayElements(env, array, body, 0); return sum;}
C ++ function implementation
#include <jni.h>/* Header for class test_JNI_javaCallc_test *//* * Class: test_JNI_javaCallc_test * Method: intMethod * Signature: (I)I */JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intMethod(JNIEnv *env, jobject obj, jint num) { return num * num; } /* * Class: test_JNI_javaCallc_test * Method: booleanMethod * Signature: (Z)Z */JNIEXPORT jboolean JNICALL Java_test_JNI_1javaCallc_1test_booleanMethod (JNIEnv *env, jobject obj, jboolean boolean) { return!boolean;}/* * Class: test_JNI_javaCallc_test * Method: stringMethod * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_test_JNI_1javaCallc_1test_stringMethod (JNIEnv *env, jobject obj, jstring string) {constchar *str = env->GetStringUTFChars(string, 0);char cap[128];strcpy(cap, str);env->ReleaseStringUTFChars(string, str);returnenv->NewStringUTF(strupr(cap));}/* * Class: test_JNI_javaCallc_test * Method: intArrayMethod * Signature: ([I)I */JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intArrayMethod (JNIEnv *env, jobject obj, jintArray array) { int i,sum = 0; jsizelen = env->GetArrayLength(array); jint*body = env->GetIntArrayElements(array, 0); for(i=0; i<len; i++) { sum += body[i]; } env->ReleaseIntArrayElements(array, body, 0); returnsum;}
Comparison of C and C ++ Functions
The only difference is the method used to access the JNI function.
In C, the JNI function call is prefixed by "(* env)->" to retrieve the value referenced by the function pointer.
In C ++, The jnienv class has inline member functions that process function pointer lookup.
The following describes the minor differences. The two lines of code access the same function, but each language has its own syntax.
C Syntax: jsize Len = (* env)-> getarraylength (ENV, array );
C ++ Syntax: jsize Len = env-> getarraylength (array );