java - JNA:结构类中getFieldOrder()的目的是什么

标签 java class visual-c++ structure jna

我正在尝试调用 dll 文件中的 C++ 函数,C++ 函数通过引用将结构对象作为参数,函数将在该函数中赋值,

所以在我的 java 应用程序中,为了将结构对象传递给函数,我确实是这样写的:

interface SomeInterface extends Library {
    SomeInterface instance = (SomeInterface) Native.loadLibrary("mydll", SomeInterface.class);
    int someFunction(StructClass.ByReference strobject);

    public static class StructClass extends Structure {
        public static class ByReference extends tTIDFUDeviceInfo implements Structure.ByReference {}
        public short xxx = 0;
        public char yyy = '0';
        public boolean zzz = false
        public String www = new String();

        protected ArrayList getFieldOrder() {
            // TODO Auto-generated method stub
            ArrayList fields = new ArrayList();
            fields.add(Arrays.asList(new short{xxx}));
            fields.add(Arrays.asList(new char{yyy}));
            fields.add(Arrays.asList(new boolean{zzz}));
            fields.add(Arrays.asList(new String{www}));
            return fields;
        }
    }
}

我的主课

public class Someclass {
    public static void main(String args[]) {
        SomeInterface.StructClass.ByReference sss=new SomeInterface.StructClass.ByReference();
        SomeInterface obj = SomeInterface.instance;
        obj.someFunction(sss);
    }
} 

当我尝试这个时,它给了我

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Comparable

那怎么办?是不是getFieldOrder()函数有问题?

谁能解释一下 JNA 如何将 java 中的类对象转换为 C++ 中的结构对象?

实际上在调用函数时发生了异常,但我不明白为什么会这样。

最佳答案

From the JavaDoc:

Return this Structure's field names in their proper order

但是,您很快就会遇到这样一个事实,即您正试图将 JNA Structure 映射到 C++ 类,而这根本行不通。 JNA 不提供 JNA 和 C++ 类之间的任何自动转换。

编辑

明确地说:

protected List<String> getFieldOrder() {
    return Arrays.asList(new String[] { "xxx", "yyy", "zzz", "www" });
}

关于java - JNA:结构类中getFieldOrder()的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13510975/

相关文章:

java - 如何使用 ognl 更改 session 范围属性?

.net - 如果 .NET 使应用程序占用空间过大,托管内存有哪些优势?

c++ - emplace_back() 如何在 Visual Studio 2012 中没有可变参数模板的情况下工作?

class - 模块导出类 Nodes.js

c++ - visual c++ 程序问题

java - Java 时区枚举中的 Eclipse 变量错误

java - 向我的本地主机 Jetty 服务器发送消息

java - Libgdx - 在函数中使用 void 作为参数

c++迭代后破坏对象的问题

python - 在Python中使用@property构建一个类