zoukankan      html  css  js  c++  java
  • JNI 回调小记

    javah在eclipse中设置参数:location(javah.exe的位置)working dir(${project_loc}/src)

    -classpath .;./classes -d ${project_loc}jni -jni ${java_type_name}

    回调的java代码

    public class HelloWorld {
        static {
            System.loadLibrary("jnidemo");
        }
    
        public void myCallbackFunc(String nMsg) {
            Log.v("EagleTag", "back message:" + nMsg);
        }
    
        private void throwException() throws NullPointerException {
            throw new NullPointerException("Null pointer");
        }
    
        public native String DisplayHello(String inputStr);
    }

    C代码

    jstring JNICALL Java_com_example_jnidemo_HelloWorld_DisplayHello(JNIEnv *env,
            jobject obj, jstring what) {
        const jbyte *l_what;
        char *result;
    
        l_what = (*env)->GetStringUTFChars(env, what, NULL);
        if (l_what == NULL) {
            return NULL; /* OutOfMemoryError already thrown */
        }
    
        result = malloc(strlen(l_what) + 6);
        if (result == NULL) {
            return NULL;
        }
        sprintf(result, "中文reiver %s", l_what);
    
        //回调
        char tChar[256];
        gJniClass = 0;
        gJinMethod = 0;
    
        gJniClass = (*env)->GetObjectClass(env, obj);
        if (gJniClass == 0 || gJniClass == NULL)
            return (*env)->NewStringUTF(env, "-1");
    
        gJinMethod = (*env)->GetMethodID(env, gJniClass, "myCallbackFunc",
                "(Ljava/lang/String;)V");
        if (gJinMethod == 0 || gJinMethod == NULL)
            return (*env)->NewStringUTF(env, "-2");
    
        strcpy(tChar, result);
        (*env)->CallVoidMethod(env, obj, gJinMethod,
                (*env)->NewStringUTF(env, tChar));
    
        return (*env)->NewStringUTF(env, result);
    }
  • 相关阅读:
    Java_适配器模式
    linux常用命令整理
    (转)使用隐藏的iframe 隐藏form提交,仿AJax无刷新提交,可以实现无刷新上传文件
    mysql添加并返回主键
    学习RMI
    关于bcprov-jdk16
    JavaScript在页面中的引用方法
    通过CFX发布WebService(一)
    字符串和json数据的转换
    MD5 加密与解密
  • 原文地址:https://www.cnblogs.com/toosuo/p/3835889.html
Copyright © 2011-2022 走看看