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.