-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_manager.cpp
106 lines (95 loc) · 2.89 KB
/
memory_manager.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "memory_manager.h"
/*Default constructor, not attached to any process
*/
CMemoryManager::CMemoryManager() {
m_hProcess = INVALID_HANDLE_VALUE;
m_dwProcessId = 0;
m_Modules.clear();
}
/*Constructor that attaches to a specific process
@param strProcessName the name of the process, default "csgo.exe"
*/
CMemoryManager::CMemoryManager(const std::string& strProcessName) {
m_hProcess = INVALID_HANDLE_VALUE;
m_dwProcessId = 0;
m_Modules.clear();
// attach and throw if the function failed so we can manage the fail
if (!Attach(strProcessName)) {
throw 20;
}
}
HANDLE CMemoryManager::GetHandle() {
return m_hProcess;
}
DWORD CMemoryManager::GetProcId() {
return m_dwProcessId;
}
std::vector<MODULEENTRY32> CMemoryManager::GetModules() {
return m_Modules;
}
/*Attach to a process
@param strProcessName the name of the process
@return true on success, false on failure
*/
bool CMemoryManager::Attach(const std::string& strProcessName) {
// remember to close this later
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnapshot == INVALID_HANDLE_VALUE) return false;
PROCESSENTRY32 ProcEntry;
ProcEntry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &ProcEntry)) {
// make sure to enable multi-byte character set in project configuration
if (!strcmp(ProcEntry.szExeFile, strProcessName.c_str())) {
// process has been found, now open a handle to it and return it
CloseHandle(hSnapshot);
// PROCESS_ALL_ACCESS grants read/write privileges
m_hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcEntry.th32ProcessID);
m_dwProcessId = ProcEntry.th32ProcessID;
return true;
}
else {
// first process is not the correct one, now loop through the rest of the processes
while (Process32Next(hSnapshot, &ProcEntry))
{
// We do the same check we did for Process32First
if (!strcmp(ProcEntry.szExeFile, strProcessName.c_str()))
{
m_hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcEntry.th32ProcessID);
m_dwProcessId = ProcEntry.th32ProcessID;
return true;
}
}
}
}
CloseHandle(hSnapshot);
return false;
}
bool CMemoryManager::GrabModule(const std::string& strModuleName) {
// basically the same structure as Attach()
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, m_dwProcessId);
if (hSnapshot == INVALID_HANDLE_VALUE) return false;
MODULEENTRY32 ModEntry;
ModEntry.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnapshot, &ModEntry)) {
if (!strcmp(ModEntry.szModule, strModuleName.c_str())) {
CloseHandle(hSnapshot);
m_Modules.push_back(ModEntry);
return true;
}
else {
while (Module32Next(hSnapshot, &ModEntry)) {
if (!strcmp(ModEntry.szModule, strModuleName.c_str())) {
CloseHandle(hSnapshot);
m_Modules.push_back(ModEntry);
return true;
}
}
}
}
else {
CloseHandle(hSnapshot);
return false;
}
CloseHandle(hSnapshot);
return false;
}