Java code:
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++ Code:
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.
}
If I do nothing, it works perfectly fine. Also, if I declare variables within the function above, it crashes.
How do I fix it? Am I doing anything wrong?
It prints out this file: http://pastebin.com/Mz76Bk8G
I just want to say that I solved it. I had to go into my JNI folder, copy the JNI.h and the JNI_MD.h for JDK 7 to my C++ project folder, include it as a file and also use a .def file to export.
Without doing all of the above steps, it crashed the JVM. Now I’m not sure why but I’m just glad it works. I’m posting this to let others who have the same problem fix it.
Thanx for the views guys. Was quite difficult to figure out what was wrong. Hope this comment helps others.