Is it possible to return an array from Java to C ++ using the return value of a method?
I know it is possible to pass an array from Java to C ++ using native methods, but in this case, you need to run the Java code.
I want to do something like this:
public float[] testing2(float[] value1, int[] value2);
Is there a way to address the return type float[]
from C ++ code? And in case it isn't, what's the easiest way to do it, and is it possible without running the code from Java?
CHANGE TO DUPLICATE:
The eventual duplicate is not a duplicate as I am requesting a different way than native methods to access this float[]
, but in other cases native methods are used.
source to share
You can use CallObjectMethod()
.
Precondition: Suppose in C ++ you have
-
JNIEnv* env;
- Global or local reference
obj
that has a Java methodpublic float[] testing2(float[] value1, int[] value2)
as you described -
val1
which is a Java referencefloat[]
-
val2
which is a Java referenceint[]
.
Then
jclass clazz = env->GetObjectClass(obj);
jmethodID testing2meth = env->GetMethodID(clazz, "testing2", "([F[I)[F");
jfloatArray ret = (jfloatArray)env->CallObjectMethod(obj, testing2meth, val1, val2);
Then you can use GetFloatArrayElements()
or GetFloatArrayRegion()
to access ret
.
source to share