Nathan Washor

Greenhorn
+ Follow
since Dec 17, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Nathan Washor

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
20 years ago
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
20 years ago
Yes it is possible to do what you're asking. What you'll want to do is make some Java objects that match your C++ objects. If the C++ Objects contain other Objects, you'll need to make identical Java objects of those types too. Then you need to write some methods in C++ which instantiate new Java Objects (the one's you are moving your data into) and copy the C++ Objects data into the Java Object's fields. Then the methods return these instantiated Java Objects to the Java Virtual Machine. This is all assuming you start execution in Java, and then call a C++ library to return the objects from the native side. If you are going the other way (start execution in C++ and send java objects to the JVM, and have the JVM do it's stuff with the data) then this needs to be altered a little. I always start in Java, so look around for some help on how to do it the other way. Should be very similar.
20 years ago
Hi Jess, thanks for the reply. Yup, that was the whole stupid problem. I was printing out the result incorrectly. Oops Thanks for the help though.
20 years ago
Greetings...
I am having some problems with JNI. I have written some pseudo code to better explain my problem. Basically I have an object in Java that I pass into the native as an argument. In the native code I want to use the values of the variables encapsulated in that object. The thing is I assign the values on the Java side of things, but when they get into the C side of things, the values associated with the variables are zero. I thought it was a pointer type issue, but played around with that a lot in the native, and I'm 99% sure that's not it. If I ran the following code I would get the following output:
Val in Java: 13
MidVal: 0 FidVal: 0
Any insight is greatly appreciated.
//The Java Class
package MyPackage;
imports...
public Class NativeCaller{

public static void main(String[] args){
NativeCaller lCaller = new NativeCaller();
AClass lArgument = new AClass;
lArgument.setVal(13);//a simple setter
System.out.println("Val in Java: " + lArgument.getVal());//simple getter
lCaller.MyMethod(lArgument);
}

public synchronized native Object MyMethod(AClass inArg);
}
//The C Function
JNIEXPORT jobject JNICALL Java_SomeProject_MyPackage_MyClass_MyMethod(
JNIEnv *env, jobject jthis, jobject inObject){
jobject result = (jobject)NULL;
jclass lCls;
jmethodID jmid;
jfieldID jfid;
double lMidVal;
double lFidVal;
lCls = (*env)->FindClass(env, "SomeProject/MyPackage/AClass");
if (lCls == NULL){
printf("Error getting class");
return (jobject)NULL;
}

if (!(*env)->IsInstanceOf(env, inObject, lCls)){
printf("input object is not correct type");
return (jobject)NULL;
}

jmid = (*env)->GetMethodID(env, lCls, "getVal", "()D");
jfid = (*env)->GetFieldID(env, lCls, "lVal", "D");
if (jfid == NULL || jmid == NULL){
printf("Problem finding jmid or jfid");
return (jobject)NULL;
}

lMidVal = (*env)->CallDoubleMethod(env, inObject, jmid);
lFidVal = (*env)->GetDoubleField(env, inObject, jfid);

printf("Mid Val: %d Fid Val: %d", lMidVal, lFidVal);
//more stuff done here, irrelevent
return result;
}
Just to clarify, all the JNI works fine, it runs, returns a null object, and
otherwise works fine. I simply cannot get the values out from the object passed
to the native.
Nathan Washor
njwashor@yahoo.com
20 years ago