JNA 调用Dll实例

工作需要,调用别人写的Dll来实现一些功能,去网上找了一些资料,发现都无法实现,会报错,找不到dll或者无法loadDll,调查修改后,实现了一个小demo,记录下来(本人记性不好。。。。)

先来使用VS2010写了一个简单的C++代码:

#include <windows.h> 
#include <iostream>
using std::cin;
using std::cout;
using std::endl; 

#define MYLIBAPI extern "C" __declspec( dllexport ) 

MYLIBAPI int add(int a,int b);

int add(int a,int b){  
	cout << "Hello, fei!   I`m C++!" << endl;
    return a + b;  
}

C++代码部分注意:

本人是64位windows7机器,生成的debug版本时不好用,用java调用的时候会提示,无法load dll

所以此处要注意生成release版本的64位DLL

 

java代码部分:

package testDll;

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


public interface testDll extends Library{
	testDll HUPF = 
			(testDll)Native.loadLibrary("hupf.dll",
					testDll.class);
	
	public int add(int a , int b);
	public int main();
}
package testDll;

public class Main {

	public static void main(String[] args) {
		System.out.println(testDll.HUPF.add(10, 100));
	}
	
}

这样才能成功调用到DLL,执行结果:

Hello, fei!   I`m C++!
110

找度娘看资料发现Java调用dll目前有两种方式,一个是JNI,一个是JNA,都说JNI不好用,我也没有详细去了解,当前例子是用的JNA,本人新手,写的不对,欢迎大家一起来探讨,谢谢 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值