简体   繁体   English

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

[英]How to pass pointer to a structure in JNA?

I`m working on a example to pass a pointer to a structure to native library in red hat Linux platform.我正在研究一个示例,将指向结构的指针传递给红帽 Linux 平台中的本机库。 I followed the FAQ and instructions given here .我按照此处给出的常见问题解答和说明进行操作。 Nothing worked out so far.到目前为止没有任何结果。 My native code goes like below:我的本机代码如下所示:

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);
    
}

and my Java code like:和我的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);
        }
    }
}

while I try to java program I am not getting the passed values in structure member variables a,b,c,d,e,f but 0 always.当我尝试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

Am I missing something here in Java code?我在 Java 代码中遗漏了什么吗? Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks in advance.提前致谢。

I've used the pattern below before, as I found the performance higher than the automatic marshaling performed by JNA.我以前使用过下面的模式,因为我发现性能比 JNA 执行的自动编组要高。 Give it a shot.试一试。

First, the structure:一、结构:

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);
   }
   ...
}

Next, the API:接下来,API:

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

   public static native void printStruct(Pointer obis);
}

Finally, the usage:最后,用法:

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

If you have different structures, with short , int , long , etc. the Pointer class provides easy accessors for reading/writing them.如果您有不同的结构,例如shortintlong等, Pointer类提供了简单的访问器来读取/写入它们。

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

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