static string PtrToString(IntPtr ptr, Encoding encoding) { int i = 0; byte b; var bytes = new List<byte>(); while ((b = Marshal.ReadByte(ptr, i)) != 0) { bytes.Add(b); i++; } return encoding.GetString(bytes.ToArray()); }