你试试这个
[C#] 纯文本查看 复制代码 public static void onMessage(IntPtr str)
{
int nAnsiLength = Marshal.PtrToStringAnsi(str).Length;
int nUniLength = Marshal.PtrToStringUni(str).Length;
int nMaxLength = (nAnsiLength > nUniLength) ? nAnsiLength : nUniLength;
int length = 0;//循环查找字符串的长度
for (int i = 0; i < nAnsiLength; i++)
{
byte[] strbuf1 = new byte[1];
Marshal.Copy(str + i, strbuf1, 0, 1);
if (strbuf1[0] == 0)
{
break;
}
length++;
}
byte[] strbuf = new byte[length];
Marshal.Copy(str, strbuf, 0, length);
string message = System.Text.UTF8Encoding.UTF8.GetString(strbuf);
} |