This repository has been 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
/
Memory.cpp
69 lines (59 loc) · 1.56 KB
/
Memory.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
#pragma once
#include "Memory.hpp"
#include <Windows.h>
#include <TlHelp32.h>
#include <string>
#include <algorithm>
// Returns first PID found from process name
uint32_t Memory::getProcessId(const std::string& processName)
{
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 entry;
entry.dwSize = sizeof(entry);
do
{
if (!strcmp(entry.szExeFile, processName.c_str()))
return entry.th32ProcessID;
} while (Process32Next(snapshot, &entry));
CloseHandle(snapshot);
return 0;
}
// Attach memory manager to process
bool Memory::attachToProcess(const std::string& processName)
{
PID = getProcessId(processName);
// Simply open a handle to target
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);
return hProcess;
}
// Finds base address of module name
uint32_t Memory::getModule(const char * moduleName)
{
HANDLE module = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, PID);
MODULEENTRY32 moduleEntry;
moduleEntry.dwSize = sizeof(moduleEntry);
do
{
if (!strcmp(moduleEntry.szModule, moduleName))
{
CloseHandle(module);
return (uint32_t)moduleEntry.modBaseAddr;
}
} while (Module32Next(module, &moduleEntry));
CloseHandle(module);
moduleEntry.modBaseAddr = 0;
return (uint32_t)moduleEntry.modBaseAddr;
}
Memory::Memory()
{
this->hProcess = 0;
this->PID = 0;
if (!attachToProcess(TARGET_PROCESS))
return;
this->clientDll = getModule(CLIENT_DLL_NAME);
this->engineDll = getModule(ENGINE_DLL_NAME);
}
Memory::~Memory()
{
CloseHandle(this->hProcess);
}