JNI - How to convert jshortArray to short *

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I have a dll file that contains the following function:

unsigned char * EncodingData(short Data[], int Length); 

I want to call this native method in java. So I have created one wrapper dll that contains the follwing function corresponding to the above function

JNIEXPORT jcharArray JNICALL Java_com_common_FilterWrapper_EncodingData (JNIEnv * env, jclass cls, jshortArray shortData, jint len) 

Now I am calling the above JNI function from java and it is being called successfully.

Problem:

I want to call the dll file's function from wrapper dll's function but I don't know how to convert the following:

  • jshortArray to short [] (to pass the data to the dll file's function which is expecting short [] and not jshortArray)

  • unsigned char to jcharArray (to return the value back to the java function)

Note: The wrapper dll is written in VC++

回答1:

unsigned short tempElem; jshort* test = env->GetShortArrayElements(shortData, NULL); int size = (sizeof(test) / sizeof(*test)) - 1; for (int i = 0; i < size; i++) {     tempElem = (unsigned short)test[i]);     printf("[%d] test %u  || size = %d\n", i, tempElem, size); } 


回答2:

This is covered in the JNI documentation.

To summarize:

First, we provide a set of functions to copy primitive array elements between a segment of a Java array and a native memory buffer. Use these functions if a native method needs access to only a small number of elements in a large array.

Second, programmers can use another set of functions to retrieve a pinned-down version of array elements. Keep in mind that these functions may require the Java VM to perform storage allocation and copying. Whether these functions in fact copy the array depends on the VM implementation



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!