• JNA java调用dll


    
    <dependency>
        <groupId>net.java.dev.jnagroupId>
        <artifactId>jnaartifactId>
        <version>5.12.1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.example.test;
    
    import com.sun.jna.Function;
    import com.sun.jna.Library;
    import com.sun.jna.Native;
    import com.sun.jna.NativeLibrary;
    import com.sun.jna.Platform;
    
    public class TestSMS {
    
    	static NativeLibrary library = NativeLibrary.getInstance("C:\\Users\\kysx\\Desktop\\SMSDLL.dll");
    
    	/**
    	 * 调用dll有返回值函数
    	 *
    	 * @param functionName 函数名
    	 * @param clazz        T类型Class
    	 * @param params       入参
    	 * @param           返回类型
    	 * @return dll函数返回值
    	 */
    	public static <T> T invoke(String functionName, Class<T> clazz, Object... params) {
    		Function function = library.getFunction(functionName);
    		Object invoke = function.invoke(clazz, params);
    		return clazz.cast(invoke);
    	}
    
    	/**
    	 * 调用dll无返回值函数
    	 *
    	 * @param functionName 函数名
    	 * @param params       入参
    	 */
    	public static void invoke(String functionName, Object... params) {
    		Function function = library.getFunction(functionName);
    		function.invoke(params);
    	}
    
    	/**
    	 * 生成dll函数
    	 *
    	 * @param functionName 函数名
    	 * @return dll函数
    	 */
    	public static Function function(String functionName) {
    		return library.getFunction(functionName);
    	}
    
    	/**
    	 * 测试
    	 */
    	public static void main(String[] args) {
    		/**
    		 * 参数:nPort 串口号 如1 则表示COM1
    		BaudRate 拨特率 115200  
    		Parity 校验位 2 
    		DataBits 数据位 8
    		StopBits停止位 0
    		FlowControl 流控制 0
    		Csca 短信中心号码,可以使用默认值,若设置则格式如:” +8613800000000”
    		   返回值:1成功,0失败
    		 */
    		int nPort = 5;
    		int BaudRate = 57600; 
    		int Parity=2;
    		int DataBits = 8;
    		int StopBits=0;
    		int FlowControl=0;
    		String csca="card";
    		int i = invoke("SMSStartService", Integer.class, nPort, BaudRate, Parity, DataBits, StopBits, FlowControl, csca);
    		System.out.println("输出的是:"+i);
    	}
    	
    	/**
    	默认类型映射 - Java类型与C类型映射。
    	=====================
    	Java primitive types (and their object equivalents) map directly to the native C type of the same size.
    	
    	
    Native TypeSizeJava TypeCommon Windows Types
    char8-bit integerbyteBYTE, TCHAR
    short16-bit integershortWORD
    wchar_t16/32-bit charactercharTCHAR
    int32-bit integerintDWORD
    intboolean valuebooleanBOOL
    long32/64-bit integerNativeLongLONG
    long long64-bit integerlong__int64
    float32-bit FPfloat
    double64-bit FPdouble
    char*C stringStringLPCSTR
    void*pointerPointerLPVOID, HANDLE, LPXXX
    Unsigned types use the same mappings as signed types. C enums are usually interchangeable with "int". */
    public String DefaultTypeMappings; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
  • 相关阅读:
    IDEA中给源码添加自己注释——private-notes插件安装使用
    君方智能设计平台-对象持久化技术方案
    “暂停加息,股市低迷:242只股票创新低,比特币突破2.8万美元后看涨趋势不可挡!“
    图像处理基本概念、GAN
    推荐系统学习 二
    Python库 pdf2docx 轻松将PDF转换成docx
    Swift 准备取代 Python,为什么 Swift 太早野心勃勃,以及这将如何影响它的未来
    SAP ALV颜色代码对应颜色(整理)
    iPhone没有收到iOS16最新版的推送,如何升级系统?
    预训练模型之ELMO -《Deep contextualized word representations》论文笔记 + 高频面试题
  • 原文地址:https://blog.csdn.net/qq_43939956/article/details/128156279