-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.hpp
74 lines (60 loc) · 2.49 KB
/
memory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
class Memory{
private:
std::uintptr_t processID = 0;
void* processHandle = nullptr;
public:
// Memory constructor to find the processID and opening the handle to the process
Memory(const std::string processName){
::PROCESSENTRY32 processEntry = { };
processEntry.dwSize = sizeof(::PROCESSENTRY32);
const HANDLE processSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
while(::Process32Next(processSnapshot, &processEntry)){
if(!processName.compare(processEntry.szExeFile)){
processID = processEntry.th32ProcessID;
processHandle = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
break;
}
}
if (processSnapshot){
::CloseHandle(processSnapshot);
}
}
// Memory Deconstructor to cleanup the closing process handle to the game memory
~Memory(){
if(processHandle){
::CloseHandle(processHandle);
}
}
const std::uintptr_t getModuleBaseAddress(const std::string moduleName){
::MODULEENTRY32 moduleEntry = { };
moduleEntry.dwSize = sizeof(::MODULEENTRY32);
const auto processSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processID);
std::uintptr_t moduleBaseAddress = 0;
while(::Module32Next(processSnapshot, &moduleEntry)){
if(!moduleName.compare(moduleEntry.szModule)){
moduleBaseAddress = reinterpret_cast<std::uintptr_t>(moduleEntry.modBaseAddr);
break;
}
}
if(processSnapshot){
::CloseHandle(processSnapshot);
}
return moduleBaseAddress;
}
// Read process memory template
template <typename T>
constexpr const T Read(const std::uintptr_t& address){
T value = {};
::ReadProcessMemory(processHandle, reinterpret_cast<const void*>(address), &value, sizeof(T), NULL);
return value;
}
// Write process memory template
template <typename T>
constexpr void Write(const std::uintptr_t& address, const T& value){
::WriteProcessMemory(processHandle, reinterpret_cast<void*>(address), &value, sizeof(T), NULL);
}
};