"Original" solve the problem of untiy using C++dll process, string garbled.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.