Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I call Java Methods containing String parameter(s) using JNI?

0
Posted

How can I call Java Methods containing String parameter(s) using JNI?

0

Created: Oct 11, 2000 Author: Davanum Srinivas (http://www.jguru.com/guru/viewbio.jsp?EID=2011) CallObjectMethod/CallStaticObjectMethod can be used to call Java Methods which need String parameters. Additional JNI functions are needed for converting Java strings to C/C++ strings or the other way around. For example: char result[256]; // Call a java method which returns a string. jmethodID midToString = env->GetMethodID(cls, “toString”, “() Ljava/lang/String;”); jstring jstr = (jstring) env->CallObjectMethod(jObj, midToString); if (jstr != 0) { // Now convert the Java String to C++ char array const char* cstr = env->GetStringUTFChars(jstr, 0); strcpy(result, cstr); env->ReleaseStringUTFChars(jstr, cstr); env->DeleteLocalRef(jstr); } // Convert the C++ char array to Java String jstring name = env->NewStringUTF(result); // Now use this to call a java method that needs a String parameter.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123