添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
呐喊的木耳  ·  Spring Boot ...·  1 年前    · 
着急的大象  ·  js使用for ...·  1 年前    · 

二、JNA包下载

java native interface JNA(Java Native Access)框架是一个开源的Java框架,是SUN公司主导开发的,建立在经典的JNI的基础之上的一个框架,比JNI更好用,JNI偏底层,JNA更友好。
JNA下载地址:
https://github.com/java-native-access/jna
找到Download下载第一个就OK
在这里插入图片描述

三、DLL转换

eclipse 为例,右键项目->Properties->Java Build Path->Libraries->Add External JARs 选择 JNA

1、编写charReference类,该类继承 com.sun.jna.ptr.ByReference 类,该类主要负责Java数据类型到C++中char *类型对应

import com.sun.jna.ptr.ByReference;
public class charReference extends ByReference{
	public charReference() {
		this((byte)0);//无
	public charReference(byte value) {
		super(1000);
		this.setValue(value);
	public void setValue(byte value) {
        this.getPointer().setByte(0L, value);
	public byte getValue() {
        return this.getPointer().getByte(0L);
	public void Init(String input) {
		for(int i=0;i<input.length();i++) {
			this.getPointer().setChar((long)i, input.charAt(i));//字符串初始化
	public void Init(byte[] bytes) {
		for(int i=0;i<bytes.length;i++) {
			this.getPointer().setByte((long)i, bytes[i]);//字节数组初始化

因为char可能是中文汉字,中文汉字占两个字节,在setChar时并不会将两个字节都传到对象中,所以有中文时需要先将字符串转化成字节数组再传。

2、声明接口

import com.sun.jna.*;
import com.sun.jna.ptr.PointerByReference;
import tools.charReference;
public interface PORT extends Library{
	PORT INSTANTCE=(PORT)Native.loadLibrary("MD5",PORT.class);//MD5为DLL文件名
	public void MD5(charReference input,charReference output);

将DLL文件放在项目bin文件夹下

3、MD5的JAVA实现

import java.io.UnsupportedEncodingException;
import com.sun.jna.*;
import com.sun.jna.ptr.PointerByReference;
import tools.byteToString;
import tools.PORT;
import tools.charReference;
public class MD5Encoder {
	//获得byte数组
	private static byte[] getBytes(String input) throws UnsupportedEncodingException {
		int byteLen=0;
		byte[] bytes=new byte[3*input.length()];
		for(int i=0;i<input.length();i++) {
			char now=input.charAt(i);
			byte[] nowCharBytes=null;
			if(isChinese(now)) {
				nowCharBytes=(""+now).getBytes("utf-8");
			}else {
				nowCharBytes=(""+now).getBytes();
			for(int j=0;j<nowCharBytes.length;j++) {
				bytes[byteLen++]=nowCharBytes[j];
		return bytes;
	//判断是否为中文
	private static final boolean isChinese(char c) {   
	    Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
	    if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
	            || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
	            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
	            || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
	            || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
	            || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
	        return true;  
	    return false;  
	public static String encoder(String inputString) throws UnsupportedEncodingException {
		charReference output=new charReference();
		charReference input=new charReference();
		input.Init(getBytes(inputString));
		PORT.INSTANTCE.MD5(input, output);
		String outputString="";
		for(int i =0;i<16;i++) {
			outputString=outputString+byteToString.bytesToHexString(output.getPointer().getByte(i));
		return outputString;
	public static void main(String[] args) throws UnsupportedEncodingException {
		charReference output=new charReference();
		charReference input=new charReference();
		String x="zx哈哈";
		input.Init(getBytes(x));
		PORT.INSTANTCE.MD5(input, output);
		String outputString="";
		for(int i =0;i<16;i++) {
			outputString=outputString+byteToString.bytesToHexString(output.getPointer().getByte(i));
		System.out.println(outputString);

byteToString.byteToHexString为另一个类中的字节数组转化成字符串的函数

public static String bytesToHexString(byte src) {
        int v = src & 0xFF;
        String hv = Integer.toHexString(v);
        if(hv.length()==1) {
        	return "0"+hv;
        return hv;

运行结果:
在这里插入图片描述
与某网站(
https://md5jiami.51240.com/)加密结果相同
在这里插入图片描述

一、 工作环境             1. windows (64位), JDK (64位),dll文件 (64位)         2. Linux (64位),      JDK (64位),so文件 (64位)             3. JNA的官方资源路径为https://github.com/twall/jna/ 二、 实际操作         1.在cf.h头文件...
     我真是个悲剧,最近在用javaJNA写一个调用c++dll,进行PE文件分析的程序!结果遇到一个大问题,找了2天都没有解决。     问题是这样的:     我在vc中导出的函数PEANALYZER_API char* GetFileName(void); #ifdef PEANALYZER_EXPORTS #define PEANALYZER_API __declsp...
    最近一个 Java 项目需要调用公司的读卡器读取卡号。C 开发提供了一个读取卡号的 DLL。     Java 调用 DLL 无非三种方法:JNI、JNAJNative     本来 C 开发测试时用了 JNative.jar 来调用 DLL,但是网路上的 JNative.jar 都是基于 32 位系统,其 jar 包中的两个 DLL 也均为 32 位。     在我本地尝试时都报...
这可能是由于C++和C#使用不同的内存管理机制导致的。C++使用的是手动内存管理,而C#使用的是自动垃圾回收机制。当C++调用C#回调函数并传递char*参数时,C#会将该参数传递给CLR(公共语言运行时),CLR会将该参数保存在堆内存中。由于CLR的自动垃圾回收机制,该堆内存并不会立即被释放,而是等待垃圾回收器进行垃圾回收时才会被释放。因此,在程序结束后才会打印该参数信息。 解决此问题的方法是在C++中对传递的char*参数进行复制,并在回调函数中释放该内存。这样可以确保在C++中打印该参数信息时,该参数的内存已经被释放,不会出现上述问题。