java - Passing a Structure with a Memory field in JNA -
my dynamic c library receives (pointer to) structures include allocated pointer (by malloc). called function allowed call realloc
on it.
typedef struct mystruct { void * buf; int buflen; /* more fields... */ } mystruct; void myfunc(mystruct *s1, /* more args*/) { /* in dynamic library */ /* .... */ s1->buf = realloc(s1->buf,newsize); /* .... */ }
i thought structure memory field trick,
public class mystructjna extends structure { public memory buf; public integer buflen; /* .... */ }
but exception:
exception in thread "main" java.lang.illegalargumentexception: structure field "buf" declared class com.sun.jna.memory, not supported within structure @ com.sun.jna.structure.writefield(structure.java:792)
any explanation and/or workaround? i'm using jna 4.0
the question answered , accepted, want add caveat, in case attempting similar approach:
this not design, because dll side realloc of pointer allocated in jna side, and, finally, jna side attempt free pointer (allocated in dll side). in general not safe.
you can't use memory
because must possible jna automatically initialize fields of structure.
you can give field pointer
type , assign memory
object it. field preserved long native code not modify value.
Comments
Post a Comment