Java使用JNA调用C++动态库dll,并得到C++传过来的对象数组值(超级详细)

本文环境:visual studio 2015、idea 2020.3.1、jdk-11.0.12_windows-x64
*注:本文档默认使用者所有环境均安装及配置好。

1、在idea创建一个java项目,项目创建好了后,在src文件夹下创建package,然后再package下创建class。
以我的为例,项目名为IdeaDemo,包名为demo,类名为demoClass。在这里插入图片描述
2、在类中加入以下这段代码,加载C++的动态库dll。JNAideaDll为我的dll。

public interface JnaLibrary extends Library{
	demoClass.JnaLibrary INSTANCE=Native.load("JNAideaDll",demoClass.JnaLibrary .class);
}

直接写入这段代码会报错,引入必要的东西,如下:

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

3、在类中写入一个main函数。

public static void main(String[] args){
   
}

4、在第2步的函数中定义一个方法,一会儿由C++那边进行具体的实现,以打印一句hello world为例。printHello为我定义的方法名。

public interface JnaLibrary extends Library{
    demoClass.JnaLibrary INSTANCE = Native.load("JNAideaDll",demoClass.JnaLibrary .class);
    void printHello();
}

5、接下再在Java中写一个方法,接收C++的实现。

public static void PrintHello(){
    demoClass.JnaLibrary .INSTANCE.printHello();
}

6、把Java中的PrintHello放入main函数中。

public static void main(String[] args){
    PrintHello();
}

7、至此,Java这边的工作已完成,接下来做C++这边的工作。

8、创建一个C++项目,步骤为新建项目——Visual C++——Win32 项目——填入项目名称和路径——下一步——应用程序类型选“DLL”——完成
在这里插入图片描述
9、.cpp文件创建项目时自动添加了,我们需要自行在头文件中添加.h文件,还要在源文件中添加一个.def文件(必须)。

10、在.def文件中添加如下代码,printHello为Java那边定义的需要C++进行实现的方法,需要在方法名后加上@和数字(按顺序)。

LIBRARY JNA
EXPORTS
  printHello @1

11、头文件.h中写入以下代码。

void printHello();

12、原文件.cpp中写上printHello的具体实现。

void printHello() {
    printf("C++ say Hello World");
}

13、VS选择release模式x64,然后进行编译。
在这里插入图片描述
14、找到编译生成的dll(我的dll在JNAideaDll\x64\Release文件夹下),复制,粘贴到Java项目的src文件夹下。

15、在idea中运行刚刚的Java项目,即可看到控制台输出一句“C++ say Hello World”。
在这里插入图片描述
16、至此,一个简单的利用JNA实现Java调用C++的dll已经实现了。

17、以此类推,我们再利用JNA实现Java调用C++传过来的对象数组,就不再赘述,直接附代码了。
Java代码:

package demo;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import lombok.Data;

public class demoClass {
    public interface JnaLibrary extends Library{
        demoClass.JnaLibrary INSTANCE = Native.load("JNAideaDll",demoClass.JnaLibrary.class);
        void printHello();
        void getStudentList(Student[] list);

        //定义一个学生类
        @Data
        @Structure.FieldOrder({"ID","Name"})
        public static class Student extends Structure{
            public int ID;
            public String Name;
            public Student(int ID,String Name){
                this.ID=ID;
                this.Name=Name;
            }
            public Student(){

            }
        }
    }
    public static void main(String[] args){
        System.out.println("Java say Hello world");
        PrintHello();
        GetStudentList();
    }

    public static void GetStudentList(){
        JnaLibrary.Student[] list=new JnaLibrary.Student[2];
        JnaLibrary.INSTANCE.getStudentList(list);
        for (int i=0;i<list.length;i++){
            System.out.println("学生账号:"+list[i].ID+",名字:"+list[i].Name);
        }
    }

    public static void PrintHello(){
        demoClass.JnaLibrary.INSTANCE.printHello();
    }
}

C++中.def文件:

getStudentList @2

C++中.h文件:

#include <qtcore/qstring.h>
#include <qtcore/qlist.h>

//此行目的是为了输出中文
#pragma execution_character_set("utf-8")

    struct Student {
        int ID;
        char* Name;
    };
    void printHello();

    void getStudentList(Student list[]);

C++中.cpp文件:(为了方便,我的C++项目中使用了QT的东西,如下的QString、QByteArray等)

void getStudentList(Student list[]) {
    for (int i = 0; i < 2; i++)
    {
        list[i].ID = i+1;
        QString name = "用户"+QString::number(i);
        QByteArray arr = name.toUtf8();
        list[i].Name = arr.data();//此行和上两行的目的是为了传输中文,否则乱码。并且要在头文件中设置一句#pragma execution_character_set("utf-8")!
    }
}

18、运行Java项目即可看到控制台输出C++传过去的对象数组值。
在这里插入图片描述

最后我有一个问题,希望有大哥可以解答一下。
我想在Java接收C++传过来的对象数组,但是必须要在Java中new一个对象数组并且指定长度。有没有方法可以不需要指定长度?有没有方法我在C++处new,然后C++传过来几条数据Java就接收到几条,有没有合适的方法呢?
希望有大哥能给小弟解答解答!

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值