Converting STRING, WriteProcessMemory ()

0

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

(I'M USING THIS METHOD, IT 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, we now have the memory address requested by the user and in Hexadecimal, the problem is: I have to throw this address in the WriteProcessMemory () function, and according to 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 base address in the specified process where data is written

In this question, my question goes only to LPVOID lpBaseAddress which is where we need to embed the memory address entered by the user, and that's where my problem comes in: The method I am 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 in the data output what I had in (LPVOID) MEMORY_ADDRESS.c_str () I noticed that it had a random value, and it was not exactly the address I had provided.

It seems like a silly question, but I'm having difficulty in how I should provide the exact value of the memory address for the WriteProcessMemory () function     

asked by anonymous 12.02.2015 / 20:03

1 answer

1

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

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

Try 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 bit rusty in WinAPI and C ++. According to the research and testing I did here it seems like it will work, but I had no way to test it.

    
12.02.2015 / 21:09