Java 与本机代码的JNA通信
我有这个本机函数,当我将设备连接到我的系统时,我在JNA中得到空值。我想我在LPVOID映射JNA时遇到了问题。任何想法都将受到赞赏Java 与本机代码的JNA通信,java,native,jna,Java,Native,Jna,我有这个本机函数,当我将设备连接到我的系统时,我在JNA中得到空值。我想我在LPVOID映射JNA时遇到了问题。任何想法都将受到赞赏 CP210x_GetProductString( DWORD DeviceNum,LPVOID DeviceString,DWORD Options) DeviceNum-需要产品描述字符串、序列号或完整路径的设备索引 DeviceString-类型为CP210x\u DEVICE\u STRING的变量,返回以NULL结尾的序列号、设备描述或完整路径字符串 选
CP210x_GetProductString( DWORD DeviceNum,LPVOID DeviceString,DWORD Options)
DeviceNum
-需要产品描述字符串、序列号或完整路径的设备索引DeviceString
-类型为CP210x\u DEVICE\u STRING
的变量,返回以NULL结尾的序列号、设备描述或完整路径字符串选项
-确定设备字符串
是否包含产品说明、序列号或完整路径字符串的标志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]);
}
}
}
}
公共类Helloworld{
公共接口CLibrary扩展库{
CLibrary实例=(CLibrary)Native.loadLibrary(
(Platform.isWindows()?“CP210xManufacturing.dll”:“c”),
类);
int CP210x_GetProductString(int dn,String[]ds,int op);
}
公共静态void main(字符串[]args){
int dn=0;
字符串dsc=新字符串[100];
if(CLibrary.INSTANCE.CP210x_GetProductString)(dn,dsc,
CP210x.CP210x_返回_序列号)==CP210x.CP210x_成功){
{
对于(int i=0;i不知道这是否可行,但是从CP210x_GetProductString的开始,我认为您必须将长度为256的字节[]传递给函数(用字符串填充的缓冲区)
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());