unicode - Encoding fails when marshalling string via COM Interop in C# (double UTF8 encoding?) -
i'm writing plugin autodesk navisworks, trying pass c# unicode string property on com object. however, string encoded incorrectly somewhere in process.
var property = ...; property.name = "中文"; // becomes "??" property.value = "中文"; // ok "中文" comes out "??" in user interface, whereas strings limited ascii work fine (e.g. "abcd"). furthermore, setting value-property (a variant) on same object works fine, not name.
further exploration leads me try encoding string "ä" utf-8:
c3 a4 and somehow "encoding" (unicode) string:
property.name = "\u00c3\u00a4"; // shows "ä" surprisingly seemed work.
this led me try following:
var bytes = encoding.utf8.getbytes("中文abcd"); char[] chars = new char[bytes.length]; for(int = 0; < chars.length; i++) chars[i] = (char)bytes[i]; string s = new string(chars); however, when use trying encode "中文abcd" first character "中" in gui. yet, "äabcd" more 1 character again...
what happening here? how can around problem? marshalling problem (e.g. incorrectly specified encoding in com interop)? or perhaps weird code inside application? if it's marshalling problem, can modify property only?
turns out name "internal" string, , should have used property username text displayed in gui.
i.e. changed:
var property = ...; property.name = "中文"; // becomes "??" property.value = "中文"; // ok to this:
var property = ...; property.username = "中文"; // ok! property.value = "中文"; // ok which worked. presumably username implicitly set name internally in way ignoring or mishandling encoding.
Comments
Post a Comment