StudentStruct$ByValue does not provide enough names [0] ([]) to match declared fields [2]
JNA调用结构体传参报错
Exception in thread "main" java.lang.Error: Structure.getFieldOrder() on class java_jna_2.HelloJNA_struct$LibraryStruct$StudentStruct$ByValue does not provide enough names [0] ([]) to match declared fields [2] ([stu_age, stu_name]) at com.sun.jna.Structure.getFields(Structure.java:1110) at com.sun.jna.Structure.deriveLayout(Structure.java:1265) at com.sun.jna.Structure.calculateSize(Structure.java:1192) at com.sun.jna.Structure.allocateMemory(Structure.java:413) at com.sun.jna.Structure.ensureAllocated(Structure.java:389) at com.sun.jna.Structure.ensureAllocated(Structure.java:379) at com.sun.jna.Structure.write(Structure.java:788) at com.sun.jna.Structure.autoWrite(Structure.java:2259) at com.sun.jna.Function.convertArgument(Function.java:532) at com.sun.jna.Function.invoke(Function.java:345) at com.sun.jna.Library$Handler.invoke(Library.java:265) at com.sun.proxy.$Proxy0.stuTea(Unknown Source)原因:List fieldOrder = this.fieldOrder();为空,找不到定义的字段
解决:在结构体类中重写getFieldOrder()方法,把字段填上,如
class StudentStruct extends Structure { // 定义变量 public int stu_age; public String stu_name; // 定义传递方式 public static class ByReference extends StudentStruct implements Structure.ByReference{ //指针和引用的传递使用ByReference } public static class ByValue extends StudentStruct implements Structure.ByValue{ //拷贝参数传递使用ByValue } @Override protected ListgetFieldOrder() { return Arrays.asList("stu_age", "stu_name"); } };
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)