JNI does not return the original value

N

Neena

Hi

I have a java code, which calls my native method setvalue. the setvalue
function receives values from my java code and the values are of type
java.lang.object.the value may be String, Long. Integer, byte[] etc.
the native method receives the value as jobject.my native call makes
another call to set the value and for this i have to convert the
jobject to a void pointer. i just make a direct casting.and the value
gets printed before and after casting are the same.

then from java i make a call to my native method getValue. getValue
makes another call and from there getValue receive the value as a
voidpointer. in Getvalue i have to convert this to a bytearray and then
return to my java call. in java i have to convert this to Object and
return to the application.

And my problem is i have passed a Long value( Long l=new Long(1); ) to
my setValue function. and my getValue returns unexpected value...what i
get back is not the value i've set..

what may be the problem?

my code:
setValue:

JNIEXPORT jboolean JNICALL Java_SetName_setValue
(JNIEnv *env, jobject obj, jobject nameValue)
{

printf("Name value in c, %x",nameValue);

return UpperLayerCall_SetValue((PVOID)nameValue);
//PVOID: void pointer

}

getValue:

JNIEXPORT jbyteArray JNICALL
Java_GetName_getValue
(JNIEnv *env, jobject obj, jint valueID, jlong valueLength)

{


PVOID nameValue = NULL; //void pinter..it is memory allocated

jbyteArray nameValueArray = NULL;

UpperLayerCall_GetValue( (int)valueID), nameValue);
//after this call nameValue will contain value we have set earlier


nameValueArray = (*env)->NewByteArray(env, valueLength);

(*env)->SetByteArrayRegion(env, nameValueArray, 0, valueLength,
nameValue);


return nameValueArray ;

}
 
G

Gordon Beaton

And my problem is i have passed a Long value( Long l=new Long(1); ) to
my setValue function. and my getValue returns unexpected value...what i
get back is not the value i've set..

what may be the problem?

- it isn't clear exactly what UpperLayer_SetValue() and
UpperLayer_GetValue() do exactly, but I'll assume that one stores
the original jobject pointer, and the other retrieves it unchanged.

- it isn't clear what valueLength contains, or how you know what
length to provide.

- it isn't clear how the Java application attempts to convert the byte
array back to a Long.

However none of these things really matter. The important thing to
realize is that after calling UpperLayer_GetValue(), nameValue does
*not* contain the Long "value". It is a pointer to a JVM-internal
structure that (ultimately) contains the value. To retrieve the value
itself you must use JNI functions made for that purpose, or pass it as
a Long back to the caller. These bytes are not meaningful to your Java
application!

So basically it seems that the native method is filling a byte array
with garbage and you are expecting your Java application to somehow
make sense of it.

Since it isn't clear what real problem you are trying to solve, it
isn't possible to suggest a solution.

/gordon
 
R

Roedy Green

(PVOID)nameValue

You should not do that. You can only manipulate the pointers handed
to you via the remote-control methods. For a list of them see
http://mindprod.com/jni.html

JNI is like eating with metre long chopsticks. You are figuratively
speaking trying to eat with your hands.

In addition to that, you are going to have to cast. I have never cast
one flavour of Java object to another on the C side. I suggest
reading one of the JNI textbooks for how you go about that. I would
not trust a C-style cast to be sufficient.

Your JNI project seems peculiar. It looks much more suited to Java
that C. Do your casting on the Java side to simplify the C side.

Consider passing in a structure with many fields, and putting your
return value in one of the slots, and then on the Java side sorting
out just what you got. That would keep things simple on the C side.

For passing data in, you can have a different, possible overloaded,
method for each type, or again you can pass in a common structure with
just one of its fields filled in and an indicator which one.

You can think of it as a bit like a union that does not overlap.

JNI is so (complicated, persickity, fussy, delicate, fragile) you have
to make everything duck simple to have a change at getting it all to
work.

At any case, you might start by making one of my simplifications, and
once you get that working inch your way back to your original design a
step at a time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top