-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryManager.hpp
177 lines (140 loc) · 3.41 KB
/
MemoryManager.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
struct ModuleInfo
{
DWORD dwModBaseAddr, dwModSize;
};
class CMemory
{
private:
DWORD m_dwProcessId;
HANDLE m_hProcess;
public:
std::string m_strProcessName;
DWORD GetProcessId()
{
if (m_strProcessName == NULL)
{
std::cout << "Error: 0" << std::endl;
return NULL;
}
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnap == INVALID_HANDLE_VALUE)
{
std::cout << "Error: 1" << std::endl;
return NULL;
}
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnap, &pEntry))
{
do
{
if (!strcmp(pEntry.szExeFile, m_strProcessName.c_str()))
{
CloseHandle(hSnap);
m_dwProcessId = pEntry.th32ProcessID;
return pEntry.th32ProcessID;
}
} while (Process32Next(hSnap, &pEntry));
}
std::cout << "Error: 2" << std::endl;
CloseHandle(hSnap);
return NULL;
}
HANDLE GetProcessHandle()
{
if (m_strProcessName == NULL)
{
std::cout << "Error: 0" << std::endl;
return NULL;
}
GetProcessId();
if (m_dwProcessId == NULL)
return INVALID_HANDLE_VALUE;
m_hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, m_dwProcessId);
if (m_hProcess == INVALID_HANDLE_VALUE)
return INVALID_HANDLE_VALUE;
return m_hProcess;
}
DWORD GetProcessBaseAddr()
{
if (m_strProcessName == NULL)
{
std::cout << "Error: 0" << std::endl;
return NULL;
}
GetProcessId();
if (m_dwProcessId == NULL)
return NULL;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, m_dwProcessId);
if (hSnap == INVALID_HANDLE_VALUE)
return NULL;
MODULEENTRY32 mEntry;
mEntry.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnap, &mEntry))
{
CloseHandle(hSnap);
return reinterpret_cast<DWORD>(mEntry.modBaseAddr);
}
std::cout << "Error 3" << std::endl;
CloseHandle(hSnap);
return NULL;
}
bool GetModuleInformation(const std::string ModuleName, ModuleInfo& mInfo)
{
if (m_strProcessName == NULL)
{
std::cout << "Error: 0" << std::endl;
return NULL;
}
GetProcessId();
if (m_dwProcessId == NULL)
return NULL;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, m_dwProcessId);
if (hSnap == INVALID_HANDLE_VALUE)
return NULL;
MODULEENTRY32 mEntry;
mEntry.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnap, &mEntry))
{
do
{
if (mEntry.szModule[0] == ModuleName[0] && !strcmp(mEntry.szModule, ModuleName.c_str()))
{
mInfo.dwModBaseAddr = reinterpret_cast<DWORD>(mEntry.modBaseAddr);
mInfo.dwModSize = mEntry.modBaseSize;
CloseHandle(hSnap);
return true;
}
} while (Module32Next(hSnap, &mEntry));
}
std::cout << "Error 3" << std::endl;
CloseHandle(hSnap);
return false;
}
template <class t>
t Read(DWORD dwAddress)
{
t val;
ReadProcessMemory(m_hProcess, (LPVOID)dwAddress, &val, sizeof(t), NULL);
return val;
}
template <class t>
void Write(DWORD dwAddress, t val)
{
WriteProcessMemory(m_hProcess, (LPVOID)dwAddress, &val, sizeof(t), NULL);
}
CMemory()
{
m_strProcessName = NULL;
}
CMemory(const std::string ProcessName)
{
m_dwProcessId = NULL;
m_hProcess = INVALID_HANDLE_VALUE;
m_strProcessName = ProcessName;
GetProcessHandle();
}
};