JNI从DWord返回值
JNI Returning value from a DWord
我在头文件
中有一个带有此签名的函数SIMAPI_DECL DWORD WINAPI SimReadDwordBuffer (DWORD* pBuffer,
DWORD dwDwordsToRead,
DWORD* pdwDwordsRead,
DWORD dwBlockDwords,
DWORD dwNoWait);
With the following native call defined
protected native int SimReadDwordBuffer (int[] pBuffer,
int dwDwordsToRead,
int pdwDwordsRead,
int dwBlockDwords,
int dwNoWait);
我使用java .exe创建jni头文件,它看起来像这样
protected native int SimReadDwordBuffer (int[] pBuffer,
int dwDwordsToRead,
int pdwDwordsRead,
int dwBlockDwords,
int dwNoWait);
实现是这样的
JNIEXPORT jint JNICALL Java_com_sig_ccm_CcmBase_SimReadDwordBuffer
(JNIEnv *env, jobject obj, jintArray pBuffer, jint dwWordsToRead,
jint pdwWordsRead,
jint dwBlockWords, jint dwNoWait){
jint *body = env->GetIntArrayElements(pBuffer, 0);
//DWORD foo = 0;
jint value = SimReadDwordBuffer((unsigned long int *)body,
dwWordsToRead,
//&foo,
(unsigned long int *)&pdwWordsRead,
dwBlockWords,
dwNoWait );
//cout << foo;
env->ReleaseIntArrayElements(pBuffer, body, 0);
return value;
}
问题是无论我尝试了什么,我都无法将值pdwWordsRead复制到我从java传递到jni的参数中。如果我使用一个局部变量,我可以把值写出来,这样c++函数就会把它传回来。如有任何建议,我将不胜感激。
Java通过值传递原语,而不是通过引用。此外,像jint的地址并将它们传递给population的函数也不是一个好主意。
JNIEXPORT jint JNICALL Java_com_sig_ccm_CcmBase_SimReadDwordBuffer
(JNIEnv *env, jobject obj, jintArray pBuffer, jint dwWordsToRead,
jint pdwWordsRead,
jint dwBlockWords, jint dwNoWait){
jint *body = env->GetIntArrayElements(pBuffer, 0);
// I would consider copying the jint[] to a dword[] here...
// I would also look into jlong instead of jint because jint
// is 32-bit signed, and dword is 32-bit unsigned. Then do
// something like dword == jlong & 0xFFFFFFFF
DWORD foo = 0;
jint value = SimReadDwordBuffer((DWORD *) body, // Potentially Bad!
(DWORD) dwWordsToRead,
&foo,
//(DWORD *) &pdwWordsRead, Bad!
dwBlockWords,
dwNoWait );
// You need to return an object or something else to get both values out!!!
env->ReleaseIntArrayElements(pBuffer, body, 0);
return value;
}
再次,我不知道什么值是,但如果它是一个成功的指标,检查它,并抛出一个异常,如果发生失败,并返回foo变量代替。
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 如何从 std::thread 返回值
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 方法错误"not all control paths return a value"和方法不返回值
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 对fread的返回值感到困惑
- 程序不向函数返回值
- 如何在另一个函数中使用返回值作为参数?
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 使用 std::p air 进行返回值优化
- JNI从DWord返回值