Wednesday, April 24, 2013

jni jnienv GetStringChars example c c++ java


GetStringChars


const jchar * GetStringChars(JNIEnv *env, jstring string,
jboolean *isCopy);

Returns a pointer to the array of Unicode characters of the string. This pointer is valid until ReleaseStringchars() is called.

If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made; or it is set to JNI_FALSE if no copy is made.
LINKAGE:
Index 165 in the JNIEnv interface function table.
PARAMETERS of GetStringChars

env: the JNI interface pointer.

string: a Java string object.

isCopy: a pointer to a boolean.

RETURNS of GetStringChars

Returns a pointer to a Unicode string, or NULL if the operation fails.
Example of GetStringChars
std::wstring JavaToWSZ(JNIEnv* env, jstring string)
{
    std::wstring value;
    if (string == NULL) {
        return value; // empty string
    }
    const jchar* raw = env->GetStringChars(string, NULL);
    if (raw != NULL) {
        jsize len = env->GetStringLength(string);
        value.assign(raw, len);
        env->ReleaseStringChars(string, raw);
    }
    return value;
}