JNA 与本机代码的通信
我有这个本机函数,当我将设备连接到我的系统时,我在 JNA 中得到空值,我认为我在使用 JNA 进行 LPVOID 映射时遇到问题,任何想法都会受到赞赏。
CP210x_GetProductString( DWORD DeviceNum,LPVOID DeviceString,DWORD Options)
DeviceNum
— 需要产品描述字符串、序列号或完整路径的设备索引。DeviceString
—CP210x_DEVICE_STRING
类型的变量,返回以 NULL 结尾的序列号、设备描述或完整路径字符串。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]);
}
}
}
}
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.
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.DeviceString
— Variable of typeCP210x_DEVICE_STRING
returning the NULL-terminated serial number, device description or full path string.Options
— Flag that determines ifDeviceString
contains the product description, serial number, or full-path string
JNA code:
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]);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道这是否有效,但来自 CP210x_GetProductString 的描述 我认为您必须将长度为 256 的 byte[] 传递给函数(用于填充字符串的缓冲区)。
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).