In JNI when we want to pass a string from C to java we do it by(C++):
return env->NewStringUTF("MY String");
As this is a return statement, it can be called just once ie. Only 1 string can be passed from C to Java.
What about the function in which we want to pass multiple String from C to Java?!
For example consider the case in which a function wants to pass multiple strings:
void num()
{
for(int i=0;i<10;i++)
{
pass i from C to java..
}
}
How can I do such thing?
Note: I know this can be achieved by using string array or by concatenating i and return the string at last .Also, I know i is a integer, and I know how to convert integer to String in c++.
Consider this is a necessity to pass i as the loop runs though that i(which is passed).
It’ll be good if the function remains to be void.
This can be achieved by
a) calling java methods (call backs) in the for loop (which are essentially setters for the required array).
b) passing jbyte(int) array as argument and filling it in loop.
c) method you mentioned in question – packing the return object(in byte array) and returning it ; unpack it in java side.
option which you are not interested is the best approach and the general approach adopted.
The main emphasis in JNI code is to reduce calls across Java-C (vice versa). Try to revise your options if your are writing non trivial code