How to create new java objects in native methods?
Hello,
Is it possible to create java objects and return the same to the java code from a native method?
Also is it possible to pass java objects other than String objects (for example, Vector objects) as parameters to native methods and is it possible to return such objects back to the java code?
If so how can I access those objects (say for example, accessing Vector elements) inside the native code?
What should I do in order to achieve them?
Regards,
Satish
[512 byte] By [
hisatish] at [2007-9-27 2:36:30]

Yes.To create new java objects, use FindClass and AllocObjectIf you are passed a class, you can call GetMethodID and CallXXXMethod to call the obect. If you know the names of the objects field, you can use GetFieldID and GetXXXField routines.Hope this helps.
Actually you need to get yourself a book on JNI, or possibly poke around the web for more info. I say this because the methods for creating objects require some study. In addition to those mentioned in the last posting, there are ways to call constructors, and there are also the NewString functions.
Reasonable book: Essential JNI by Rob Gordon
bschauwe is correct in that constructing Java objects and calling methods on Java objects from native code is tough and takes some study. While you're at it, you might want to check out Jace, http://jace.reyelts.com/jace. It's a free open-source toolkit that really takes the nastiness out of doing this sort of stuff. For example,/**
* A C++ function that takes a java.util.Vector and plays around with it.
*/
public void useVector( java::util::Vector& vector ) {
// Print out all the contents of the vector
for ( Iterator it = vector.iterator(); it.hasNext(); ) {
cout << it.next();
}
// Add some new elements to the vector
vector.addElement( "Hello" );
vector.addElement( "world" );
}
All this code just results in calls to standard JNI functions like FindClass, NewObject, GetMethodID, NewStringUTF, CallObjectMethod, etc...
God bless,
-Toby Reyelts