• Post Reply Bookmark Topic Watch Topic
  • New Topic

JNI Returns as Arguments

 
Nathan Washor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a question about what I can and can not do in JNI. I can't find a clear cut answer anywhere, and my attempts have failed. I'd simply like to know whether or not I can send into C (from Java) either an initialized or uninitialized object array (or any object for that matter). I want C to do it's stuff, and then set the value of that parameter equal to the address of a jobject (instantiated inside the C code). Thus acting just like a return. For example:
Java class:
public synch native myMethod(Object);
C function:
public jboolean Java_class_myMethod(JNIEnv* env, jobject jthis, jobject inObj){
jobject lObjectToReturn;
//lots of C stuff here
//lObjToReturn = something;
//finish up C stuff
inObj = lObjectToReturn;
return (jboolean) true;
}
And I know that this is not necessarily the proper way to return an object to Java from C (yes I should use the return object, but in this case it's a boolean, and I don't feel like changing many, many lines of code to work it this way).
I've tried doing:
&inObj = &lObjectToReturn;//doesn't compile or work.
but that is essentially what I am trying to do.
Anyone know if this is legal, possible in any way???
Thanks.
Nathan
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are basically just asking if you can do "in-out" parameters in Java with JNI. The answer is no, you can't. The Java language simply doesn't have in-out parameters; no amount of JNI monkeying is going to change that.
Now, since the parameter is an array, if you're just interested in changing the contents of the array object that gets passed in as a parameter, then yes, you can do that, using the specific JNI methods for manipulating arrays. Perhaps your routine could work by setting element 0 of the array to the object you want to return. This would probably require that the type of the array be java.lang.Object[] .
Note that passing an array as an argument, and letting the routine change the array contents, is a standard Java idiom for simulating in-out parameters.
 
Nathan Washor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's what I thought. The Object[] idea is good, but would require just as much work as just returning the objects in a return object. Thanks for the advice though.
Nathan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!