我尝试在“jdouble*”变量中解析从 jni 获得的 double 组。 我想要做的是将该 double 组转换为“double*”数组。
它不起作用并给我这样的结果:
X0 875387640 875387816 959506832 875387864
这是我的代码:
JNIEXPORT void JNICALL Java_MainFrame_Jni_Functions_sendTablesToNative
(JNIEnv *env, jobject obj, jobject paramObj, jobject respObj, jobject objObj, jobject ConObj, jobject intConObj){
jclass jTablObjClass = env->FindClass("MainFrame/Jni/TablesObjects"); // get jclass
//////////////////////////////////////////////////////////////
jobject col, row; // will be used to navigate within the jobjectarrays
//************************************************************
//////////////////////////////////////////////////////////////
jint paramNbRows; // will get the parameter nb rows
jdouble* paramValue; //
jdouble* paramMin; // Define double variables for
jdouble* paramMax; // param : value, min, max, reference
jdouble* paramReference; //
col = env->GetObjectArrayElement((jobjectArray)paramObj, 0);
vector<char*> paramName( env->GetArrayLength((jobjectArray)col) ); // define parameter Name Column size
col = env->GetObjectArrayElement((jobjectArray)paramObj, 5);
vector<char*> paramType( env->GetArrayLength((jobjectArray)col) ); // define parameter Type Column size
//
paramNbRows = env->GetArrayLength((jobjectArray)col); // get parameter nb rows
col = env->GetObjectArrayElement((jobjectArray)paramObj, 1);
paramValue = env->GetDoubleArrayElements((jdoubleArray)col, 0); // get parameter value column
col = env->GetObjectArrayElement((jobjectArray)paramObj, 2);
paramMin = env->GetDoubleArrayElements((jdoubleArray)col, 0); // get parameter min column
col = env->GetObjectArrayElement((jobjectArray)paramObj, 3);
paramMax = env->GetDoubleArrayElements((jdoubleArray)col, 0); // get parameter max column
col = env->GetObjectArrayElement((jobjectArray)paramObj, 4);
paramReference = env->GetDoubleArrayElements((jdoubleArray)col, 0); // get parameter reference column
col = env->GetObjectArrayElement((jobjectArray)paramObj, 0);
for(int i=0; i<(env->GetArrayLength((jobjectArray)col)); i++){
paramName.at(i) = (char*)env->GetStringUTFChars(
(jstring)env->GetObjectArrayElement( (jobjectArray)col, i), 0 ); // get parameter name column
}
col = env->GetObjectArrayElement((jobjectArray)paramObj, 5);
for(int i=0; i<(env->GetArrayLength((jobjectArray)col)); i++){
paramType.at(i) = (char*)env->GetStringUTFChars(
(jstring)env->GetObjectArrayElement( (jobjectArray)col, i), 0 ); // get parameter name column
}
//************************************************************
printf("\n\n %s %d %d %d %d %s",
paramName.at(0), (double)paramValue[0], (double)paramMin[0],
(double)paramMax[0], (double)paramReference[0], paramType.at(0) );
//////////////////////////////////////////////////////////////
jstring str = env->NewStringUTF("Test Jni from C++");
jmethodID printMethId = env->GetStaticMethodID(jTablObjClass, "print", "(Ljava/lang/String;)V");// get jmethod
env->CallStaticVoidMethod(jTablObjClass, printMethId,str); // call jmethod
return;
}
最佳答案
对于初学者:为什么所有类型转换?如果您使用的是 JNI 接口(interface)
正确地,应该不需要任何类型转换。如果我们拿第一,
例如:如果 paramObj
在 native 中被声明为一个数组
接口(interface),传入的类型应该是jobjectArray
,而不是
作业对象
;如果它声明为一个对象,则调用
GetObjectArrayElement
应该不起作用。 (因为 jobject
和 jobjectArray
可能是指向指针类型的类型定义,而
接口(interface)是 extern "C"
,没有参数类型检查,你可能
如果 Java 端的参数实际上是一个数组,就不用管它了
的对象。)如果我是你,我会重新检查你所有的类型。
至于奇怪的输出:你正在匹配一个 double
到一个 "%d"
格式
说明符,它需要一个 int
。未定义的行为。就掉
printf
,并使用std::cout
,问题会自行解决
自动地。 (否则,使用 "%e"
、"%f"
或 "%g"
作为格式
说明符。)
关于java - 将 jdouble* native 变量解析为 double* native 变量 (jni),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8224045/