I encounter a problem that the program cannot find the game named MAT.exe What should
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <iostream>
#include <TlHelp32.h>
#include <random>
using namespace std;
DWORD dwGetModuleBaseAddress(DWORD dwProcessID, LPCTSTR lpszModuleName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessID);
DWORD dwModuleBaseAddress = 0;
if (hSnapshot != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 ModuleEntry32 = { 0 };
ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnapshot, &ModuleEntry32))
{
do
{
if (_tcscmp(ModuleEntry32.szModule, lpszModuleName) == 0)
{
dwModuleBaseAddress = (DWORD_PTR)ModuleEntry32.modBaseAddr;
break;
}
} while (Module32Next(hSnapshot, &ModuleEntry32));
}
CloseHandle(hSnapshot);
}
return dwModuleBaseAddress;
}
DWORD readPointerChain(HANDLE phandle, DWORD base, int* offsets, size_t numOffsets)
{
DWORD addr = base;
SIZE_T read;
for (size_t i = 0; i < numOffsets; i++)
{
BOOL success = ReadProcessMemory(phandle, (void*)addr, &addr, sizeof(addr), &read);
if (!success) { cout << " failed to read " << hex << addr; return 0; }
addr += offsets[i];
}
return addr;
}
int main()
{
HWND hwnd = FindWindow(NULL, _T("MAT"));
if (!hwnd) { printf("Start the game!\n"); cin.get(); return 1; }
DWORD pid;
GetWindowThreadProcessId(hwnd, &pid);
DWORD moduleBase = dwGetModuleBaseAddress(pid, _T("MAT"));
long base = moduleBase + 0x1FD660;
int offsets[] = { 0xC,0x14,0, 0x18 };
HANDLE phandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_VM_WRITE, 0, pid);
if (!phandle) { printf("Failed to open!\n"); cin.get(); return 1; }
DWORD addr = readPointerChain(phandle, base, offsets, sizeof(offsets) / sizeof(*offsets));
if (!addr) { cout << "Failed to follow path" << endl; cin.get(); return 1; }
DWORD val, read;
BOOL success = ReadProcessMemory(phandle, (void*)addr, &val, sizeof(val), &read);
cout << "val at " << hex << addr << " is " << dec << val << endl;
//
http://www.cplusplus.com/reference/random/
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(100, 1000);
int dice_roll = distribution(generator);
val = val + dice_roll;
WriteProcessMemory(phandle, (void*)addr, &val, sizeof(val), &read);
success = ReadProcessMemory(phandle, (void*)addr, &val, sizeof(val), &read);
cout << "val at " << hex << addr << " is now " << dec << val << endl;
cin.get();
return 0;
}