Converting char *tab[10] to java object (JNI)
I am trying to use a C API in java using JNI. I'm not a C programmer...
Here is the C "object" I have to convert : char *tab[10]
First, I'm not sure what it means 开发者_运维问答: is it a pointer to a tab of char, or a tab of pointers to char ? (and is there any difference ?).
Then, what would the equivalent java object ?
char * tab[10] is pointer to an array of chars (points on first element).
Here is JNI API: http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html
Type equivalent for "C char" in JNI is jchar - in java it is "char" primitive type; Possibilities of What you can do having array of "C chars" are: copy it into existand String object in java OR into java primitive char array. Usefull link: http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html#array_operations
The 2nd way is may looks like that:
you pass a java char array into JNI call or create new java array from C code with New<PrimitiveType>Array function. However you will end with pointer to array in java. They copy your C chars into java's one:
jEnv->ReleaseCharArrayElements(javaCharArray, C_CharArray, JNI_COMMIT);
where jEnv - is java enviroment, passing through JNI call. In case u have a pointer u may need to dereference it like *C_CharArray. I think thats may work out.
It's (most likely) an array of 10 strings, and so would be modeled directly as:
String[] tab = new String[10];
It's also possible that it's an array of character buffers, so you might use a StringBuilder
instead of a String
. And remember, Java arrays are objects in their own right and know their size so that information is not attached to the type (unlike in C, where array sizes are type characteristics).
I'd not expect to have direct transfer of the type/value through JNI; you'll need some conversion glue code in there.
精彩评论