JNA(JAVA调用lib/so)

JNA用途

当需要java直接调用c编写的dll或者so时候就可以用到JNA 

当然也可以用JNI也是可以的JNA相对JNI的优势是 JNA是在JAVA中把JAVA的数据转换成C的数据。

也就是开发量在JAVA端。JNI是需要修改C代码。


JNA 网址:http://jna.java.net/


用法 

直接吧JNA.jar加入项目的环境变量下面编写代码 加载你需要的LIB或so


public class ClassLib
public interface CLibrary extends Library{
CLibrary INSTANCE  = (CLibrary)Native.loadLibrary("client_ib", CLibrary.class);
}
}


然后在这个类中加入相对C的函数

public class ClassLib
public interface CLibrary extends Library{
CLibrary INSTANCE  = (CLibrary)Native.loadLibrary("client_ib", CLibrary.class);
public  int Sum(int a,int b);
}

}


当然 需要把你要调用dll放入环境变量中。


int rs = ClassLib.CLibrary.INSTANCE.Sum(10,20);
System.out.println(rs);


 

 简单的例子调用成功~! 

在C语言中结构体是经常被用到的。

下面简单介绍一下

结构体定义

public static class ss extends Structure{
		
		public byte[] RSIP=new byte[256];
		public int a=100;
		public int b=100;
		public static class ByValue     extends ss implements Structure.ByValue { }
		public static class ByReference extends ss implements Structure.ByReference { }
	}


如果结构体作为C函数的输入参数

			ss.ByValue tests = new ss.ByValue();
			tests.RSIP=String2Hex.stringToByteLength("192.168.1.53", "utf-8", 256);
			tests.a=100;
			tests.b=200;

上面的RSIP 必须赋予一个 byte[256]长度的值 这个要和C那边的定义保持一致  String2Hex是我自定义的函数。

int rs = ClassLib.CLibrary.INSTANCE.C函数名(结构体);
int rs = ClassLib.CLibrary.INSTANCE.testStructure(tests);


 

注意

Byvalue 与 ByReference 的区别

Byvalue作为函数的输入参数。所以Byvalue中的值必须初始化。

ByReference作为函数的输入参数。但该输入参数用作返回值。

输入参数中的值必须赋值、数组长度必须和C一致、结构体中的属性类型和顺序必须和C保持一致。

package ideabank.framework.util;

import ideabank.datebase.pojo.CertBean;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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

public  class CAPackUtil {
	

	
	public static Map<String,Object> getSendCAPackForApply(CertBean bean,int opt_type)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		Result.ByReference                 result = new Result.ByReference();
		CertGetRspInfo.ByReference certGetRspInfo = new CertGetRspInfo.ByReference();
		
