Вопрос

I`m working on a example to pass a pointer to a structure to native library in red hat Linux platform. 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:

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.

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? 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. 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:

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top