繁体   English   中英

JNA与本机代码的通信

[英]JNA communication to Native Code

我有这个本机函数,当我将设备连接到系统时,我在JNA中得到空值,我认为我在使用JNA进行LPVOID映射时遇到问题,任何想法都将不胜感激。

CP210x_GetProductString( DWORD DeviceNum,LPVOID DeviceString,DWORD Options)
  1. DeviceNum —需要其产品描述字符串,序列号或完整路径的设备的索引。
  2. DeviceString -型的可变CP210x_DEVICE_STRING返回NULL结尾的序列号,设备描述或完整路径串。
  3. Options -确定DeviceString包含产品描述,序列号或完整路径字符串的标志

JNA代码:

public class Helloworld {

    public interface CLibrary extends Library{
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
            (Platform.isWindows() ? "CP210xManufacturing.dll" : "c"),
            CLibrary.class);

        int CP210x_GetProductString(int dn,String [] ds,int op);
    }

    public static void main(String[] args) {
        int dn=0;
        String dsc = new String[100];
        if(CLibrary.INSTANCE.CP210x_GetProductString(dn, dsc,
               CP210x.CP210x_RETURN_SERIAL_NUMBER) == CP210x.CP210x_SUCCESS){
        {
            for(int i=0;i<dsc.length;i++)
                System.out.print(dsc[i]);
            }

        }
    }
}

不知道这是否行得通,但是从对CP210x_GetProductString的描述中 ,我认为您必须将长度为256的byte []传递给函数(用于填充String的缓冲区)。

 int CP210x_GetProductString(int dn,byte[] ds,int op);

 //use it like this
 byte[] rawString = new byte[256];
 int dn = ...;
 int op = ...;
 CP210x_GetProductString(dn,rawString,op);
 //You might have to choose a different Charset here
 //since I don't know what encoding the string uses I used the default
 String product = new String(rawString,Charset.defaultCharset());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM