Environment: 1. C + + DLLs <--> C # DLLs <--> Unity2. C + +, C # file encoding is UTF83. C + + DLL return multibyte string.
Question: 1. Strings received from C + + can be displayed normally in the Unity editor. 2. And the publishing project shows garbled characters. The log file also shows garbled characters. (log file is also in UTF8 format)
Analysis:
1. Whether string marshaling is a problem.
C + + Apisskj_api const char* Sskj_getaudiodevice (void* player, int* Devicelen); C # api[dllimport ("Videoengine")]private static extern IntPtr Sskj_getaudiodevice (IntPtr player, out int strLen);
Using the DllImport default parameters, you can use [DllImport ("Videoengine"), CharSet = CharSet.Ansi] And so on, depending on your needs.
The returned string can also use attribute constraints. [Return:marshalasattribute (UNMANAGEDTYPE.LPSTR)]//Constraint return string
2. Whether the receive conversion method is incorrect.
STRPTR is the address of a string in an unmanaged C++dll, C # needs to be converted into managed memory. byte[] buffer = new Byte[strlen]; Marshal.Copy (strPtr, buffer, 0, strLen);
c++,c# files are UTF8, C + + transmission with multibyte, need to be received with gb2312 (page 936), and then converted to UTF8 (page 65001) to write to the file. Encoding.ascii for Us-ascii (page 20127) cannot accept Chinese, will convert??? try{ Need to have I18N.dll series in Unity Player, otherwise not supportedgetencoding ("gb2312"). Buffer and UTF8 are not the same length byte[] UTF8 = Encoding.convert (encoding.getencoding ("gb2312"), Encoding.UTF8, buffer); str = Encoding.UTF8.GetString (UTF8);} catch (System.Exception arg) { log.info ("System.Exception", Arg. Message); throw;}
From for notes (Wiz)
"Original" solve the problem of untiy using C++dll process, string garbled.