码迷,mamicode.com
首页 > 编程语言 > 详细

java JNI 二维数组作为方法参数传递给本地

时间:2017-11-03 14:14:20      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:nal   exp   c++   参数传递   port   nbsp   java   release   jni   

java里面有如下本地定义:

private static native int setFilter(final int fd, final int[][] array_filter);

 

c++中对应的函数如下:

JNIEXPORT jint JNICALL Java_JniSocketCan_setFilter
  (JNIEnv *env, jclass obj, jint fd, jobjectArray array_filter)
{
    struct can_filter *pFilter = NULL;
    jint filter_num = 0;
    jint column_num = 0;
    jint i;
    jintArray filter_tmp;
    jint mem_length = 0;

    filter_num = env->GetArrayLength(array_filter);
    DBG("filter_num = %d\n", filter_num);

    mem_length = filter_num * sizeof(struct can_filter);
    pFilter = (struct can_filter*)malloc(mem_length);
    if (NULL == pFilter) {
     LOG_ERR("memory is not enough, allocate failed\n");
     return -2;
    }

    for (i = 0; i < filter_num; i++) {
     filter_tmp = (jintArray)(env->GetObjectArrayElement(array_filter, i));
     column_num = env->GetArrayLength(filter_tmp);
     
     if (2 != column_num) {
      LOG_ERR("array_filter column != 2, we only need can_id and can_mask\n");
      return -1;
     }

     jint *pColumnData = env->GetIntArrayElements(filter_tmp, 0 );
     pFilter[i].can_id = pColumnData[0];
     pFilter[i].can_mask = pColumnData[1];
     DBG("%d:id=0x%x,mask=0x%x\n", i, pFilter[i].can_id, pFilter[i].can_mask);
     env->ReleaseIntArrayElements(filter_tmp, pColumnData, 0);
    }

    jint ret = setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, pFilter, mem_length);

    free(pFilter);
    return ret;
}

 

java中调用的代码如下:

int[][] can_filter = {
   {0x234,0x7FF},
   {0x235,0x7FF}
  };

  canTest.setFilter(s, can_filter);

java JNI 二维数组作为方法参数传递给本地

标签:nal   exp   c++   参数传递   port   nbsp   java   release   jni   

原文地址:http://www.cnblogs.com/eaggle/p/7777738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!