JNI:如何传递“unsigned char *”从C ++到java [英] JNI: How to pass "unsigned char* " from C++ to java
问题描述
我有一个'unsigned char *',我希望使用JNI
将其传递给Java代码我已经按照以下方式尝试了它
I have a 'unsigned char *' and I want to pass it on to the Java code using JNI I have tried it in the following way
jstring test1;
std::string str(reinterpret_cast<const char*>(ucptest));
test1 = env->NewStringUTF(str.c_str());
其中'ucptest'是'unsigned char *'并且其中包含ascii值。
此代码有效,我可以成功将jstring传递给Java代码,但在加载调用中我看到JVM崩溃。
where 'ucptest' is 'unsigned char *' and has ascii values in it. this code works and I can successfully pass jstring to Java code, but in the load call I am seeing crash in JVM.
可能的原因是什么?为了崩溃?崩溃是不一致的,这可能意味着内存损坏。
What could be the possible reason for the crash? crash is inconsistence, which could mean memory corruption.
有人可以提出一种更好的方法将'unsigned char *'传递给Java吗?
Can someone suggest a better way to pass 'unsigned char*' to Java?
感谢任何帮助。
谢谢
推荐答案
<我找到了解决方案,并认为我会分享相同的内容,以便其他人可以使用它。
I found the solution, and thought I would share the same, so that others could use it.
最初我转换 char *
到C ++ String,然后将其传递给Java String
。这工作得很好,直到我试图转换的字符串中有0。这导致崩溃。
Initially I was converting char*
to C++ String and then passing it to Java String
. This was working just fine, until we had "0" in the string I was trying to convert. This was causing a crash.
最后,我使用字节数组将值传递给服务器。这是完美的。
Finally, I used a byte array to pass on the value to server. This is working perfectly.
jbyteArray jbArray = env->NewByteArray((int)call.len);
env->SetByteArrayRegion(jbArray, 0, (int)call.len, (jbyte*)call.data);
这篇关于JNI:如何传递“unsigned char *”从C ++到java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!