Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting a C++ long type to a JNI jlong

I am using JNI to pass data between C++ and Java. I need to pass a 'long' type, and am doing so using something like:

 long myLongVal = 100;
 jlong val = (jlong)myLongVal;
 CallStaticVoidMethod(myClass, "(J)V", (jvalue*)val);

However in Java, when the 'long' parameter is retrieved, it gets retrieved as some very large negative number. What am I doing wrong?

like image 964
lost_bits1110 Avatar asked Sep 24 '11 01:09

lost_bits1110


1 Answers

CallStaticVoidMethod(myClass, "(J)V", (jvalue*)val);

This is undefined behaviour. You are casting an integer to be a pointer. It is not a pointer. You need, at the very least, to pass the address. This code would on most platforms instantly crash.

like image 82
Puppy Avatar answered Oct 10 '22 03:10

Puppy