I asked a similar question about char to jsstring
... but now I have a problem with an int to jlongArray
, which I just can't figure out :/
I get the following error:
Error:(290, 10) error: cannot initialize a variable of type 'int *' with an rvalue of type 'jlong *' (aka 'long long *')
on this line:
JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_LoadPlayerA(JNIEnv *javaEnvironment, jobject self, jstring audioPath, jlongArray offsetAndLength) {
const char* audio = javaEnvironment->GetStringUTFChars(audioPath, JNI_FALSE);
int* params = javaEnvironment->GetLongArrayElements(offsetAndLength, JNI_FALSE);
example->LoadPlayerA(audio, params);
}
this is the declaration:
void LoadPlayerA(const char *audioPath, int *params);
can someone help me out?
long
in Java is a signed 64-bit integer type. A C++ int
on the other hand is only defined as being "guaranteed to have a width of at least 16 bits".
Lets's assume that your int
s are 32-bit (that's a very common scenario): so now you've got a pointer to a bunch of data where each element is 64 bits, and you're trying to pass that to a function that expects a pointer to 32-bit data. That's obviously not going to work, even if the compiler had allowed it.
Some options for you:
- Change
LoadPlayerA
to take ajlong*
orint64_t*
(and change any code inLoadPlayerA
that relies on the incoming data being of typeint
). - Change the Java code to pass the data as an
int[]
instead of along[]
, and changeLoadPlayerA
to take ajint*
orint32_t*
. - Allocate an array/vector of type
int
in your JNI function and convert the data fromjlong
toint
before passing it toLoadPlayerA
.