创建C++接口
创建项目,创建动态连库
image.png
修改 源文件/pch.cpp
#include "pch.h"
#include <stdio.h>
int sayHello(int a,int b) {
printf("Hello World!");
return a+b;
}
修改 头文件/pch.h
#include <stdio.h>
extern "C" _declspec(dllexport) int sayHello(int a,int b);
image.png
Java调用
pom添加jna依赖
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.5.0</version>
</dependency>
创建CLibrary
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface CLibrary extends Library {
//加载dll链接库
CLibrary INSTANTCE = (CLibrary) Native.loadLibrary("D:\\develop\\git\\CPlus\\Dll1\\x64\\Debug\\Dll1.dll", CLibrary.class);
//此方法为链接库中的方法
int sayHello(int a,int b);
}
创建测试类
public class Main {
public static void main(String[] args) {
int result = CLibrary.INSTANTCE.sayHello(2,3);
System.out.println("sayHello的放回结果:"+result);
}
}
运行结果
sayHello的放回结果:5
Hello World!
网友评论