zoukankan      html  css  js  c++  java
  • JNI intArray

    JNIDemo.java
    public class JNIDemo {
        static {         /* 1. load */
            System.loadLibrary("native"); /* libnative.so */
         }
        public native static int[] hello(int[] a);
        public static void main(String[] args) {
             
             System.out.println("JNIDemo");
             int [] a = {1,2,3};
             int [] b = null;
             int i;
             /* 1. load */
             System.loadLibrary("native"); /* libnative.so */
    
             /* 2. map hello java<-->c c_hello */
    
             /* 3. call */
             b = hello(a);
             for(i = 0; i < b.length; i++)
                System.out.println(b[i]);
         } 
    
    }

    native.c

    #include <jni.h>    /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */
    #include <stdio.h>
    #include <stdlib.h>
    
    #if 0
     typedef struct {
        char *name;          /* Java里调用的函数名 */
        char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */
        void *fnPtr;          /* C语言实现的本地函数 */
    } JNINativeMethod;
    #endif
    
    jintArray c_hello(JNIEnv *env, jclass cls, jintArray arr)
    {
        jint *carr;
        jint *oarr;
        jintArray rarr;
    
        jint i, sum = 0, n = 0;
        carr = (*env)->GetIntArrayElements(env, arr, NULL);
        if (carr == NULL) {
            return 0; /* exception occurred */
        }
    
        n = (*env)->GetArrayLength(env, arr);
        oarr = malloc(sizeof(jint) * n);
    
        if(oarr == NULL)
        {
            (*env)->ReleaseIntArrayElements(env, arr, oarr, 0);
            return 0;
        }    
    
        for (i=0; i < n; i++) {
            oarr[i]= carr[n - 1 -i];
        }
    
        (*env)->ReleaseIntArrayElements(env, arr, carr, 0);
        
        /*create jintArray*/
        rarr = (*env)->NewIntArray(env, n);
        if(rarr == NULL)
        {
            return 0;
        }
    
        (*env)->SetIntArrayRegion(env, rarr, 0, n, oarr);
        free(oarr);
        
        return rarr;
    }
    
    static const JNINativeMethod methods[] = {
        {"hello", "([I)[I", (void *)c_hello},
    };
    
    /* System.loadLibrary */
    JNIEXPORT jint JNICALL
    JNI_OnLoad(JavaVM *jvm, void *reserved)
    {
        JNIEnv *env;
        jclass cls;
    
        if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
            return JNI_ERR; /* JNI version not supported */
        }
        cls = (*env)->FindClass(env, "JNIDemo");
        if (cls == NULL) {
            return JNI_ERR;
        }
    
        /* 2. map hello java<-->c c_hello */
        if((*env)->RegisterNatives(env, cls, methods, 1) < 0)
            return JNI_ERR;
        
        return JNI_VERSION_1_4;
    }
  • 相关阅读:
    openlayers之标注功能四:聚合标注
    openlayers加载百度地图作为底图坐标偏移的解决办法
    设置bootstrap按钮组选中状态
    openlayers F11全屏显示
    8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码
    IDA使用之旅(一)用IDA查看最简单的sys文件
    IDA使用之旅(三)实践中使用IDA工具
    IDA使用之旅(二)工具及窗口的使用
    正则表达式26英文字母大小写互转
    Ollydbg 中断方法
  • 原文地址:https://www.cnblogs.com/CZM-/p/7635510.html
Copyright © 2011-2022 走看看