[英]JNA communication to Native Code
I have this native function and I get the null value in JNA when I attach device to my system I think I have problem in LPVOID maping with JNA any Idea will be appreciated. 我有这个本机函数,当我将设备连接到系统时,我在JNA中得到空值,我认为我在使用JNA进行LPVOID映射时遇到问题,任何想法都将不胜感激。
CP210x_GetProductString( DWORD DeviceNum,LPVOID DeviceString,DWORD Options)
DeviceNum
— Index of the device for which the product description string, serial number, or full path is desired. DeviceNum
—需要其产品描述字符串,序列号或完整路径的设备的索引。 DeviceString
— Variable of type CP210x_DEVICE_STRING
returning the NULL-terminated serial number, device description or full path string. DeviceString
-型的可变CP210x_DEVICE_STRING
返回NULL结尾的序列号,设备描述或完整路径串。 Options
— Flag that determines if DeviceString
contains the product description, serial number, or full-path string Options
-确定DeviceString
包含产品描述,序列号或完整路径字符串的标志 JNA code: 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]);
}
}
}
}
Don't know if this will work, but from this description of CP210x_GetProductString I think you have to pass a byte[] with length 256 to the function (the buffer to fill with the String). 不知道这是否行得通,但是从对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.