-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_manager.h
67 lines (52 loc) · 1.98 KB
/
memory_manager.h
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
#pragma once
#include <string>
#include <Windows.h>
#include <Psapi.h>
#include <TlHelp32.h>
namespace forceinline {
class memory_manager {
public:
memory_manager() { }
memory_manager(std::string_view process);
~memory_manager();
MODULEENTRY32 get_module_entry(std::string_view module);
std::uintptr_t get_module_base(std::string_view module);
std::uintptr_t find_pattern(std::string_view module, std::string_view pattern);
std::uintptr_t find_pattern(std::uintptr_t module_begin, std::size_t module_size, std::string_view pattern);
bool is_attached();
template < typename T >
T read(std::uintptr_t address) {
T temp_val;
ReadProcessMemory(m_proc_handle, reinterpret_cast<LPCVOID>(address), &temp_val, sizeof T, nullptr);
return temp_val;
}
/*
Reads custom length.
Example: you want to read a float[ 64 ].
you'd use this as follows:
read_ex< float >( float_arr_ptr, 0xDEADBEEF, 64 )
*/
template < typename T >
void read_ex(T* out_object_ptr, std::uintptr_t address, std::size_t object_count) {
ReadProcessMemory(m_proc_handle, reinterpret_cast<LPCVOID>(address), out_object_ptr, sizeof T * object_count, nullptr);
}
template < typename T >
bool write(std::uintptr_t address, T value) {
SIZE_T bytes_written;
WriteProcessMemory(m_proc_handle, reinterpret_cast<LPVOID>(address), &value, sizeof T, &bytes_written);
return bytes_written == sizeof T;
}
//See read_ex
template < typename T >
bool write_ex(T* object_ptr, std::uintptr_t address, std::size_t object_count) {
SIZE_T bytes_written;
WriteProcessMemory(m_proc_handle, reinterpret_cast<LPCVOID>(address), object_ptr, sizeof T * object_count, &bytes_written);
return bytes_written == sizeof T * object_count;
}
std::uintptr_t operator[ ](std::string_view module);
private:
void attach_to_process(std::string_view process);
HANDLE m_proc_handle = nullptr;
std::uintptr_t m_proc_id = 0;
};
}