Search code examples
c++c++-clijava-native-interface

C++/CLI UTF-8 & JNI Not Converting Unicode String Properly


I have a Java class that returns a unicode string... Java has the correct version of the string but when it comes through a JNI wrapper in the form of a jstring it must be converted over to a C++ or C++/CLI string. Here is some test code I have which actually works on most languages except for the asian char sets. Chinese Simplified & Japanese characters are garbled and I can't figure out why. Here is the code snippet, I don't see anything wrong with either methods of conversion (the if statement checks os as I have two VMs with diff OS's and runs the appropriate conversion method).

        String^ JStringToCliString(const jstring string){
        String^ converted = gcnew String("");
        JNIEnv* envLoc = GetJniEnvHandle();
        std::wstring value;
        jboolean isCopy;

        if(string){
            try{
                jsize len = env->GetStringLength(string);
                if(Environment::OSVersion->Version->Major >= 6) // 6 is post XP/2003                                        
                {
                    TraceLog::Log("Using GetStringChars() for string conversion");
                    const jchar* raw = envLoc->GetStringChars(string, &isCopy);
                    // todo add exception handling here for jvm
                    if (raw != NULL) {                          
                        value.assign(raw, raw + len);
                        converted = gcnew String(value.c_str());
                        env->ReleaseStringChars(string, raw);
                    }
                }else{
                    TraceLog::Log("Using GetStringUTFChars() for string conversion.");
                    const char* raw = envLoc->GetStringUTFChars(string, &isCopy);
                    if(raw) {
                        int bufSize = MultiByteToWideChar(CP_UTF8, 0 , raw , -1, NULL , 0 );
                        wchar_t* wstr = new wchar_t[bufSize];
                        MultiByteToWideChar( CP_UTF8 , 0 , raw , -1, wstr , bufSize );
                        String^ val = gcnew String(wstr);                           
                        delete[] wstr;

                        converted = val; // partially working
                        envLoc->ReleaseStringUTFChars(string, raw);
                    }                       
                }
            }catch(Exception^ ex){
                TraceLog::Log(ex->Message);
            }
        }
        return converted;
    }

Solution

  • Answer was to enable east asian languages in Windows XP as Win7 + Later work fine. Super easy.... waste of a entire day lol.