具体代码:
#define CHARSET_UTF8 "utf8" static jclass jClassString = NULL; void jstringToBuffer(JNIEnv* pEnv, jstring jresult, char** ppBuffer) { if (jClassString == NULL) { jClassString = pEnv->FindClass("java/lang/String"); } jmethodID jmethod = pEnv->GetMethodID(jClassString, "getBytes", "(Ljava/lang/String;)[B"); jbyteArray jarr = (jbyteArray)pEnv->CallObjectMethod(jresult, jmethod, pEnv->NewStringUTF(CHARSET_UTF8)); jsize jlen = pEnv->GetArrayLength(jarr); jbyte* jdata = pEnv->GetByteArrayElements(jarr, JNI_FALSE); if (*ppBuffer == NULL) { *ppBuffer = (char*)malloc((int)jlen+1); memset(*ppBuffer, 0, (int)jlen +1); } strncpy(*ppBuffer, (char*)jdata, (int)jlen); pEnv->ReleaseByteArrayElements(jarr, jdata, 0); }