Several months ago, I wrote a Java API that use JNI to wrap around a C API. The C API used char strings and I used GetStringUTFChars to create the C strings from the Java Strings.
I neglected to think through the problems that might arise with non-ASCII characters.
Since then the creator of the C API has created wide character equivalents to each of his C functions that require or return wchar_t strings. I would like to update my Java API to use these wide character functions and overcome the issue I have with non-ASCII characters.
Having studied the JNI documentation, I am a little confused by the relative merits of using the GetStringChars or GetStringRegion methods.
I am aware that the size of a wchar_t character varies between Windows and Linux and am not sure of the most efficient way to create the C strings (and convert them back to Java strings afterwards).
This is the code I have at the moment which I think creates a string with two bytes per character:
int len;
jchar *Src;
len = (*env)->GetStringLength(env, jSrc);
printf("Length of jSrc is %d\n", len);
Src = (jchar *)malloc((len + 1)*sizeof(jchar));
(*env)->GetStringRegion(env, jSrc, 0, len, Src);
Src[len] = '\0';
However, this will need modifying when the size of a wchar_t differs from jchar.