【发布时间】:2017-11-08 08:56:33
【问题描述】:
首先,我想说旧的答案现在不起作用(例如,下面答案中的 GetDirectBufferAddress 函数现在需要一个参数),如下所示:
JNI - native method with ByteBuffer parameter
这里,
how to write and read from bytebuffer passing from java to jni
如果有人帮忙会更好..
因此,我无法使用 JNI 从 Java 将填充了一些元素的 ByteBuffer 正确发送到 C,并且我无法再次将该 ByteBuffer 的元素返回给 C
我的原生函数声明:
public native int myfunc(ByteBuffer pkt);
为其分配
private ByteBuffer pkt = ByteBuffer.allocate(1000);
我这样称呼它:
System.out.println(myfunc(pkt)); // Doesn't works, throws exception
pkt.position(0);
System.out.println(pkt.get()); // works, when I do comment line above .println
我的 C 代码如下:
JNIEXPORT jint JNICALL Java_xxx_myfunc(JNIEnv *, jobject, jobject); // header
JNIEXPORT jint JNICALL Java_xxx_myfunc(JNIEnv *env, jobject obj, jobject pkt) // function
{
jbyte *buff = (jbyte *) env->GetDirectBufferAddress(pkt);
// buff[0] = 0; I've also tried in this way
return buff[0];
//return 1; if I return 1, it returns correctly
}
当我运行 java 程序时,它会抛出异常。如何从 C 中返回填充的 ByteBuffer 值?
编辑
A fatal error has been detected by the Java Runtime Environment:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x719c16b8, pid=1096, tid=1900
【问题讨论】:
-
你能发布堆栈跟踪吗?
-
@Emax 我正在使用不同的电脑进行编码,但将问题编辑为跟踪的开始
-
好的,但是你在哪里分配
pkt?您的问题是缺少minimal reproducible example。顺便说一句,所有示例都表明您必须发送env作为第一个参数env->GetDirectBufferAddress(env, buf)编译时是否有警告? -
@Stargateur 抱歉我忘记了,但我现在编辑了分配行。正如我在问题中指定的那样,我无法为 GetDirectBufferAddress 函数输入两个参数。而且我不确定 buf 是什么。 buf 不是我的 pkt 参数吗?不,没有警告。我认为,如果我的函数出现问题,我无法向 Java 返回 1 值或无法打印 ByteBuffer 的第一个参数
-
如果您可以通过逐个访问值来生存,请看这里:github.com/mkowsiak/jnicookbook/tree/master/recipeNo040
标签: java c java-native-interface native bytebuffer