How can I convert an unmanaged IntPtr type to a c# string?

白昼怎懂夜的黑 提交于 2019-11-28 11:56:40

Marshal.PtrToStringAuto Method (IntPtr)

Allocates a managed String and copies all characters up to the first null character from a string stored in unmanaged memory into it.

The above answer was great and it nearly solved the same issue for me but... I got what looks like Chinese characters back from the method (潆湵⁤瑡氠慥瑳漠敮爠灥慥整⁤浩条⁥慮敭›䌢⸢). What I had to do was use the Marshal.PtrToStringAnsi(IntPtr) method as described here: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.ptrtostringansi.aspx and here: http://answers.unity3d.com/questions/555441/unitys-simplest-plugin-print-does-not-work.html.

Once I made the change, my String was in English once more. Not sure why that was happening, but there ya go!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!