If I'm not mistaken, a JNA Structure builds the struct by relying on the public fields of its corresponding Java class, which should extend Structure
. My problem is that I need to pass a struct whose declaration is not known beforehand (let's say it's known at runtime). I just have a list of Object
s, which the C library expects as a (reference to a) struct. Can I still use the Structure
class or must I build a Memory object by hand, dealing with sizes, alignments/packing myself?
For example:
/* native code */
typedef struct mystruct {
int x;
float y;
} mystruct;
void dosomething(mystruct * s) {
s->y += s->x;
}
And in Java:
...
callFunctionWithSt("dosomething",
new Object[]{Integer.valueOf(2),Float.valueOf(3.0)});
...
void callFunctionWithSt(String funcName, Object[] structVals) {
NativeLibrary nl = ...
Pointer arg = ... // or Memory ... or Structure
// build structure
for (Object objJava : structVals) {
valJna = convertFromJavaToJnaObject(objJava);
// fill structure
}
f.invoke(arg);
}
Object convertFromJavaToJnaObject(Object) {
// assume we know how to do this
}
If you don't know the content of the struct, then it is referred to as "opaque".
For opaque
struct *
, you can usecom.sun.jna.Pointer
to represent the structure address.You can still use
Structure
. Define your interface method like this:Then when you call it, you just do this: