1 回答
TA贡献1856条经验 获得超11个赞
将 const unsigned char* 转换为 jbyteArray:
#include <tuple>
JNIEXPORT jbyteArray JNICALL Java_MyJavaApplication_readValue(JNIEnv* env, jobject obj)
{
const unsigned char* someConstUnsignedChar;
int len;
std::tie(someConstUnsignedChar, len) = (gcnew MyCSharpLibrary())->readValue();
jbyteArray someJbyteArray = env->NewByteArray((jsize)len);
env->SetByteArrayRegion(someJbyteArray, 0, len, reinterpret_cast<const jbyte*>(someConstUnsignedChar));
return someJbyteArray;
}
将 jbyteArray 转换为 const unsigned char*:
JNIEXPORT jboolean JNICALL Java_MyJavaApplication_writeValue(JNIEnv* env, jobject obj, jbyteArray someJbyteArray)
{
jsize len = env->GetArrayLength(someJbyteArray);
jbyte* body = env->GetByteArrayElements(someJbyteArray, 0);
unsigned char* someUnsignedChar = new unsigned char[len];
for (jint i = 0; i < len; i++)
{
someUnsignedChar[i] = (unsigned char)body[i];
}
bool result = (gcnew MyCSharpLibrary())->writeValue((const unsigned char*)someUnsignedChar);
env->ReleaseByteArrayElements(someJbyteArray, body, 0);
return (jboolean)result;
}
这个对我有用。
添加回答
举报