How do I pass an array of primitives to a native method, like a char[]? And how do I define/access it from the C/C++ side?
Created May 4, 2012
When you get passed a character array, your native method sees a jobject as an argument. You use that jobject as input to the primitive array manipulation methods GetCharArrayRegion and SetCharArrayRegion. You can query the length of the array through GetArrayLength.
This is one of the areas where JNI is pretty messy, especially once you go to arrays of arrays or arrays of objects.
//our native method declaration abbreviated
void _cmj_Java_My_method( ..., jcpp_char_array & arr )
{
for( int i; i<arr.length; i++ )
arr[ i ] = 'x'
}
You can find out more about JunC++ion at http://www.codemesh.com. It is generally available on Wintel but ports to various Unixes are underway.