JNI-[坑3]JNA结构体传参ByValue does not provide enough names

JNI-[坑3]JNA结构体传参ByValue does not provide enough names,第1张

JNI-[坑3]JNA结构体传参ByValue does not provide enough names

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 List getFieldOrder() {
        return Arrays.asList("stu_age", "stu_name");
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4997318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存