JNA: what is the purpose of getFieldOrder() in structure class(JNA:Structure类中getFieldOrder()的用途是什么)
问题描述
我正在尝试调用DLL文件中存在的C++函数,C++函数通过引用将Structure对象作为参数,函数将在该函数中赋值,
因此,在我的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:
但是,您很快就会遇到这样一个事实,即您试图将JNA按正确顺序返回此结构的字段名
Structure
映射到C++类,这根本不会起作用。JNA不提供JNA和C++类之间的任何自动转换。
编辑
明确地说:
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "xxx", "yyy", "zzz", "www" });
}
这篇关于JNA:Structure类中getFieldOrder()的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JNA:Structure类中getFieldOrder()的用途是什么


基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01