I'm currently trying to read a null-terminated ASCII string from another process. To do so, I created a class which acts as a memory reader with a generic readBytes
function which acts as a wrapper for the Windows function ReadProcessMemory
with error handling. My readBytes
function works just fine, however my readAsciiString
function (code below) is throwing Access Violation Errors when creating a new std::string
from a unique_ptr
char array.
std::string ProcessReader::readAsciiString(DWORD_PTR offset, int maxChars)
{
auto buffer = std::make_unique<char[]>(maxChars);
if (readBytes(offset, buffer.get(), maxChars))
{
int pos = 0;
for (; pos < maxChars; pos++)
{
if (buffer[pos] == '')
break;
}
auto returnBuffer = std::make_unique<char[]>(pos);
memcpy(returnBuffer.get(), buffer.get(), pos);
// The program breaks here reporting an Access Violation
// trying to writing to offset 0x00000000FFFFFFFF
return std::string(returnBuffer.get());
}
return std::string("");
}
Here is a screenshot of the code in Visual Studio with a breakpoint on the return line while watching both buffer
and returnBuffer
. Resuming the program causes it to crash on that very line.
I'm relatively new to C++ and am struggling to see what I'm doing wrong. Am I approaching this the wrong way?
Aucun commentaire:
Enregistrer un commentaire