title: C++/CLI UTF-8 & JNI Not Converting Unicode String Properly
body: <p>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 &amp; 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). </p>
  
  <pre><code>        String^ JStringToCliString(const jstring string){
          String^ converted = gcnew String("");
          JNIEnv* envLoc = GetJniEnvHandle();
          std::wstring value;
          jboolean isCopy;
  
          if(string){
              try{
                  jsize len = env-&gt;GetStringLength(string);
                  if(Environment::OSVersion-&gt;Version-&gt;Major &gt;= 6) // 6 is post XP/2003                                        
                  {
                      TraceLog::Log("Using GetStringChars() for string conversion");
                      const jchar* raw = envLoc-&gt;GetStringChars(string, &amp;isCopy);
                      // todo add exception handling here for jvm
                      if (raw != NULL) {                          
                          value.assign(raw, raw + len);
                          converted = gcnew String(value.c_str());
                          env-&gt;ReleaseStringChars(string, raw);
                      }
                  }else{
                      TraceLog::Log("Using GetStringUTFChars() for string conversion.");
                      const char* raw = envLoc-&gt;GetStringUTFChars(string, &amp;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-&gt;ReleaseStringUTFChars(string, raw);
                      }                       
                  }
              }catch(Exception^ ex){
                  TraceLog::Log(ex-&gt;Message);
              }
          }
          return converted;
      }
  </code></pre>