Java代码:
package library;
import java.nio.ByteBuffer;
public class Library
{
static{System.loadLibrary("TestDLL");}
native static void GetGLBuffer(ByteBuffer Buffer, int Width, int Height, int Size);
public static void main(String[] args) {
ByteBuffer B = ByteBuffer.allocateDirect(5);
byte[] C = {'H', 'E', 'R', 'E', '\0'};
B.put(C);
GetGLBuffer(B, 0, 0, 4);
System.out.println((char)B.get(0));
}
}
C++ 代码:
JNIEXPORT void JNICALL Java_library_Library_GetGLBuffer(JNIEnv *env, jclass cls, jobject buffer, jint Width, jint Height, jint Size)
{
unsigned char* Buff = (unsigned char*)env->GetDirectBufferAddress(buffer);
//*Buff = 'A'; Crashes it.
//Buff[0] = 'A'; Crashes it.
//std::cout<<Buff[0]; Prints fine but crashes when this function ends.
}
如果我什么都不做,它工作得很好。另外,如果我在上面的函数中声明变量,它会崩溃。
我如何解决它?我做错什么了吗?
它打印出这个文件: http: //pastebin.com/Mz76Bk8G