I have a Java function with the following signature:
char[] getResult()
How do I invoke this method from native C code and convert the return value to an array of C chars? JNI provides a CallCharMethod function but does not appear to provide CallCharArrayMethod or such. Do I have to wrap the output of getResult in a String and use CallObjectMethod?
This is complicated by the fact that the Java char type and the C char type, despite having the same name, are very different.
The Java char type represents a sixteen-bit Unicode character. C’s char is also meant to represent a character, but since C predates Unicode this was taken as a byte, and I’m not aware of any C implementations where char is more than an 8-bit byte (although the standard doesn’t prohibit this).
So you may have some encoding issues to deal with as far as converting an array of Java chars to an array of C chars goes. All the more complicated if you have Unicode characters that don’t fit in sixteen bits.
But as for calling the method, JNI provides functions to call methods returning primitive types and a method for Objects, and in Java, arrays are Objects, so you’d want to use
CallObjectMethod.