JNA与C对应的数据类型:
注意:
使用byte[]对应C++中的char* 可以返回函数执行的结果值
一、添加JNA需要的jar包
1、jna.jar
2、platform.jar
二、编写调用DLL的类
package rfid; import com.sun.jna.*; import com.sun.jna.ptr.*; public interface MasterRD extends Library { MasterRD instance = (MasterRD) Native.loadLibrary("MasterRD", MasterRD.class); int lib_ver(IntByReference ver); int rf_init_com(int port, NativeLong baud); int rf_beep(short idev, byte msec); int rf_request(short idev, byte model, short[] tagtype); int rf_anticoll(short idev, byte bcnt, byte[] psnr, byte[] prlength); int rf_select(short idev, byte[] psnr, byte srclen, byte[] size); int rf_M1_authentication2(short idev, byte model, byte block, byte[] key); int rf_M1_read(short idev, byte block, byte[] pdata, byte[] plen); int rf_M1_write(short idev, byte block, byte[] data); int rf_light(short idev, byte color); int rf_ClosePort(); }
其中MasterRD为MasterRD.dll 文件名,对应于DLL中的函数如下【部分举例】:
/* * 提取DLL接口 */ #ifndef _EXPORT_FUNC_H_ #define _EXPORT_FUNC_H_ #include <Windows.h> int (WINAPI* lib_ver)(unsigned int *nVer); int (WINAPI * des_encrypt)(unsigned char *szOut,unsigned char *szIn , unsigned int inlen,unsigned char *key,unsigned int keylen); int (WINAPI * des_decrypt)(unsigned char *szOut,unsigned char *szIn , unsigned int inlen,unsigned char *key,unsigned int keylen); int (WINAPI * rf_init_com)(int port,long baud); int (WINAPI * rf_init_device_number)(unsigned short icdev); int (WINAPI * rf_get_device_number)(unsigned short *Icdev); int (WINAPI * rf_get_model)(unsigned short icdev,unsigned short *Version); int (WINAPI * rf_get_snr)(unsigned short icdev,unsigned char *Snr); int (WINAPI * rf_beep)(unsigned short icdev,unsigned char msec); #endif
三、使用
MasterRD.instance.lib_ver() ….