Tuesday, July 23, 2002 04:35 PM
That's why sane people usually don't write JNI interfaces with complex types :-)
But seriously, even if you get this right after debugging all little JNI issues that cost you a day each, do you really want to maintain this code?
A typical pattern for exposing a complex C++ object to Java is to have a Java class that mirrors the structure in layout and has a constructor and two methods as in:
MyWrapper( long ptr );
private native void initFromCpp( long ptr );
private native void writeToCpp( long ptr );
The native methods are implemented as taking the "ptr" argument and casting it to a poiner to an instance of "myclass". Now you can get at the values of the C++ object and set them into the Java instance's fields, one by one.
Not trivial and very error prone. Alternatively check out JunC++ion at www.codemesh.com which allows you to implement your native method in straight C++, making assignments to Java fields etc. as you would in C++.
Hope this helps,