I have a function in C which I'm trying to call from Java with JNA:
int myCfunc(void *s, int *ls);
According to the JNA documentation the void* requires a com.sun.jna.Pointer
be passed to the function. In java with JNA I believe the above function will be wrapped as follows:
public interface myWrapper extends Library{
public int myCfunc(Pointer s, IntByReference ls);
}
The objects which need to linked to the Pointer, and passed in parameter s
will be classes which implement JNA Structure, such as:
public class myClass extends Structure{
public int x;
public int y;
public int z;
}
Unfortunately, the parameter ls
is an integer representing the length of the class in bytes. Java doesn't have a sizeof
function, so this adds an extra bit of complexity.
The other major problem I'm having is ensuring I'm correctly passing the contents of the object to native memory and back.
my code is similar to the following:
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
public void foo(){
myWrapper wrapper = (myWrapper) Native.loadLibrary("SomeDllWithLegacyCode", myWrapper.class);
myClass myObj = new myClass();
myObj.x = 1;
myObj.y = 2;
Pointer myPointer = myObj.getPointer();
int size = Native.getNativeSize(myClass.class);
IntByReference len = new IntByReference(size);
myObj.write(); //is this required to write the values in myObj into the native memory??
wrapper.myCfunc(myPointer, len);
myObj.read(); //does this read in the native memory and push the values into myObj??
myPointer.clear(size); //is this required to clear the memory and release the Pointer to the GC??
}
I'm getting an error that the data passed is of a larger size than is expected by the C function.
The above code roughly following the same sort of steps as provided out in this answer to a question dealing with a similar issue, but in C#. I've tried and tested that it works in C#.
My question is similar to another on Stackoverflow, but that deals with a Pointer to an IntByReference rather than a pointer to a class.