简体   繁体   English

如何使用JNA处理结构内部的结构成员?

[英]how to process a structure member inside a structure using JNA?

I am working on JNA to return a nested structure pointer on a native function call. 我正在JNA上以在本机函数调用上返回嵌套结构指针。 The nested structure has members of type unsigned char, struct. 嵌套结构具有类型为unsigned char,struct的成员。 I was able to read the primitive types but struck up in processing the nested structure member eg OBISCODE in Response. 我能够读取原始类型,但是在处理嵌套结构成员(例如Response中的OBISCODE)时感到吃惊。 My nested Structure looks like below: 我的嵌套结构如下所示:

typedef struct ObisCode
{
    unsigned char a;
    unsigned char b;
    unsigned char c;
    unsigned char d;
    unsigned char e;
    unsigned char f;
} OBISCODE;

typedef struct Response
{
    unsigned char result;
    OBISCODE obis;
} RESPONSE;

and native function as: 本机功能为:

RESPONSE* getStruct(OBISCODE * obis)

My Java code calling this native function as below: 我的Java代码调用了本机函数,如下所示:

package struct;

    import com.sun.jna.Native;
    import com.sun.jna.Pointer;

public class JNATest {

    static {
        Native.register("StructTest"); 
    }


    public static class OBISCODE {
        private Pointer pointer;

        public OBISCODE() {
            long memory = Native.malloc(6);
            pointer = new Pointer(memory);
        }

        public OBISCODE(Pointer pointer) {
            this.pointer = pointer;         
        }

        Pointer getPointer() {
            return pointer;
        }

        int getA() {
            return pointer.getByte(0);
        }

        int getB() {
            return pointer.getByte(1);
        }

        int getC() {
            return pointer.getByte(2);
        }

        int getD() {
            return pointer.getByte(3);
        }

        int getE() {
            return pointer.getByte(4);
        }

        int getF() {
            return pointer.getByte(5);
        }

        void setA(byte a) {
            pointer.setByte(0, a);
        }

        void setB(byte b) {
            pointer.setByte(1, b);
        }

        void setC(byte c) {
            pointer.setByte(2, c);
        }

        void setD(byte d) {
            pointer.setByte(3, d);
        }
        void setE(byte e) {
            pointer.setByte(4, e);
        }
        void setF(byte f) {
            pointer.setByte(5, f);
        }

        void free() {
            Native.free(Pointer.nativeValue(pointer));
        }

    }


    public static native void printStruct(Pointer obis);

    public static native Pointer getStruct(Pointer obis);
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {

            struct.JNATest.OBISCODE obis = new struct.JNATest.OBISCODE();
            obis.setA( (byte) 0);
            obis.setB( (byte)  0);
            obis.setC( (byte)  8);
            obis.setD( (byte)  0);
            obis.setE( (byte)  0);
            obis.setF( (byte)  255);

            Pointer ptr = obis.getPointer();
            System.out.println("ptr = " + ptr.toString());
            printStruct(obis.getPointer());

            Pointer resptr = getStruct(obis.getPointer());
            Response response = new Response(resptr);
            System.out.println("result = " + response.getResult());
            System.out.println("1=" + resptr.getInt(1) + "2=" + resptr.getInt(2)
                    );

            obis.free();

        } catch (UnsatisfiedLinkError e) {
            System.out.println("Exception" + e);
        }
    }
}

My equivalent native structure Response class as below 我的等效本机结构Response类如下

package struct;

import com.sun.jna.Native;
import com.sun.jna.Pointer;

public class Response {

    private Pointer pointer;

    public Response() {
        long memory = Native.malloc(6);
        pointer = new Pointer(memory);
    }

    public Response(Pointer pointer) {
        this.pointer = pointer;         
    }

    Pointer getPointer() {
        return pointer;
    }

    int getResult() {
        return pointer.getByte(0);
    }

    int getOBIS() {
        return pointer.getByte(1);
    }

    void setResult(byte a) {
        pointer.setByte(0, a);
    }

    void setOBIS(byte b) {
        pointer.setByte(1, b);
    }

    void free() {
        Native.free(Pointer.nativeValue(pointer));
    }

}

I was running this test in Redhat Linux and the output obtained as below 我在Redhat Linux中运行此测试,并且获得的输出如下

result = 12
1=5242882=2048

Please correct me if I am missing something in Java code to process the structure. 如果我缺少Java代码来处理结构,请纠正我。 Any help is greatly appreciated? 任何帮助是极大的赞赏? Thanks in advance. 提前致谢。

JNA provides direct support for Structures . JNA为Structures提供直接支持。

For example: 例如:

public class ObisCode extends Structure 
{
    public byte a;
    public byte b;
    public byte c;
    public byte d;
    public byte e;
    public byte f;
    protected List getFieldOrder() { return Arrays.asList(new String[] { "a", "b", "c", "d", "e", "f" }); }
    public OBISCODE() { }
    public OBISCODE(Pointer p) { super(p); read(); }
}

public class Response extends Structure
{
    public byte result;
    public OBISCODE obis;
    protected List getFieldOrder() { return Arrays.asList(new String[] { "result", "obis" }); }
    public Response() { }
    public Response(Pointer p) { super(p); read(); }
}

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

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