Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNA:what is the purpose of getFieldOrder() in structure class

I am trying to call C++ function present within a dll file,C++ function takes structure object as parameter by reference,and function will assign values in that function,

So in my java application in order to pass structure object to a function i did write like this:

interface SomeInterface extends Library {
    SomeInterface instance = (SomeInterface) Native.loadLibrary("mydll", SomeInterface.class);
    int someFunction(StructClass.ByReference strobject);

    public static class StructClass extends Structure {
        public static class ByReference extends tTIDFUDeviceInfo implements Structure.ByReference {}
        public short xxx = 0;
        public char yyy = '0';
        public boolean zzz = false
        public String www = new String();

        protected ArrayList getFieldOrder() {
            // TODO Auto-generated method stub
            ArrayList fields = new ArrayList();
            fields.add(Arrays.asList(new short{xxx}));
            fields.add(Arrays.asList(new char{yyy}));
            fields.add(Arrays.asList(new boolean{zzz}));
            fields.add(Arrays.asList(new String{www}));
            return fields;
        }
    }
}

my main class

public class Someclass {
    public static void main(String args[]) {
        SomeInterface.StructClass.ByReference sss=new SomeInterface.StructClass.ByReference();
        SomeInterface obj = SomeInterface.instance;
        obj.someFunction(sss);
    }
} 

when i tried this it is giving me

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Comparable

so what do i do?is there any problem in getFieldOrder() function?

can anyone explain me how exactly JNA will convert the class object in java to structure object in C++?

actually exception is happening at calling the function but i don't get it why it happening so.

like image 204
Mahantesh M Ambi Avatar asked Nov 22 '12 10:11

Mahantesh M Ambi


1 Answers

From the JavaDoc:

Return this Structure's field names in their proper order

However, you are going to quickly run up against the fact that you're attempting to map a JNA Structure onto a C++ class, which simply won't work. JNA does not provide any automatic translation between JNA and C++ classes.

EDIT

To be explicit:

protected List<String> getFieldOrder() {
    return Arrays.asList(new String[] { "xxx", "yyy", "zzz", "www" });
}
like image 108
technomage Avatar answered Oct 04 '22 02:10

technomage