0

I am implementing a C++ to Java interface using JNI. I have a C++ function as follows:

static int testhandler(void *arg, uint32_t stream, uint32_t func, const char* name, uint32_t funcgroup, uint32_t source);

When I have to call this in Java using eclipse, I do it as follows:

public void handle(int stream, int func,char name, int group, int token);

But I am not able to read the const char part in Java using Eclipse. Does anybody know what the problem might be? Should I call the method in Java in some other manner?

Sumit Desai
  • 1,542
  • 9
  • 22
user2358330
  • 377
  • 2
  • 6
  • 17
  • In Java, `char` is a UTF-16 encoded character. In C++, `char` is a byte, and `char *` is most likely a nul-terminated string. – Angew is no longer proud of SO Jun 04 '13 at 09:39
  • Loot at http://electrofriends.com/qna/jni-faq/convert-jstring-cstyle-string-vice-versa/. It should help. – michal Jun 04 '13 at 09:41
  • 1
    At least testhandler doesn't look like a JNI function. Did you miss something we should know? – BlueWanderer Jun 04 '13 at 09:48
  • @Angew I use a Java function signature from C++ to Java as follows: "(IICII)V" Should I change the C to B(Byte) or String? I am not sure how to set signature for string though. – user2358330 Jun 04 '13 at 09:49
  • @BlueWanderer the JNI part is like this:JNIEXPORT void JNICALL Java_otf_OtfJni_otfjni_1set_1handler (JNIEnv *env, jobject obj, jint type, jobject handler) { struct callback *cb; jclass handler_class = (*env)->GetObjectClass(env, handler); cb = (struct callback *)malloc(sizeof(struct callback)); cb->env = env; cb->handler = (*env)->NewGlobalRef(env, handler); cb->id = (*env)->GetMethodID(env, handler_class, "handle", "(IICII)V"); printf("adding a handler\n"); OTF_HandlerArray_setHandler(handlers, testhandler, type); OTF_HandlerArray_setFirstHandlerArg(handlers, (void *)cb, type); – user2358330 Jun 04 '13 at 09:53
  • Im sorry but I dont know how to include the code part here..Im new here :( – user2358330 Jun 04 '13 at 09:53
  • You can edit your post. So basically you pass the function pointer to Java, but how you call the function pointer? – BlueWanderer Jun 04 '13 at 10:02
  • @BlueWanderer the function is called in java using public void handle(int stream, int func, char name, int group, int token); Is this what you meant to ask me? – user2358330 Jun 04 '13 at 10:13
  • I wanted to ask you to post the implementation of JNI function that handles the call and it's Java counter part. – BlueWanderer Jun 04 '13 at 10:41
  • @BlueWanderer The JNIExport function makes a call to testhandler which has the const char*. The JNI code is something like this: `JNIEXPORT void JNICALL Java_otf_OtfJni_otfjni_1set_1handler (JNIEnv *env, jobject obj, jint type, jobject handler){ OTF_HandlerArray_setHandler(handlers, testhandler, type); }` And the testhandler function is: `{ struct callback *cb = (struct callback *)arg; JNIEnv *env = cb->env; (*env)->CallVoidMethod(env, cb->handler, cb->id, stream, func, name, funcgroup, source); return (*env)->NewStringUTF(env, name); }` – user2358330 Jun 04 '13 at 13:26
  • And the Java part is like this: `public interface IOtfHandler { public void handle(long time, int state, int cpuid, int token); public void handle(int stream, int func, String name, int group, int token); }` – user2358330 Jun 04 '13 at 13:27

1 Answers1

1

You can't just pass const char* string through JNI.

Create a jstring from it using function NewStringUTF (it is member-function of class JNIEnv). Then pass it to Java's String.

Tsar Ioann
  • 444
  • 4
  • 9
  • I understand that I have to use **NewStringUTF**, but in my case, the *const char** is part of a function call as seen in testhandler. So shud I use the **NewStringUTF** inside the function call or inside the JNIExport function? – user2358330 Jun 04 '13 at 11:54
  • @user2358330 Could you please give some code parts here for me to understand what do you mean? – Tsar Ioann Jun 04 '13 at 12:23
  • The JNIExport function makes a call to testhandler which has the const char*. The JNI code is something like this: `JNIEXPORT void JNICALL Java_otf_OtfJni_otfjni_1set_1handler (JNIEnv *env, jobject obj, jint type, jobject handler){ OTF_HandlerArray_setHandler(handlers, testhandler, type); }` And the testhandler function is: `{ struct callback *cb = (struct callback *)arg; JNIEnv *env = cb->env; (*env)->CallVoidMethod(env, cb->handler, cb->id, stream, func, name, funcgroup, source); return (*env)->NewStringUTF(env, name); }` – user2358330 Jun 04 '13 at 13:11
  • @user2358330 I can't get the logic. Why `int testhandler` is returning `jstring`? When I was answering the main question, I meant that parameter `name` of type `char` in java is not equivalent to `const char*` from C++. Here you need `NewStringUTF` to convert this parameter to `jstring` and give to java function, which gets `String`. – Tsar Ioann Jun 05 '13 at 12:09
  • @user2358330 I thought of something like this: `env->CallVoidMethod(......., env->NewStringUTF(name), .....)`. – Tsar Ioann Jun 05 '13 at 12:11
  • I have changed the program now. Please see http://stackoverflow.com/questions/16939349/convert-char-to-jstring-in-jni-when-char-is-passed-using-va-arg and let me know if you have any answers for that. Thanks in advance! – user2358330 Jun 05 '13 at 12:18