繁体   English   中英

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

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

我正在研究一个示例,将指向结构的指针传递给红帽 Linux 平台中的本机库。 我按照此处给出的常见问题解答和说明进行操作。 到目前为止没有任何结果。 我的本机代码如下所示:

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 代码中遗漏了什么吗? 任何帮助是极大的赞赏。 提前致谢。

我以前使用过下面的模式,因为我发现性能比 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类提供了简单的访问器来读取/写入它们。

暂无
暂无

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

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