1- #pragma once
1+ #pragma once
22#include < windows.h>
33#include < initializer_list>
44#include < cstring>
55#include < memory>
66
7- // 使用更独特的节区名称,避免冲突
7+ // 使用更独特的节区名称,避免冲突
88#define CODE_PATCH_SECTION " .codepatch"
99#pragma section(CODE_PATCH_SECTION, read, execute)
1010
11- // 指令结构体
11+ // 指令结构体
1212#pragma pack(push, 1)
1313struct FarJumpInstruction
1414{
1515 BYTE opcode; // 0xE9 for near jump, 0xEA for far jump
16- DWORD offset; // 跳转偏移量
16+ DWORD offset; // 跳转偏移量
1717
1818 FarJumpInstruction (DWORD from, DWORD to)
1919 {
@@ -25,7 +25,7 @@ struct FarJumpInstruction
2525struct NearCallInstruction
2626{
2727 BYTE opcode; // 0xE8
28- DWORD offset; // 调用偏移量
28+ DWORD offset; // 调用偏移量
2929
3030 NearCallInstruction (DWORD from, DWORD to)
3131 {
@@ -36,18 +36,18 @@ struct NearCallInstruction
3636
3737struct ExtendedCallInstruction
3838{
39- WORD prefix; // 特殊调用前缀
40- DWORD offset; // 调用偏移量
39+ WORD prefix; // 特殊调用前缀
40+ DWORD offset; // 调用偏移量
4141
4242 ExtendedCallInstruction (DWORD from, DWORD to)
4343 {
44- prefix = 0x15FF ; // FF15 操作码
44+ prefix = 0x15FF ; // FF15 操作码
4545 offset = to;
4646 }
4747};
4848#pragma pack(pop)
4949
50- // 内存保护封装类
50+ // 内存保护封装类
5151class MemoryGuard
5252{
5353private:
@@ -73,19 +73,19 @@ class MemoryGuard
7373 }
7474 }
7575
76- // 禁用复制
76+ // 禁用复制
7777 MemoryGuard (const MemoryGuard&) = delete ;
7878 MemoryGuard& operator =(const MemoryGuard&) = delete ;
7979};
8080
81- // 节区信息结构
81+ // 节区信息结构
8282struct SectionInfo
8383{
8484 void * virtualAddress;
8585 size_t virtualSize;
8686};
8787
88- // 主补丁类
88+ // 主补丁类
8989#pragma pack(push, 1)
9090class CodeModifier
9191{
@@ -95,14 +95,14 @@ class CodeModifier
9595 const BYTE* m_patchData;
9696
9797public:
98- // 默认构造函数
98+ // 默认构造函数
9999 CodeModifier () : m_targetOffset(0 ), m_dataSize(0 ), m_patchData(nullptr ) {}
100100
101- // 参数化构造函数
101+ // 参数化构造函数
102102 CodeModifier (DWORD offset, DWORD size, const BYTE* data)
103103 : m_targetOffset(offset), m_dataSize(size), m_patchData(data) {}
104104
105- // 应用补丁到内存
105+ // 应用补丁到内存
106106 void Execute () const
107107 {
108108 if (m_targetOffset == 0 || m_dataSize == 0 || m_patchData == nullptr ) {
@@ -111,17 +111,17 @@ class CodeModifier
111111
112112 void * targetAddress = reinterpret_cast <void *>(m_targetOffset);
113113
114- // 使用RAII模式管理内存保护
114+ // 使用RAII模式管理内存保护
115115 MemoryGuard guard (targetAddress, m_dataSize);
116116
117- // 安全地复制数据
117+ // 安全地复制数据
118118 std::memcpy (targetAddress, m_patchData, m_dataSize);
119119 }
120120
121- // 静态方法集
121+ // 静态方法集
122122 static void ExecuteAllStored ();
123123
124- // 类型化数据补丁
124+ // 类型化数据补丁
125125 template <typename DataType>
126126 static void ModifyWithData (DWORD offset, std::initializer_list<DataType> dataPattern)
127127 {
@@ -133,49 +133,49 @@ class CodeModifier
133133 modifier.Execute ();
134134 }
135135
136- // 原始字节补丁(数组版本)
136+ // 原始字节补丁(数组版本)
137137 template <size_t ArraySize>
138138 static void ModifyRawBytes (DWORD offset, const BYTE (&byteArray)[ArraySize])
139139 {
140140 CodeModifier modifier (offset, static_cast <DWORD>(ArraySize), byteArray);
141141 modifier.Execute ();
142142 }
143143
144- // 原始字节补丁(初始化列表版本)
144+ // 原始字节补丁(初始化列表版本)
145145 static void ModifyRawBytes (DWORD offset, std::initializer_list<BYTE> byteData)
146146 {
147147 ModifyWithData<BYTE>(offset, byteData);
148148 }
149149
150- // 原始字节补丁(指针+长度版本)
150+ // 原始字节补丁(指针+长度版本)
151151 static void ModifyRawBytes (DWORD offset, const BYTE* dataPtr, size_t dataLength)
152152 {
153153 CodeModifier modifier (offset, static_cast <DWORD>(dataLength), dataPtr);
154154 modifier.Execute ();
155155 }
156156
157- // 远跳转补丁
157+ // 远跳转补丁
158158 static void InsertFarJump (DWORD offset, DWORD jumpTarget);
159159 static void InsertFarJump (DWORD offset, void * jumpTarget)
160160 {
161161 InsertFarJump (offset, reinterpret_cast <DWORD>(jumpTarget));
162162 }
163163
164- // 近调用补丁
164+ // 近调用补丁
165165 static void InsertNearCall (DWORD offset, DWORD callTarget);
166166 static void InsertNearCall (DWORD offset, void * callTarget)
167167 {
168168 InsertNearCall (offset, reinterpret_cast <DWORD>(callTarget));
169169 }
170170
171- // 6字节调用补丁(特殊格式)
171+ // 6字节调用补丁(特殊格式)
172172 static void InsertExtendedCall (DWORD offset, DWORD callTarget);
173173 static void InsertExtendedCall (DWORD offset, void * callTarget)
174174 {
175175 InsertExtendedCall (offset, reinterpret_cast <DWORD>(callTarget));
176176 }
177177
178- // 虚函数表修补
178+ // 虚函数表修补
179179 static void UpdateVTableEntry (DWORD vtableOffset, DWORD newFunction);
180180 static void UpdateVTableEntry (DWORD vtableOffset, void * newFunction)
181181 {
@@ -200,7 +200,7 @@ class CodeModifier
200200 UpdateVTableEntry (vtableOffset, GetMemberFunctionAddress (memberFunc));
201201 }
202202
203- // 偏移量修补(别名)
203+ // 偏移量修补(别名)
204204 static void ModifyPointer (DWORD offset, DWORD newValue)
205205 {
206206 UpdateVTableEntry (offset, newValue);
@@ -212,5 +212,5 @@ class CodeModifier
212212};
213213#pragma pack(pop)
214214
215- // 节区扫描函数声明
215+ // 节区扫描函数声明
216216bool LocateExecutableSection (const char * sectionName, SectionInfo* result);
0 commit comments