The core of Java calling C + + is the conversion of variable types, and the Java int is the same as int in C + +, the conversion is relatively simple. If you think of string in Java, the processing is more complicated.
Jniexport jstring jnicall java_com_dd_lib_chinesetopinyin (jnienv *env, Jclass, jstring src_jstr)
{
Try not to use char *a = new char[100]; Such a declaration may result in various libc.so.6 errors, as much as possible to allow all memory to be reclaimed by the system.
/* Start with new and delete to manage memory, appeared c [libc.so.6+0x7a5db] unsigned long+0xa5b, c [LIBC.SO.6+0X1E9E0] [email protected] @GLIBC_PRI Vate+0x1e9e0 and so on errors, error hints can only be accurate to the function, should be memory errors, so use Java calls C + + to try to let the system to manage memory. */
Char Src_nc[srclen];
try{
Convert Java string to C + + string, where Src_char is a pointer to a byte array in Src_jstr
const char * Src_char = Env->getstringutfchars (Src_jstr, false);
strcpy (Src_nc,src_char);
To release the SRC_JSTR after the string is removed
Env->releasestringutfchars (Src_jstr, Src_char);
} catch (Exception &e) {
cout << e.what () << Endl;
}
.....................
String ret_str = "return value";
String to be converted to Java on return
Jstring rtstr = Env->newstringutf (Ret_str.c_str ());
return rtstr;
}
Java uses JNI