		CAReqInfo.ByValue info = certBean_TO_CAReqInfo(bean);
		CAPackUtil.CLibrary lib = CAPackUtil.CLibrary.INSTANCE;
		lib.SendCA(opt_type, info, new RAUsedInfo.ByValue(), result, certGetRspInfo);
		map.put("Result.ByReference", result);
		map.put("CertGetRspInfo.ByReference", certGetRspInfo);
		map.put("state", String.valueOf(result.retval));
		return map;
	}

	
        public static int  RA_REG =  				1;	
	public static int  RA_CANCEL =  			5;	
	public static int  RA_UPDATE_KEY =  		        6;	
	public static int  RA_UPDATE_INFO =  		        7;	
	public static int  RA_DELAY  = 				8;	
	public static int  RA_FREEZE =  			9;	
	public static int  RA_UNFREEZE =  			10;	
	public static int  RA_ONLINE_KEY_RESUME =               11;	
	public static int  RA_OFFLINE_KEY_RESUME =	        12;	

	
	public static class CAReqInfo extends Structure{
		
		public byte[] extinfo;
		public byte[] subjectid;
		public byte[] ipaddress;
		public byte[] dnsname;
		public byte[] su_extsubaltname;
		public byte[] crldistpoint;
		public byte[] serialNo;
		public byte[] certReqID;
		public byte[] type;
		public byte[] certSn;
		public byte[] CarrierNum;
		public byte[] raManagerID;
		
		public static class ByValue extends CAReqInfo      implements Structure.ByValue { }
		public static class ByReference extends CAReqInfo  implements Structure.ByReference { }
	}
	
	public static class RAUsedInfo extends Structure{
		public byte[] RSIP    = String2Hex.stringToByteLength(StaticCodeBook.CAPACK_RAUsedInfo_RSIP, "utf-8", 256);
		public int RSPort     = Integer.parseInt(StaticCodeBook.CAPACK_RAUsedInfo_RSPort);
		public int MsgTimeOut = Integer.parseInt(StaticCodeBook.CAPACK_RAUsedInfo_MsgTimeOut);
		public static class ByValue     extends RAUsedInfo implements Structure.ByValue { }
		public static class ByReference extends RAUsedInfo implements Structure.ByReference { }
	}
	
	public static class Result extends Structure{
		
		public int retval=0;
		public int err_num=0;
		public byte[] err_buf=new byte[2048];
		
		public static class ByValue     extends Result implements Structure.ByValue { }
		public static class ByReference extends Result implements Structure.ByReference { }
	}
	
	public static class EnvBody extends Structure{
		
		
		public byte[] certSn=new byte[256];
		public byte[] commonName=new byte[256];
		public int   keyEnAlgID=0;
		public byte[] encryptedKey =new byte[2048];
		public int  encryptedKeylen=0;
		public int  contEnAlgID=0;
		public byte[] encryptedCont=new byte[2048];
		public int  encryptedContlen=0;
		
		public static class ByValue     extends EnvBody implements Structure.ByValue { }
		public static class ByReference extends EnvBody implements Structure.ByReference { }
	}
	
	public static class CertGetRspInfo extends Structure{
		
		public byte[] certReqID=new byte[256];
		public int status=0;
		public byte[] signcert=new byte[2048];
		public int signcertlen=0;
		public byte[] enccert=new byte[2048];
		public int enccertlen=0;
		public EnvBody.ByValue signKey = new EnvBody.ByValue();
		public EnvBody.ByValue encKey = new EnvBody.ByValue();
		public static class ByValue     extends CertGetRspInfo implements Structure.ByValue { }
		public static class ByReference extends CertGetRspInfo implements Structure.ByReference { }
	}
	


	public interface CLibrary extends Library{
        /**
         * 当前路径是在项目下,而不是bin输出目录下。
         */
		CLibrary INSTANCE1 = (CLibrary)Native.loadLibrary("acdb", CLibrary.class);
		CLibrary INSTANCE2 = (CLibrary)Native.loadLibrary("cert", CLibrary.class);
		CLibrary INSTANCE3 = (CLibrary)Native.loadLibrary("ComFunc", CLibrary.class);
		CLibrary INSTANCE4 = (CLibrary)Native.loadLibrary("Config", CLibrary.class);
		CLibrary INSTANCE5 = (CLibrary)Native.loadLibrary("crypto", CLibrary.class);
		CLibrary INSTANCE6 = (CLibrary)Native.loadLibrary("CryptoInterface", CLibrary.class);
		CLibrary INSTANCE7 = (CLibrary)Native.loadLibrary("msgcoding", CLibrary.class);
		CLibrary INSTANCE8 = (CLibrary)Native.loadLibrary("sqlapi", CLibrary.class);
		CLibrary INSTANCE9  = (CLibrary)Native.loadLibrary("fun_ib", CLibrary.class);
		CLibrary INSTANCE  = (CLibrary)Native.loadLibrary("client_ib", CLibrary.class);

		public int SendCA(int command_num,CAReqInfo.ByValue reqinfo ,RAUsedInfo.ByValue g_RAUsedInfo,Result.ByReference result,CertGetRspInfo.ByReference rspinfo);

       
    }
	
	private static CAReqInfo.ByValue certBean_TO_CAReqInfo(CertBean bean)
	{
		req.ipaddress=String2Hex.stringToByteLength("127.0.0.1", "utf-8", 256);
		req.dnsname=String2Hex.stringToByteLength("192.168.1.1", "utf-8", 256);
		req.su_extsubaltname=String2Hex.stringToByteLength("dddddddddddddd", "utf-8", 256);
		req.crldistpoint=String2Hex.stringToByteLength("http://www.test.com", "utf-8", 256);
		req.serialNo=String2Hex.stringToByteLength("123556", "utf-8", 256);
		
		req.certReqID=String2Hex.stringToByteLength("123556", "utf-8", 256);
		req.type=String2Hex.stringToByteLength("123556", "utf-8", 256);
		req.certSn=String2Hex.stringToByteLength("123556", "utf-8", 256);
		req.CarrierNum=String2Hex.stringToByteLength("123556", "utf-8", 256);
		req.raManagerID=String2Hex.stringToByteLength("123556", "utf-8", 256);
		
		RAUsedInfo.ByValue g_RAUsedInfo = new RAUsedInfo.ByValue();
		g_RAUsedInfo.RSIP=String2Hex.stringToByteLength("192.168.1.53", "utf-8", 256);
		g_RAUsedInfo.RSPort=4999;
		g_RAUsedInfo.MsgTimeOut=10000;
		return req;
	}
	

}

 
 
 
 
 
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值