Search This Blog

Thursday, October 10, 2013

Wednesday, October 9, 2013

Passing jstring to JAVA from C++ - the right way.

hi
i used to use this function
m_env->NewStringUTF(formattedMsg);

and one day i saw that the string i passed in my case it was huge more than 500k, cut in the end.
so i replcae this function with this one ... and its worked !!

jstring WindowsToJstring(JNIEnv* pEnv, LPCTSTR cstr) {
jstring retJstring = NULL;
int slen = strlen(cstr);
int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)cstr, slen, NULL, 0 );
unsigned short* tempbuffer = (unsigned short *)malloc( length*2 + 1 );
MultiByteToWideChar( CP_ACP, 0, (LPCSTR)cstr, slen, (LPWSTR)tempbuffer, length );
retJstring = (pEnv)->NewString((jchar*)tempbuffer, length );
free( tempbuffer );
return retJstring;
}

more details could be found here
http://www.anyang-window.com.cn/between-java-and-c-transmission-through-jni-chinese-string-and-hash-issues/

and
http://stackoverflow.com/questions/8258834/sending-utf-chars-to-java-from-c-using-jni