Java跟C.C++相互调用

//由于诸多误解,我对函数注解说明下,这2个参数谁才是真正的皇帝,谁代替谁了
//首先、这个函数是我自己为说明问题写的,由于和头文件写在一起,故此加入这个宏,
//不然c++编译器报告类型不对,你懂得
//其次,这个函数应该是在JVM库里面实现,所以肯定没有这个宏“__cplusplus”,
//因为JVM不可能每次会根据你是用C调用还是C++,
//还再把自己编译一次是吧,再者JVM是纯C的至于头文件里面为什么有__cplusplus定义
//是因为为了C++调用更自然而采用的,
//此时JVM已经是库了,不会再参与编译,只是在头文件里面加了个C++类型包装从而是操作更加自然,
//比如在C里面是这么写的 int v = (*env)->GetVersion(env); 那是因为没办法只能这么写,
//而C++在头文件里面加了那些 我们就可以这么去写 int v = env->GetVersion();
//再谈这个函数,如果在JVM库里面这个函数应该是去掉 __cplusplus宏的形式,也就是下面的形式
//再问 JNIEnv 类型是什么,现在应该没有人说 是 typedef JNIEnv_ JNIEnv;了吧
//因为编译JVM库时肯定用的是typedef const struct JNINativeInterface_ *JNIEnv;而非上面的宏
//int GetVersion(JNIEnv *env) {
// return (*env)->version;
//}
//既然JNIEnv是const struct JNINativeInterface_ *形式,
//这下应该知道谁才是真包公谁才是假包公了吧? 是“this” 还是“&functions”就一目了然了吧
//是我的错这个函数里面的宏误导了大家,但说也奇怪怎么就没人问JVM代码里会有这个宏吗,
//我想只有 WIN32 WIN64 UNIX32 UNIX64 这些宏吧,当然还有NPTL等等这些

#include <stdio.h>
#include <stdlib.h>

struct JNINativeInterface_;
struct JNIEnv_;

#ifdef __cplusplus
typedef JNIEnv_ JNIEnv;
#else
typedef const struct JNINativeInterface_ *JNIEnv;
#endif

struct JNINativeInterface_ {
    int version;
    int (*GetVersion)(JNIEnv *env);
};

struct JNIEnv_ {
    const struct JNINativeInterface_ *functions;
#ifdef __cplusplus
    int GetVersion() {
        return functions->GetVersion(this);
    }

#endif
};

int GetVersion(JNIEnv *env)
{
#ifdef __cplusplus 
//JVM中代码不走这段,为运行方便而加
    return env->functions->version;
#else
    return (*env)->version;
#endif
}

struct JNINativeInterface_ g_env = {100, GetVersion};

void JNI_CreateJavaVM(void **penv)
{
#ifdef __cplusplus
//同上 JVM中不会走这个
    (*(JNIEnv **)penv) = new JNIEnv;
    (*(JNIEnv **)penv)->functions = &g_env;
#else
    static JNIEnv ge = &g_env;
    *(JNIEnv **)penv = &ge;
#endif
}

void JNI_DestroyJavaVM(void **penv)
{
#ifdef __cplusplus
//同上 JVM不会走这个
    delete (*(JNIEnv **)penv);
#else
    *(JNIEnv **)penv = NULL;
#endif
}

int main(int argc, char **argv)
{
    JNIEnv *env;
    JNI_CreateJavaVM((void **)&env);
#ifdef __cplusplus
    int v = env->GetVersion();
#else
    int v = (*env)->GetVersion(env);
#endif
    printf("version=%d
", v);
    JNI_DestroyJavaVM((void **)&env);
    return 0;
}
原文地址:https://www.cnblogs.com/xuejianhui/p/3501663.html