This repository was archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMemoryManager.cpp
More file actions
101 lines (86 loc) · 2.62 KB
/
MemoryManager.cpp
File metadata and controls
101 lines (86 loc) · 2.62 KB
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
#include "MemoryManager.h"
#include <iostream>
bool getPID(const char* ProcessName, DWORD& PID)
{
HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 ProcEntry;
ProcEntry.dwSize = sizeof(ProcEntry);
do
if (!strcmp((const char*)ProcEntry.szExeFile, ProcessName))
{
PID = ProcEntry.th32ProcessID;
CloseHandle(hPID);
return true;
}
while (Process32Next(hPID, &ProcEntry));
return false;
}
void grabHandle(DWORD PID, HANDLE& processHandle)
{
std::cout << "PID " << (int)PID << std::endl;
std::cout << "Grabbing handle.." << std::endl;
processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);
if (processHandle == INVALID_HANDLE_VALUE || !processHandle) {
std::cerr << "Failed to open process -- invalid handle!" << std::endl;
std::cerr << "Error code: " << GetLastError() << std::endl;
return;
}
else {
std::cout << "Successfully grabbed handle.." << std::endl;
}
}
MemoryManager::MemoryManager()
{
getPID(BATTLERITE_EXE.c_str(), PID);
handle = NULL;
grabHandle(PID, handle);
HANDLE moduleSnapshotHandle_ = INVALID_HANDLE_VALUE;
moduleSnapshotHandle_ = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, PID);
if (moduleSnapshotHandle_ == INVALID_HANDLE_VALUE)
{
std::cout << "Module Snapshot error" << std::endl;
return;
}
/* Size the structure before usage */
MODULEENTRY32 moduleEntry_;
moduleEntry_.dwSize = sizeof(MODULEENTRY32);
/* Retrieve information about the first module */
if (!Module32First(moduleSnapshotHandle_, &moduleEntry_))
{
std::cout << "First module not found" << std::endl;
CloseHandle(moduleSnapshotHandle_);
return;
}
/* Get base addresses of modules */
Battlerite_Base = NULL;
MonoDll_Base = NULL;
while (!Battlerite_Base || !MonoDll_Base)
{
/* Find module of the executable */
do
{
if (!strcmp((const char*)moduleEntry_.szModule, BATTLERITE_EXE.c_str()))
{
Battlerite_Base = (unsigned int)moduleEntry_.modBaseAddr;
Battlerite_Size = (unsigned int)moduleEntry_.modBaseSize;
}
if (!strcmp((const char*)moduleEntry_.szModule, MONO_DLL.c_str()))
{
MonoDll_Base = (unsigned int)moduleEntry_.modBaseAddr;
MonoDLL_Size = (unsigned int)moduleEntry_.modBaseSize;
}
} while (Module32Next(moduleSnapshotHandle_, &moduleEntry_));
if (!Battlerite_Base)
{
std::cout << "Failed to find module " << BATTLERITE_EXE << std::endl;
Sleep(200);
}
else if (!MonoDll_Base)
{
std::cout << "Failed to find module " << MONO_DLL << std::endl;
Sleep(200);
}
}
std::cout << BATTLERITE_EXE << " = " << Battlerite_Base << std::endl;
std::cout << MONO_DLL << " = " << MonoDll_Base << std::endl;
}