Problems with pointers and memory adresses

It doesn’t work, because you won’t get access to every location in memory you want. Not every location in memory is valid, you may want to read about Virtual Address Space.

Some addresses are reserved for device drivers and kernel mode operations. Another range of addresses (for example 0xCCCCCCCC and higher) may be reserved for uninitialized pointers.

Even if some location is valid, operating system may still deny access to write to/read from certain location, if that would cause undefined behaviour or violate system safety.

EDIT

I think you might be interested in creating some kind of “GameHack”, that allows you to modify amount of resources, number of units, experience level, attributes or anything.

Memory access is not a simple topic. Different OSes use different strategies to prevent security violations. But many thing can be done here, after all there is a lot software for doing such things.

First of all, do you really need to write your own tool? If you just want some cheating, use ArtMoney – it is a great memory editor, that I have been using for years.

But if you really have to write it manually, you need to do some research first.
On Windows, for example, I would start from these:

ReadProcessMemory

WriteProcessMemory

Also, I am quite certain, that one of possible techniques is to pretend, that you are a debugger:

DebugActiveProcess.

EDIT 2

I have done some research and it looks, that on Windows (I assume this is your platform, since you mentioned gaming; can’t imagine playing anything on crappy Linux), steps required to write another process’ memory are:

1. Enumerate processes: (EnumProcesses)

const size_t MAX_PROC_NUM = 512;

DWORD procIDs[MAX_PROC_NUM] = { 0 };
DWORD idsNum = 0;

if(!EnumProcesses(procIDs, sizeof(DWORD) * MAX_PROC_NUM, &idsNum))
  //handle error here

idsNum /= sizeof(DWORD); //After EnumProcesses(), idsNum contains number of BYTES!

2. Open required process. (OpenProcess,GetModuleFileNameEx)

const char* game_exe_path = "E:\\Games\\Spellforce\\Spellforce.exe"; //Example

HANDLE game_proc_handle = nullptr;
DWORD proc_access = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_VM_WRITE; //read & write memory, query info needed to get .exe name

const DWORD MAX_EXE_PATH_LEN = 1024;

for(DWORD n = 0 ; n < idsNum ; ++idsNum)
{
  DWORD current_id = procIDs[n];
  HANDLE current_handle = OpenProcess(proc_access, false, current_id);

  if(!current_handle)
  {
    //handle error here
    continue;
  }

  char current_path[MAX_EXE_PATH_LEN];

  DWORD length = GetModuleFileNameEx(current_handle, nullptr, current_path, MAX_EXE_PATH_LEN);

  if(length > 0)
  {
    if(strcmp(current_path, game_exe_path) == 0) //that's our game!
    {
      game_proc_handle = current_handle;
      break;
    }
  }

  CloseHandle(current_handle); //don't forget this!
}

if(!game_proc_handle)
  //sorry, game not found

3. Write memory (WriteProcessMemory)

void* pointer = reinterpret_cast<void*>(0x02F70BCC);
int new_value = 5000; //value to be written

BOOL success = WriteProcessMemory(game_proc_handle, pointer, &new_value, sizeof(int), nullptr);

if(success)
  //data successfully written!
else
  //well, that's... em...

This code is written just ‘as is’, but I see no errors, so you can use it as your starting point. I also provided links for all functions I used, so with some additional research (if necessary), you can achieve what you are trying to.

Cheers.

Leave a Comment