Need help writing to memory
Hello, im having major issues with using the pointer and offsets provided by the Cheat Engine pointer scan.
I know i need to:
-Get address of exe
-Add it to the base address provided
-Read address in memory
-Add offsets
-Write to memory
I'm using GetLastError() and getting error 487 which is an invalid address error, please help
I know i need to:
-Get address of exe
-Add it to the base address provided
-Read address in memory
-Add offsets
-Write to memory
I'm using GetLastError() and getting error 487 which is an invalid address error, please help

Code:
#include <iostream>
#include <windows.h>
#include <TlHelp32.h>
using namespace std;
DWORD moneyBaseAddress = 0x011FE910;
DWORD moneyOffset1 = 0x40;
DWORD moneyOffset2 = 0x30;
int desiredMoney = 500;
DWORD GetModuleBase(LPSTR lpModuleName, DWORD dwProcessId)
{
MODULEENTRY32 lpModuleEntry = { 0 };
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId);
if (!hSnapShot)
return NULL;
lpModuleEntry.dwSize = sizeof(lpModuleEntry);
BOOL bModule = Module32First(hSnapShot, &lpModuleEntry);
while (bModule)
{
if (!strcmp((const char*)lpModuleEntry.szModule, lpModuleName))
{
CloseHandle(hSnapShot);
return (DWORD)lpModuleEntry.modBaseAddr;
}
bModule = Module32Next(hSnapShot, &lpModuleEntry);
}
CloseHandle(hSnapShot);
return NULL;
}
void ModifyMoneyMemory() {
HWND hWnd = FindWindow(NULL, L"American Truck Simulator");
if (hWnd == 0) {
cout << "Could not find American Truck Simulator Process :(. Please make sure it is running." << endl;
}
else {
DWORD pId;
GetWindowThreadProcessId(hWnd, &pId);
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pId);
if (!hProc) {
cout << "Cannot open process. Make sure this hack is running in administrator." << endl;
}
else {
cout << "Enter desired money amount: " << endl;
cin >> desiredMoney;
DWORD modBase = GetModuleBase("American Truck Simulator", pId);
cout << modBase << endl;
DWORD address = (DWORD)(modBase + 0x011FE910);
ReadProcessMemory(hProc, (LPCVOID)address, &address, sizeof(address), NULL);
address += 0x40 + 0x30;
//VirtualProtectEx(hProc, (LPVOID)addy, 1, PAGE_READWRITE, &oldProtect);
if (WriteProcessMemory(hProc, (LPVOID)address,&desiredMoney, 4, NULL)) {
cout << "Memory successfully written to. Enjoy :)" << endl;
}
else {
cout << "Cannot write to memory :(. Please try again." << " Error: " << GetLastError() << endl;
}
// VirtualProtectEx(hProc, (LPVOID)offsetedMoneyAddress, 1, oldProtect, &oldProtect);
CloseHandle(hProc);
}
}
}
