JNI 传字符串参数,只输出了第一个字符

如下边的代码,

运行结果:

Result: H

Java代码:

public class Test {
    public void print(String msg);
    
    static {
        System.loadLibrary("MyJni");
    }

    public static void main(String[] args) {
        Test t = new Test();
        System.out.print("Result: ");
        t.print("Hello, World!");
    }
}

C:

JNIEXPORT void TestJni_print(JNIEnv * env, jobject obj, jstring str) {
    const char * str_buf = (*env)->GetStringChars(env, str, NULL);
    printf("constent is %s", str_buf);
    (*env)->ReleaseStringChars(env, str, str_buf);
}


正确的写法:

JNIEXPORT void JNICALL Java_TestJni_print
  (JNIEnv * env, jobject obj, jstring str) {
    const char * str_buf = (*env)->GetStringUTFChars(env, str, NULL);
    printf("constent is %s", str_buf);
    (*env)->ReleaseStringUTFChars(env, str, str_buf);
}



猜你喜欢

转载自blog.csdn.net/xzx735/article/details/25497183