简体   繁体   English

如何使用JNA从结构中获取Structure.ByReference?

[英]How do I obtain a Structure.ByReference from a Structure using JNA?

I do have a object of Structure in my java code. 我的Java代码中确实有一个Structure对象。 But the Jar generated using JNAerator expects Structure.ByReference as a data type. 但是使用JNAerator生成的Jar需要将Structure.ByReference作为数据类型。 Is there any method in jna or code snippet to convert Structure object into Structure.ByReference object? jna或代码段中是否有任何方法可以将Structure对象转换为Structure.ByReference对象?

Generally, you don't need to explicitly specify Structure.ByReference when passing parameters. 通常,传递参数时无需显式指定Structure.ByReference If it's a parameter, you can drop the .ByReference from the signature and it'll work just fine. 如果是参数,则可以从签名中删除.ByReference ,这样就可以正常工作。

If it's a field within a structure, then JNA interprets Structure as by value, in which case you would need to provide the .ByReference explicitly. 如果它是一个结构中的一个字段,然后JNA解释Structure由值,在这种情况下, 需要提供.ByReference明确。

This is one way to do it. 这是做到这一点的一种方法。

class MyStructure extends Structure {
    class ByReference extends MyStructure implements Structure.ByReference {
        public ByReference() { }
        public Byreference(Pointer p) { super(p); read(); }
    }
    public MyStructure() { }
    public MyStructure(Pointer p) { super(p); read(); }
}

MyStructure s;
// ...
MyStructure.ByReference ref = new MyStructure.ByReference(s.getPointer());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM