Wednesday, April 24, 2013

jni jnienv NewString example c c++ java


NewString


jstring NewString(JNIEnv *env, const jchar *unicodeChars, jsize len);

Constructs a new java.lang.String object from an array of Unicode characters.
LINKAGE:
Index 163 in the JNIEnv interface function table.
PARAMETERS of NewString

env: the JNI interface pointer.

unicodeChars: pointer to a Unicode string.

len: length of the Unicode string.

RETURNS of NewString

Returns a Java string object, or NULL if the string cannot be constructed.

THROWS:

OutOfMemoryError: if the system runs out of memory.
Example of NewString

jstring WindowsToJstring(JNIEnv* pEnv, char* 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;
}