开发者

How to return an array in JNI?

I have a native function I am accessing via JNI that needs to return the contents of an array to java. My function prototype is as follows:

JNIEXPORT jcharArray JNICALL Java_jniusb_Main_receiveData
  (JNIEnv *, jclass, jchar);

which was generated with javah.exe.

So in the function's code I have an array 'unsigned char InputPacketBuffer[65]' which i want to return to java. However, I am having 开发者_StackOverflow社区problems mapping this to my return type 'jcharArray'.

In another function I used the 'GetCharArrayRegion' method provided by JNI to convert an input parameter of type 'jcharArray' to a 'jchar' array which i could then typecast to an 'unsigned char' array. Basically, I need to do the opposite of this to convert in the other direction, but I haven't been able to find an appropriate JNI method in the JNI specification pdf. Anyone know how to do this?

UPDATE:

I found the correct JNI function on andy's link - SetCharArrayRegion().

fyi - the "The Java Native Interface - programmer's guide and specification" gives incorrect examples for using their functions.

i.e.

(*env)->SetCharArrayRegion(env, elemArr, 0, len, chars); 

doesn't compile. Instead, the proper syntax is:

(*env).SetCharArrayRegion(elemArr, 0, len, chars);


See the JNI documentation on array operations. The counterpart to GetCharArrayRegion is SetCharArrayRegion.

A jchar is a short, not a char. Java supports Unicode characters. If you want an array of bytes, you can use jbytearray.

An alternative is to use JNI string operations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