Java JNI programming skills in windows -- Java calls C/C ++ (3)

Source: Internet
Author: User

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 );


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.