Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert a JString in c++  RSS feed

 
Rikard Qvarforth
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all i dont know how to convert a String to a
char * i am sending to a native c++ function.
this is what i find on the net about this
in c code
*env)->GetStringUTFChars(env, String, 0);
but in c++ env->GetStringUTFChars(String, iscopy)
i been loking for more info on this but i cant find it, when i run my code i get this:
An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION occurred at PC=0x6d447e13
Function name=(N/A)
Library=(N/A)
NOTE: We are unable to locate the function name symbol for the error
just occurred. Please refer to release documentation for possible
reason and solutions.
Current Java thread:
at Pingprocess.ping(Native Method)
at Pingprocess.main(Pingprocess.java:16)
Dynamic libraries:
0x7D2A0000 - 0x7D2CD000 C:\WINDOWS\SYSTEM\DBGHELP.DLL
Local Time = Thu Jan 10 13:09:35 2002
Elapsed Time = 2
#
# The exception above was detected in native code outside the VM
#
# Java VM: Java HotSpot(TM) Client VM (1.3.1-b24 mixed mode)
#
maby i should send down a char array or ?
i now this is a beginner question but i am new to this
thanks
 
Rikard Qvarforth
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello .. i got it with this code but i dont know if i understand it (i have to do the cast )
so if someone java/jni expert could explain in a five year old type of language i would be gratefull thanks !
JNIEXPORT jint JNICALL Java_Pingprocess_ping
(JNIEnv *env, jclass c, jstring adress)
{
const jbyte* argvv =
(jbyte*)env->GetStringUTFChars(adress, NULL);

char* argv =(char *) argvv;
 
Sahir Shibley
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String you pass from java is an array of 16 bit unicode chars. You try to to treat it as a normal ascii char array and you got troubles.
The sample code below demonstrates the implementation of the native method you declare as
public native String Test(String s);
JNIEXPORT jstring JNICALL Java_Test
(JNIEnv* env, jobject jobj, jstring jstrInput)
{
jboolean blnIsCopy;
jstring jstrOutput;
char* strCOut;
const char* strCIn = (env)->GetStringUTFChars(jstrInput , &blnIsCopy);
// convert jstring to a char array
// Do stuff with the char array and and store the result
// in another char array strCOut
(env)->ReleaseStringUTFChars(jstrInput , strCIn); // release jstring

jstrOutput = (env)->NewStringUTF(strCOut); // convert char array to jstring
return jstrOutput;
}
[ January 23, 2002: Message edited by: Sahir Shibley ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!