forked from ishare2121/DofProject
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemory.h
More file actions
205 lines (137 loc) · 5 KB
/
Copy pathMemory.h
File metadata and controls
205 lines (137 loc) · 5 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/***************************************************************************************************
Module: Memory.h
Author: Kotori Baby @ 2016
***************************************************************************************************/
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __MEMORY_H__
#define __MEMORY_H__
////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _WINDLL
static_assert(false, "DLL-Only");
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <Windows.h>
#include <initializer_list>
#include <algorithm>
// Windows Í·Îļþ
#include <unordered_map>
#include <filesystem>
////////////////////////////////////////////////////////////////////////////////////////////////////
class Memory {
public:
Memory() {}
~Memory() {}
private:
Memory(const Memory&);
Memory& operator=(const Memory&);
public:
template<typename Type>
Type Read(uintptr_t address) const {
auto real_address = reinterpret_cast<Type*>(address);
Type result = {};
DWORD old_protect = 0;
::VirtualProtect(real_address, sizeof(Type), PAGE_EXECUTE_READWRITE, &old_protect);
__try {
result = *real_address;
}
__except (::GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
::VirtualProtect(real_address, sizeof(Type), old_protect, &old_protect);
return 0;
}
::VirtualProtect(real_address, sizeof(Type), old_protect, &old_protect);
return result;
}
template<typename Type>
void Write(uintptr_t address, Type value) const {
auto real_address = reinterpret_cast<Type*>(address);
DWORD old_protect = 0;
::VirtualProtect(real_address, sizeof(Type), PAGE_EXECUTE_READWRITE, &old_protect);
__try {
*real_address = value;
}
__except (::GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
::VirtualProtect(real_address, sizeof(Type), old_protect, &old_protect);
return;
}
::VirtualProtect(real_address, sizeof(Type), old_protect, &old_protect);
}
template<typename Type>
Type Read(std::initializer_list<uintptr_t> offsets) const {
if (offsets.size() == 0) {
return 0;
}
return Read<Type>(GetFinalAddress(offsets));
}
template<typename Type>
void Write(std::initializer_list<uintptr_t> offsets, Type value) const {
if (offsets.size() == 0) {
return;
}
Write<Type>(GetFinalAddress(offsets), value);
}
bool Reads(uintptr_t address, void* buffer, size_t size) const {
auto real_address = reinterpret_cast<void*>(address);
DWORD old_protect = 0;
::VirtualProtect(real_address, size, PAGE_EXECUTE_READWRITE, &old_protect);
__try {
::RtlCopyMemory(buffer, real_address, size);
}
__except (::GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
::VirtualProtect(real_address, size, old_protect, &old_protect);
return false;
}
::VirtualProtect(real_address, size, old_protect, &old_protect);
return true;
}
bool Writes(uintptr_t address, const void* buffer, size_t size) const {
auto real_address = reinterpret_cast<void*>(address);
DWORD old_protect = 0;
::VirtualProtect(real_address, size, PAGE_EXECUTE_READWRITE, &old_protect);
__try {
::RtlCopyMemory(real_address, buffer, size);
}
__except (::GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
::VirtualProtect(real_address, size, old_protect, &old_protect);
return false;
}
::VirtualProtect(real_address, size, old_protect, &old_protect);
return true;
}
bool Reads(std::initializer_list<uintptr_t> offsets, void* buffer, size_t size) const {
if (offsets.size() == 0) {
return false;
}
return Reads(GetFinalAddress(offsets), buffer, size);
}
bool Writes(std::initializer_list<uintptr_t> offsets, const void* buffer, size_t size) const {
if (offsets.size() == 0) {
return false;
}
return Writes(GetFinalAddress(offsets), buffer, size);
}
uintptr_t GetFinalAddress(std::initializer_list<uintptr_t> offsets) const {
uintptr_t base = 0;
__try {
std::for_each(offsets.begin(), offsets.end() - 1, [&base](const uintptr_t& offset) {
auto address = reinterpret_cast<uintptr_t*>(base + offset);
DWORD old_protect = 0;
::VirtualProtect(address, sizeof(uintptr_t), PAGE_EXECUTE_READWRITE, &old_protect);
base = *address;
::VirtualProtect(address, sizeof(uintptr_t), old_protect, &old_protect);
});
}
__except (::GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
return 0;
}
return base + *(offsets.end() - 1);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif
//////////////////////////////////////////// End Of File ///////////////////////////////////////////