Conversion of STRING, Writeprocessmemory()

Asked

Viewed 121 times

0

I have the following situation, the user must enter a memory address:

(I’m using this method, may not be correct for the following situation)

std::string TEMP_MEMORY_ADDRESS;
std::string MEMORY_ADDRESS = "0x";
std::cout << "Digite o endereco de Memoria: ";
std::getline(std::cin, TEMP_MEMORY_ADDRESS);
MEMORY_ADDRESS.append(TEMP_MEMORY_ADDRESS);

Beauty, now we have the memory address requested by the user and in Hexadecimal, the problem is: I have to play this address in the function Writeprocessmemory(), and according to the MICROSOFT this address must be in the following parameters:

BOOL WINAPI WriteProcessMemory(

      _In_   HANDLE hProcess,
      _In_   LPVOID lpBaseAddress,
      _In_   LPCVOID lpBuffer,
      _In_   SIZE_T nSize,
      _Out_  SIZE_T *lpNumberOfBytesWritten
    );

lpBaseAddress[in] = A pointer to the specified process base address where data is recorded

In this question, my question only goes to LPVOID lpBaseAddress which is where we need to fit the memory address typed by the user, and that’s where my problem comes in: The method I’m using is not working when I play the string in lpBaseAddress.

EXAMPLE:

int isSuccessful = WriteProcessMemory(hproc, (LPVOID)MEMORY_ADDRESS.c_str(), &NewValue, (DWORD)sizeof(NewValue), NULL);

Trying to play on the output dice what had on (LPVOID)MEMORY_ADDRESS.c_str() I realized it had a random value, and it wasn’t exactly the address I had provided.

seems yes a silly doubt, but I’m having difficulties in how I should provide the exact value of the memory address for the function Writeprocessmemory();

1 answer

1


You need to convert the hexadecimal string typed by the user into an integer before moving to the function. The way you did, what’s being passed is address of the hexadecimal string and not the value correspondent.

To convert the Hex string to decimal you can use the function stoul.

Try it like this:

unsigned long mem_addr = std::stoul(MEMORY_ADDRESS, NULL, 16);
int isSuccessful = WriteProcessMemory(hproc, (LPVOID)mem_addr, &NewValue, (DWORD)sizeof(NewValue), NULL);

PS.: I’m a little rusty on Winapi and C++. According to the researches and tests I did here it looks like it will work, but I had no way to test.

  • Thanks a lot, buddy, it seems silly, but I help a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.