jna

作者: hehehehe | 来源:发表于2020-08-14 16:18 被阅读0次

cpp

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass {
private:
    int m_nb;
public:
    MyClass(int nb);
    int getValue() const;
    void increment();
};
#endif

#include "cpplib/MyClass.h"

MyClass::MyClass(int nb)
  : m_nb(nb)
{
}

int MyClass::getValue() const
{
    return m_nb;
}
void MyClass::increment()
{
    m_nb++;
}

jna

package edu.nyu.cpptest.cpplib;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

public class MyClass {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)Native.loadLibrary("cpplib_shared",
                CLibrary.class);
        Pointer MyClass_ctor(int nb);
        int MyClass_getValue(Pointer self);
        void MyClass_increment(Pointer self);
    }

    private Pointer self;
    public MyClass(int nb) {
        self = CLibrary.INSTANCE.MyClass_ctor(nb);
    }
    public int getValue(){
        return CLibrary.INSTANCE.MyClass_getValue(self);
    }
    public void increment(){
        CLibrary.INSTANCE.MyClass_increment(self);
    }
}

#include "cpplib/MyClass.h"

#ifdef _WIN32
#   define DLL_EXPORT __declspec(dllexport)
#else
#   define DLL_EXPORT
#endif

extern "C" DLL_EXPORT MyClass *MyClass_ctor(int nb);

MyClass *MyClass_ctor(int nb)
{
    return new MyClass(nb);
}

extern "C" DLL_EXPORT int MyClass_getValue(const MyClass *self)
{
    return self->getValue();
}

extern "C" DLL_EXPORT void MyClass_increment(MyClass *self)
{
    self->increment();
}

jni

package edu.nyu.cpptest.cpplib;

public class MyClass {

    static {
        System.loadLibrary("cpplib_jni");
    }

    private long self_ptr;

    public MyClass(int nb)
    {
        init(nb);
    }

    private native void init(int nb);

    public native int getValue();

    public native void increment();

    protected native void finalize();

}

#include "cpplib/MyClass.h"
#include "MyClass_jni.h"

static jfieldID _get_self_id(JNIEnv *env, jobject thisObj)
{
    static int init = 0;
    static jfieldID fidSelfPtr;
    if(!init)
    {
        jclass thisClass = env->GetObjectClass(thisObj);
        fidSelfPtr = env->GetFieldID(thisClass, "self_ptr", "J");
    }
    return fidSelfPtr;
}

static MyClass *_get_self(JNIEnv *env, jobject thisObj)
{
    jlong selfPtr = env->GetLongField(thisObj, _get_self_id(env, thisObj));
    return *(MyClass**)&selfPtr;
}

static void _set_self(JNIEnv *env, jobject thisObj, MyClass *self)
{
    jlong selfPtr = *(jlong*)&self;
    env->SetLongField(thisObj, _get_self_id(env, thisObj), selfPtr);
}


JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_init(JNIEnv *env, jobject thisObj, jint nb)
{
    MyClass *self = new MyClass(nb);
    _set_self(env, thisObj, self);
}

JNIEXPORT jint JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_getValue(JNIEnv *env, jobject thisObj)
{
    MyClass *self = _get_self(env, thisObj);
    return self->getValue();
}

JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_increment(JNIEnv *env, jobject thisObj)
{
    MyClass *self = _get_self(env, thisObj);
    self->increment();
}

JNIEXPORT void JNICALL Java_edu_nyu_cpptest_cpplib_MyClass_finalize(JNIEnv *env, jobject thisObj)
{
    MyClass *self = _get_self(env, thisObj);
    if(self != NULL)
    {
        delete self;
        _set_self(env, thisObj, NULL);
    }
}

相关文章

  • Java使用JNA方式调用DLL(动态链接库)(原创,装载请注明

    Java使用JNA调用DLL 1.准备 1.JDK环境2.Eclipse3.JNA包下载JNA包:(1).JNA的...

  • JNA从入门到精通-02入门

    JNA入门 添加JNA的依赖 原始的c代码

  • 2019-04-04 springboot实现Excel导入

    添加依赖 net.java.dev.jna jna-platform

  • jna

    cpp jna jni

  • jna

    JNA Java Natice Access ,提供一组Java工具类访问系统本地库。开发人员只需在java接口中...

  • jna

    jna遇到一个问题,c库参数为long,jna声明和调用也是long,然后调用之后,c始终收到的是0。 后面分析是...

  • Jna

    编写JNA封装c接口给JAVA调用最重要是参数传递,JNA只认纯c类型,对于c++的stl类型是不认识的,并且需要...

  • jna

    jna:java native access,是一个建立在经典的JNI技术之上的Java开源框架。JNA提供一组J...

  • JNA

    JNA(Java Native Access)是建立在JNI(Java Native Interface,Java...

  • JNA介绍以及简单使用

    JNA介绍 JNA(Java Native Access )提供一组Java工具类用于在运行期动态访问系统本地库(...

网友评论

      本文标题:jna

      本文链接:https://www.haomeiwen.com/subject/jdagdktx.html