JNI:can';是否将jstring转换为LPCTSTR? C++的一面看起来是这样的: JNIEXPORT jint JNICALL Java_Myclass_showMessage (JNIEnv* env, jobject obj, jstring title, jstring message, jint type) { const char* _title = env->GetStringUTFChars(title, 0); const char* _message = env->GetStringUTFChars(message, 0); const int result = MessageBox(NULL, (LPCTSTR) _message, (LPCTSTR) _title, type); env->ReleaseStringUTFChars(title, _title); env->ReleaseStringUTFChars(message, _message); return result; }
java方面:JNI:can';是否将jstring转换为LPCTSTR? C++的一面看起来是这样的: JNIEXPORT jint JNICALL Java_Myclass_showMessage (JNIEnv* env, jobject obj, jstring title, jstring message, jint type) { const char* _title = env->GetStringUTFChars(title, 0); const char* _message = env->GetStringUTFChars(message, 0); const int result = MessageBox(NULL, (LPCTSTR) _message, (LPCTSTR) _title, type); env->ReleaseStringUTFChars(title, _title); env->ReleaseStringUTFChars(message, _message); return result; },java,c++,java-native-interface,Java,C++,Java Native Interface,java方面: int result = showMessage("caption", "Hello!", 0); 但是,当我从java调用导出函数时,我得到的是: 知道是什么原因导致了这种情况吗?使用MessageBox A-MessageBox会根据您的项目Unicode/mbcs设置自动选择wide或mbcs版本 如果你需要转换到LPCTSTR,这表明出了问题 或者,使用GetStringChars和MessageBoxW,在这种情况下,您可能需要根据jchar的定义进行强制转换 编辑
int result = showMessage("caption", "Hello!", 0);
但是,当我从java调用导出函数时,我得到的是:
知道是什么原因导致了这种情况吗?使用MessageBox A-MessageBox会根据您的项目Unicode/mbcs设置自动选择wide或mbcs版本 如果你需要转换到LPCTSTR,这表明出了问题 或者,使用GetStringChars和MessageBoxW,在这种情况下,您可能需要根据jchar的定义进行强制转换 编辑:示例代码
JNIEXPORT jint JNICALL Java_Myclass_showMessage (JNIEnv* env, jobject obj, jstring title, jstring message, jint type)
{
const jchar* _title = env->GetStringChars(title, 0);
const jchar* _message = env->GetStringChars(message, 0);
const int result = MessageBox(NULL, (wchar_t *) _message, (wchar_t *) _title, type);
env->ReleaseStringChars(title, _title);
env->ReleaseStringChars(message, _message);
return result;
}
在这种情况下,cast可以工作,因为wchar恰好是16位宽,与jchar相同