JNA 与本机代码的通信

发布于 2024-10-08 14:23:56 字数 1171 浏览 0 评论 0原文

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

CP210x_GetProductString( DWORD DeviceNum,LPVOID DeviceString,DWORD Options)
  1. DeviceNum — 需要产品描述字符串、序列号或完整路径的设备索引。
  2. DeviceStringCP210x_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]);
            }

        }
    }
}

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)
  1. DeviceNum — Index of the device for which the product description string, serial number, or full path is desired.
  2. DeviceString — Variable of type CP210x_DEVICE_STRING returning the NULL-terminated serial number, device description or full path string.
  3. Options — Flag that determines if DeviceString 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2024-10-15 14:23:56

不知道这是否有效,但来自 CP210x_GetProductString 的描述 我认为您必须将长度为 256 的 byte[] 传递给函数(用于填充字符串的缓冲区)。

 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());

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).

 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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文