Hi,
I currently need to read a std::string from memory, so I came up with the following code:
It works for small/bigger strings, however I've been wondering if there is a more efficient way to achieve this or a better way to write this without the hard-coded offsets for example.Code:internal static string ReadStdString(IntPtr address) { var length = Marshal.ReadInt32(address + 20); if (length <= 0) { return string.Empty; } var capacity = Marshal.ReadInt32(address + 24); var startStr = address + 4; if (capacity > 15) { startStr = Marshal.ReadIntPtr(address + 4); } var result = new StringBuilder(length); for (var i = 0; i < length; i++) { result.Append((char)Marshal.ReadByte(startStr, i)); } return result.ToString(); }
Thanks for any answers/advice...