Search code examples
javacpointersstructjna

How to send a pointer to uint_32 from Java in JNA?


I have the following struct in C:

struct m{
   unit32_t *p; // array of uint32_t
};

And the following function:

void print(struct *m);

How can I call this method with JNA and represent the structure in Java?


Solution

  • Nominally you'd do this:

    public interface MyLib extends Library {
        class MyStruct extends Structure {
            public Pointer p;
            protected List getFieldOrder() {
                return Arrays.asList("p");
            }
        }
        void print(MyStruct s);
    }
    
    MyLib lib = Native.loadLibrary("mylib", MyLib.class);
    MyStruct s = new MyStruct();
    s.p = new Memory(length * 4);
    lib.print(s);