jni有关知识点总结

一、c/c++语言:
1.关于动态链接库的搜索问题:
LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that need to be linked to your program.
LD_LIBRARY_PATH is used by your program to search for directories containing the libraries after it has been successfully compiled and linked.
 
二、java中的数据类型与c语言中的数据类型转化问题
1.java中的long类型为8个字节而c语言中的long类型为4个字节,所以应当在java中使用int来对应c语言中的long或者unsigned long类型
下面给一个简单示例:
 1 JNIEXPORT jint JNICALL example
 2   (JNIEnv * env, jclass obj, jstring address, jshort port, 
 3   jstring username, jstring password, jlong channel, 
 4   jintArray start_time, jintArray end_time, jstring path){
 5 
 6       const char* addr = env->GetStringUTFChars(address, NULL);
 7       const char* uname = env->GetStringUTFChars(username, NULL);
 8       const char* pwd = env->GetStringUTFChars(password, NULL);
 9       char* p = (char*)env->GetStringUTFChars(path, NULL);
10       jint* start = env->GetIntArrayElements(start_time, NULL);
11       jint* end = env->GetIntArrayElements(end_time, NULL);
12       int code = download(addr, (WORD)port, uname, pwd, (DWORD)channel, (DWORD*)start, (DWORD*)end, p);
13         env->ReleaseStringUTFChars(address, addr);
14         env->ReleaseStringUTFChars(username, uname);
15         env->ReleaseStringUTFChars(password, pwd);
16         env->ReleaseStringUTFChars(path, p);
17         env->ReleaseIntArrayElements(start_time, start, 0);
18         env->ReleaseIntArrayElements(end_time, end, 0);
19 
20         return code;
21   }

上例中的DWORD就是unsigned long的别名。我们使用的是jintArray.

三、jni加载动态库在windows和linux平台上的不同规则

对于System.loadLibrary("nvr");在windows上加载的是nvr.dll文件而在linux上加载的是libnvr.so文件

四、对于jni方法中返回值为jintArray类型的处理方法

// 新建一个数组
jintArray arr = env->NewIntArray(2);
// 获取数组第一个元素的地址
jint* el = env->GetIntArrayElements(arr, NULL);
// 给数组中的元素赋值
*el = result[0];
*(el+1) = result[1];
最后把数组返回即可return arr;

 

 

posted @ 2019-09-01 21:16  CLAYJJ  阅读(320)  评论(0编辑  收藏  举报