-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.hpp
105 lines (103 loc) · 2.44 KB
/
Memory.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
#include <windows.h>
#include <vector>
#include <Psapi.h>
/// <summary>¶ÁÄÚ´æÄ£°å·â×°</summary>
template<class ClassName>
class Memory :public ClassName
{
public:
Memory():ClassName(){}
~Memory(){}
template<typename T>
T read(DWORD_PTR address) {
T value = T();
readVirtualMemory((ULONG)address, &value, sizeof(T));
return value;
}
template<typename T>
BOOL write(DWORD_PTR dwBaseAddress, T Value)
{
return writeVirtualMemory((ULONG)dwBaseAddress, &Value, sizeof(T));
}
template <typename T>
T readOffset(DWORD_PTR base_address, std::vector<int> offset)
{
DWORD ofset_address = read<DWORD>(base_address);
T value;
for (size_t i = 0; i < offset.size(); i++)
{
if (ofset_address > 0)
{
if ((i + 1) < offset.size())
{
ofset_address = read<DWORD>(ofset_address + offset[i]);
}
else {
value = read<T>(ofset_address + offset[i]);
}
}
}
return value;
}
template <typename T>
bool writeOffset(DWORD_PTR base_address, std::vector<int> offset, T value)
{
DWORD ofset_address = read<DWORD>(base_address);
bool result = false;
for (size_t i = 0; i < offset.size(); i++)
{
if (ofset_address)
{
if ((i + 1) < offset.size())
{
ofset_address = read<DWORD>(ofset_address + offset[i]);
}
else {
result = write<T>(ofset_address + offset[i], value);
}
}
}
return result;
}
std::wstring readWString(DWORD_PTR dwBaseAddress, SIZE_T Size)
{
wchar_t *buffer = new wchar_t[Size];
readVirtualMemory((ULONG)dwBaseAddress, buffer, Size);
std::wstring wstr(buffer, Size);
delete[]buffer;
return wstr;
}
std::string readString(DWORD_PTR dwBaseAddress, SIZE_T Size)
{
char *buffer = new char[Size];
readVirtualMemory((ULONG)dwBaseAddress, buffer, Size);
std::string str(buffer, Size);
delete[]buffer;
return str;
}
std::vector<byte> readBytes(DWORD_PTR dwBaseAddress, SIZE_T Size)
{
std::vector<byte> bytes;
bytes.resize(Size);
byte * buffer = new byte[Size];
readVirtualMemory((ULONG)dwBaseAddress, buffer, Size);
for (size_t i = 0; i < Size; i++)
{
bytes.insert(bytes.end(), buffer[i]);
}
delete[]buffer;
return bytes;
}
BOOL writeBytes(DWORD_PTR dwBaseAddress, std::vector<byte> Bytes)
{
byte * buffer = new byte[Bytes.size()];
BOOL result = TRUE;
for (size_t i = 0; i < Bytes.size(); i++)
{
buffer[i] = Bytes[i];
}
result = writeVirtualMemory((ULONG)dwBaseAddress, buffer, Bytes.size());
delete[]buffer;
return result;
}
};