java使用jna调用dll

1.先放代码
c++生成dll代码:

// Testdll.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
extern "C" __declspec(dllexport) int add(int a, int b);

int add(int a, int b) {
	return a + b;
}

java代码:

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

public class Test {
	
	public interface CLibrary extends Library {
		CLibrary INSTANCE = (CLibrary)Native.loadLibrary("E:/c-works/Testdll/x64/Debug/Testdll", CLibrary.class);
		int add(int a, int b); 
	}
	
	public static void main(String[] args) {
		int sum = CLibrary.INSTANCE.add(3, 5);
		System.out.println(sum);
	}
}

注意:路径可以使根路径,或者是绝对路径
添加jar:

<dependency>
    <groupId>com.sun.jna</groupId>
    <artifactId>jna</artifactId>
    <version>自己定</version>
</dependency>

猜你喜欢

转载自blog.csdn.net/weixin_43155301/article/details/85046938