-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallhack.cpp
78 lines (57 loc) · 2.16 KB
/
wallhack.cpp
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
#include "wallhack.h"
MemAccessException::MemAccessException(const string& msg) : message(msg) {}
const char* MemAccessException::what() const noexcept {
return message.c_str();
}
void Wallhack::setStateFromMemory() {
ReadProcessMemory(hProcess, (void*)WH_ADDRESS, &state, sizeof(state), nullptr);
}
short Wallhack::getNextState() {
return state == ENABLE_WALLHACK ? DISABLE_WALLHACK : ENABLE_WALLHACK;
}
void Wallhack::changeDrawMode(short mode, short* statePtr) {
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQueryEx(hProcess, (LPVOID)WH_ADDRESS, &mbi, sizeof(mbi)) == 0) {
throw MemAccessException("Error: invalid address " + to_string(WH_ADDRESS) + ". Code: " + to_string(GetLastError()));
}
if (mbi.State != MEM_COMMIT) {
throw MemAccessException("Error: memory is not committed");
}
DWORD oldProtect;
if (VirtualProtectEx(hProcess, (LPVOID)WH_ADDRESS, sizeof(mode), PAGE_READWRITE, &oldProtect)) {
if (!WriteProcessMemory(hProcess, (LPVOID)WH_ADDRESS, &mode, sizeof(mode), nullptr)) {
throw MemAccessException("Error: cannot write value in memory. Code: " + to_string(GetLastError()));
}
*statePtr = mode;
} else {
throw MemAccessException("Error: cannot unprotect memory. Code: " + to_string(GetLastError()));
}
VirtualProtectEx(hProcess, (LPVOID)WH_ADDRESS, sizeof(mode), oldProtect, &oldProtect);
}
Wallhack::Wallhack(const string& gameWindowName) : gameWindowName(gameWindowName) {}
void Wallhack::init() {
hWindow = FindWindowA(nullptr, gameWindowName.c_str());
if (hWindow != nullptr) {
GetWindowThreadProcessId(hWindow, &pid);
hProcess = OpenProcess(PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, pid);
setStateFromMemory();
}
}
bool Wallhack::isAvailable() {
return hWindow != nullptr;
}
bool Wallhack::isActive() {
return state == ENABLE_WALLHACK;
}
bool Wallhack::isFlashing() {
return flashing;
}
void Wallhack::toggle() {
changeDrawMode(getNextState(), &state);
}
void Wallhack::toggleFlashing() {
if (state == ENABLE_WALLHACK) {
changeDrawMode(getNextState(), &state);
}
flashing = !flashing;
}