如何将指针传递给JNA中的结构?

时间:2013-12-11 07:13:36

标签: java jna

我正在研究一个示例,将指向结构的指针传递给red hat Linux平台中的本机库。我按照here给出了常见问题解答和说明。到目前为止没有任何结果。我的原生代码如下:

typedef struct Code
{
    unsigned char a;
    unsigned char b;
    unsigned char c;
    unsigned char d;
    unsigned char e;
    unsigned char f;
} CODE;

void printStruct(CODE * code) {

    printf("OBIS value =%d.%d.%d.%d.%d.%d \n ", code->a, code->b, code->c, code->d, code->e, code->f);

}

和我的Java代码如:

public class JNATest {


    interface CLibrary extends Library {
        public static class CODE extends Structure {
            public int a=0,b=1,c=2,d=3,e=4,f=5;
            public CODE() {
                allocateMemory();
                autoWrite();
            }
            public CODE(Pointer p) {
                super(p);
            }
            @Override
            protected List getFieldOrder() {
                return Arrays.asList(new String[]{"a", "b",
                        "c", "d", "e", "f"});
            }

            public static class ByReference extends CODE implements Structure.ByReference {};
            public static class ByValue extends CODE implements Structure.ByValue {};
        }

        CLibrary INSTANCE = (CLibrary) Native.loadLibrary("./libStructTest.so", CLibrary.class);
        void sayHello(String name);
        void printStruct(CODE obis);
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            CLibrary.INSTANCE.sayHello("Sara");
            struct.JNATest.CLibrary.CODE obis = new struct.JNATest.CLibrary.CODE();
            obis.writeField("a", 0);
            obis.writeField("b", 0);
            obis.writeField("c", 1);
            obis.writeField("d", 0);
            obis.writeField("e", 0);
            obis.writeField("f", 255);
            obis.write();

            Pointer ptr = obis.getPointer();
            System.out.println("ptr = " + ptr.toString());
            CLibrary.INSTANCE.printStruct(obis);
            Pointer p = obis.getPointer();                              
            System.out.println(obis.size() + ":c=" + obis.c);  

            System.out.println(obis.size() + ":c=" + obis.c);

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

当我尝试java程序时,我没有得到结构成员变量a,b,c,d,e,f中的传递值,但总是为0。

ptr = auto-allocated@0x6d3daa68 (24 bytes)
OBIS value =0.0.0.0.0.0
24:c=1
24:c=1

我在Java代码中遗漏了什么吗?任何帮助是极大的赞赏。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

之前我使用过以下模式,因为我发现性能高于JNA执行的自动编组。试一试。

首先,结构:

public class Code {
   private Pointer pointer;

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

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

   Pointer getPointer() {
      return pointer;
   }

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

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

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

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

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

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

接下来,API:

class JNATest {
   static {
      Native.register("StructTest"); // undecorated name ... becomes libStructTest.so on Linux
   }

   public static native void printStruct(Pointer obis);
}

最后,用法:

Code code = new Code();
code.setA((byte) 'a');
code.setB((byte) 'b');
...
JNATest.printStruct(code.getPointer());
code.free();  // free the memory allocated in the constructor

如果你有不同的结构,shortintlong等等,Pointer类提供了便于阅读/编写的结构。