Originally Posted by
lanman92
How would you use a template like that in c++? I know in C# you use Type.Typecode, what is the equal in c++?
You mean Cyphers example?
Here's the code rewritten with a template:
Code:
template <typename CharT>
std::basic_string<CharT> ReadString(std::uintptr_t address)
{
std::basic_string<CharT> temp;
CharT current;
do
{
SIZE_T bytesRead;
BOOL ec = ReadProcessMemory( hProcess,
reinterpret_cast<LPCVOID>(address),
static_cast<LPVOID>(¤t),
sizeof(current),
&bytesRead);
if(!ec || bytesRead != sizeof(current))
throw std::runtime_error("Error reading string");
if(current)
temp.push_back(current);
address += sizeof(current);
}
while(current);
return temp;
}
And use it like that:
auto string8 = ReadString<char>(0xDEADBEEF); // Read a string with a character size of 8bit, ASCII or UTF8.
auto string16 = ReadString<char16_t>(0xDEADBEEF); // 16bit, probably UTF16.
auto string32 = ReadString<char32_t>(0xDEADBEEF); // 32bit, probably UTF32.
UTFX are only hints, std::basic_string<> doesn't care about encoding and is more or less a dynamic array offering common string operations.