char*到C#中的字符串

met*_*hod 8 c# pointers

我从一个返回char*指针的本机DLL调用一个函数 ,如何将返回的指针转换为字符串?我试过了 :

char* c = function();
string s = new string(c);
Run Code Online (Sandbox Code Playgroud)

但它只是返回了一个奇怪的汉字,这不是正确的价值c.

Mic*_*Liu 15

也许本机DLL实际上返回的是ANSI字符串而不是Unicode字符串.在这种情况下,请致电Marshal.PtrToStringAnsi:

using System.Runtime.InteropServices;
...
string s = Marshal.PtrToStringAnsi((IntPtr)c);
Run Code Online (Sandbox Code Playgroud)