-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.h
80 lines (73 loc) · 2.05 KB
/
mem.h
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
75
76
77
78
79
80
#include <windows.h>
#include <TlHelp32.h>
#include <vector>
namespace proc {
DWORD procId = NULL;
HANDLE hProc = nullptr;
DWORD GetProcId(const wchar_t* procName) {
DWORD procId = NULL;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnap != INVALID_HANDLE_VALUE) {
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);
if (Process32First(hSnap, &procEntry)) {
do {
if (!_wcsicmp(procEntry.szExeFile, procName)) {
procId = procEntry.th32ProcessID;
break;
}
} while (Process32Next(hSnap, &procEntry));
}
}
CloseHandle(hSnap);
return procId;
}
uintptr_t GetModuleBaseAddress(DWORD procId, const wchar_t* modName) {
uintptr_t modBaseAddr = NULL;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);
if (hSnap != INVALID_HANDLE_VALUE) {
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);
if (Module32First(hSnap, &modEntry)) {
do {
if (!_wcsicmp(modEntry.szModule, modName)) {
modBaseAddr = (uintptr_t)modEntry.modBaseAddr;
break;
}
} while (Module32Next(hSnap, &modEntry));
}
}
CloseHandle(hSnap);
return modBaseAddr;
}
}
namespace mem {
template<typename T>
T RPM(uintptr_t address) {
T buffer;
ReadProcessMemory(proc::hProc, (LPCVOID)address, &buffer, sizeof(buffer), nullptr);
return buffer;
}
template<typename T>
void WPM(uintptr_t address, T buffer) {
WriteProcessMemory(proc::hProc, (LPVOID)address, &buffer, sizeof(buffer), nullptr);
}
template<typename T>
T ReadChain(uintptr_t base, std::vector<uintptr_t> offsets) {
uintptr_t address = base;
for (unsigned int i = 0; i < offsets.size(); i++) {
address = RPM<uintptr_t>(address);
address += offsets[i];
}
return RPM<T>(address);
}
template<typename T>
void WriteChain(uintptr_t base, std::vector<uintptr_t> offsets, T buffer) {
uintptr_t address = base;
for (unsigned int i = 0; i < offsets.size(); i++) {
address = RPM<uintptr_t>(address);
address += offsets[i];
}
WPM<T>(address, buffer);
}
}