Get the pointer of a Java ByteBuffer though JNI

╄→尐↘猪︶ㄣ 提交于 2019-12-18 13:19:51

问题


How can I get a pointer to the inner array of a Java ByteBuffer?

JNIEXPORT void JNICALL test(JNIEnv *env, jobject thiso) {
    jclass cls = env->FindClass("java/nio/ByteBuffer");
    jmethodID aloc = env->GetStaticMethodID(cls, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
    jobject obj = env->CallStaticObjectMethod(cls, aloc, 1000);
}

PS: I'm doing that to share the memory used by Java and C++.


回答1:


void * data = env->GetDirectBufferAddress(obj);

The ByteBuffer must be a direct one for this to work.




回答2:


If you're trying to return the address of the first element within m_buffer, then you can just do:

return m_buffer;

..or:

return &m_buffer[0]



来源:https://stackoverflow.com/questions/8000548/get-the-pointer-of-a-java-bytebuffer-though-jni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!