代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Class:     com_ema_testlib_infrastructure_demo_JNITools
 * Method:    uartTest
 * Signature: ([Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_ema_testlib_infrastructure_demo_JNITools_uartTest
        (JNIEnv *env, jobject, jobjectArray jargv) {

    int argc = env->GetArrayLength(jargv);
    typedef char *pchar;
    pchar *argv = new pchar[argc];
    int i;
    for(i=0; i<argc; i++)
    {
        jobject x= env->GetObjectArrayElement(jargv, i); //A Java string
        jstring js = (jstring) x; //A Java string
        const char *pjc = env->GetStringUTFChars(js,0); //A pointer to a Java-managed char buffer
        size_t jslen = strlen(pjc);
        argv[i] = new char[jslen+1]; //Extra char for the terminating null
        strcpy(argv[i], pjc); //Copy to *our* buffer. We could omit that, but IMHO this is cleaner. Also, const correctness.
        env->ReleaseStringUTFChars(js, pjc);
    }

    for (int i = 0; i < argc; ++i) {
        LOGD("%s",argv[i]);
    }

    //Call main
    int result=test_main(argc, argv);

    //Now free the array
    for(i=0;i<argc;i++)
        delete [] argv[i];
    delete [] argv;

    return result;
}