JNI: Calling Java Method with String as Parameter from C
Hi,
In my C Method i try to call a Method, which expects a String as
Parameter. Therefore the String will be created in the c function and
then passed to the java method. Everything works fine, expect of the
fact, that i get in the Java Method a String object with length 0 :(
The strange thing is, that in the c code the jstring object seems to
be correct (getUtfSize() returns the right length). But on Java Side
the length is 0. So what am i doing wrong???? I thought about it the
whole day without finding any solution!
Thanks for your help in advance!!!
Source Code:
Java:
private synchronized boolean internalNotifyCallback(String obj)
{
if(obj != null) {
System.out.println(obj.length());
System.out.println("object is not null : " + obj);
}
if(closeNotify){
System.out.println("CloseNotify is " + closeNotify);
}
return !closeNotify;
}
C:
void listenXYZ(RXPARAMS par)
{
....
jsize count;
jmethodID jmid;
jclass icls;
jboolean continueListening = 1;
char string[50];
RXPARAMS prxParams = par;
JNIEnv *env = par.env;
.....
icls = (*env)->GetObjectClass(env, prxParams.jobj);
//get CallBack Address
jmid = (*env)->GetMethodID(env, icls,
"internalNotifyCallback","(Ljava/lang/String;)Z");
writeLog("after getMID\n");
if (jmid == 0){
writeLog("jmid == NULL\n");
}
const char *pWelt = "Hello World";
jstring jStr = NULL;
jStr = (*env)->NewStringUTF(env, pWelt);
if (jStr != NULL){
jsize size = (*env)->GetStringUTFLength(env,jStr);
writeLog("Is Not Null. Size: %d \n", size);
}
else
writeLog("Is Null\n");
continueListening = (*env)->CallBooleanMethod(env, prxParams.jobj,
jmid, eventObj, jStr);
}
P.S. I know that my english isn't very good - but i try to improve it,
so please be lenient toward me ;)