java - JNA: how to deal with unkown structs? -
if i'm not mistaken, jna structure builds struct relying on public fields of corresponding java class, should extend structure
. problem need pass struct declaration not known beforehand (let's it's known @ runtime). have list of object
s, c library expects (reference a) struct. can still use structure
class or must build memory object hand, dealing 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 (object objjava : structvals) { valjna = convertfromjavatojnaobject(objjava); // fill structure } f.invoke(arg); } object convertfromjavatojnaobject(object) { // assume know how }
if don't know content of struct, referred "opaque".
for opaque struct *
, can use com.sun.jna.pointer
represent structure address.
Comments
Post a Comment