简体   繁体   中英

String gets a weird form after calling a ndk c++ function from Android Java

This is the function I call from Android:

String entityID = "414";
app.JoinConference(entityID);

And I have this code in my c++ jni files:

JNIEXPORT void Java_com_vidyo_vidyosample_VidyoSampleApplication_JoinConference(jstring eid)
{
FUNCTION_ENTRY;
LOGI("GuiOnOutEvent errorNOT JoinConference() enter\n");
VidyoClientPortalServiceJoinConferenceRequest createSchedRoomReq = {0};
createSchedRoomReq.typeRequest = VIDYO_CLIENT_PRIVATE_SOAP_JOIN_CONFERENCE;
LOGI("GuiOnOutEvent errorNOTJoinConference enter3: %s \n", eid);
//  createSchedRoomReq.entityID = eid;
strlcpy(&createSchedRoomReq.entityID, eid, sizeof(eid));
LOGI("errorNOTJoinConference request is:  %d pin %s + link: %d ", createSchedRoomReq.typeRequest,
     createSchedRoomReq.entityID, VIDYO_CLIENT_PRIVATE_IN_EVENT_VCSOAP);
LOGI("errorNOTJoinConference sizeof : %d", sizeof(VidyoClientPrivateSoapInEventCreateScheduledRoom));
VidyoClientSendEvent( VIDYO_CLIENT_PRIVATE_IN_EVENT_VCSOAP, &createSchedRoomReq, sizeof(VidyoClientPortalServiceJoinConferenceRequest));
LOGI("GuiOnOutEvent errorNOTJoinConference EXIT\n");
FUNCTION_EXIT;
}

What Am I doing wrong? The line with "enter3" which logs out the eid, logs out the following instead of "414":

01-24 18:04:29.310: I/VidyoMobile app/src/main/jni/ndkVidyoSample.c(12976): GuiOnOutEvent errorNOTJoinConference enter3: |9Ǵ 

What should I use instead of jstring in the methods parameters for the ndk function? OR do I need to change it from both the android and ndk to some kind of char array?

You cant directly pass jstring eid as argument to printf like function, you need to first get const char* from it, like:

const char *s = (*env)->GetStringUTFChars(env, eid, 0);

use s as argument to LOGI, and then when s is no longer needed:

(*env)->ReleaseStringUTFChars(env, eid, s);

Also why your jni function have no JNIEnv *env parameter? Static native methods signature should be:

JNIEXPORT jstring JNICALL
....... (JNIEnv *env, jclass type, jstring str);

and for non static:

JNIEXPORT jstring JNICALL
....... (JNIEnv *env, jobject instance, jstring str);

with android studio support for jni code its now super easy, you write in java code your native function for example:

public native void test(String s);

then IDE marks it as red, you click alt+enter on it and choose to generate native function which looks as follows:

JNIEXPORT void JNICALL
Java_com_example_hellojni_HelloJni_test(JNIEnv *env, jobject instance, jstring s_) {
  const char *s = (*env)->GetStringUTFChars(env, s_, 0);

  // TODO

  (*env)->ReleaseStringUTFChars(env, s_, s);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM