jna使用心得

此文档陆续记录JNA的使用经验!

会根据实际操作陆续更新

 

  1. 带有返回值的字符串形参
    • int lib_ver(unsinged char *buff);
      
      功 能:读取软件版本号
      
      参 数:buff:存放版本号的缓冲区,长度18字节(包括结束字符’\0’)。
      
      返 回:成功则返回 0
      
         例:unsigned char buff[18];
      
                lib_ver(buff);
      
                    printf(“software version is %s”,buff);
      
       
    • JNA JAVA对应方法: int lib_ver(byte[] buf);
    • Java调用方式:
      	public synchronized String version() throws RuntimeException {
      		byte[] buf = new byte[18];
      		machine.lib_ver(buf);
      		return Native.toString(buf);
      	}
        
    • 参考资料:官方解答How to return a out String paramter in the JNA? 
      How do I read back a function's string result?
      
      Suppose you have a function:
      // Returns the number of characters written to the buffer
      int getString(char* buffer, int bufsize);
      The native code is expecting a fixed-size buffer, which it will fill in with the requested data. A Java String is not appropriate here, since Strings are immutable. Nor is a Java StringBuffer, since the native code only fills the buffer and does not change its size. The appropriate argument type would be either byte[], Memory, or an NIO Buffer, with the size of the object passed as the second argument. The method Native.toString(byte[]) may then be used to convert the array of byte into a Java String.

    • 字符串转换指定编码,解决乱码问题:Native.toString(buf, "GBK") 
       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值