-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMem.h
73 lines (58 loc) · 1.94 KB
/
CMem.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
68
69
70
71
72
73
#pragma once
#include <Windows.h>
#define ASM_JMP 0xE9
#define ASM_NOP 0x90
class CMem
{
public:
template<class T, class U> static void PutSingle(U pMemory, T value);
template<class T, class U, class V> static void Put(U pMemory, V pValue, unsigned long uiLen);
template<class T, class U> static T Get(U pMemory);
template <class T> static BOOL Unprotect(T lpMemory, SIZE_T dwSize, DWORD* pOut = NULL);
template <class T> static void Set(T lpMemory, unsigned char ucByte, size_t uiSize);
template <class T, class U> static void InstallJmp(T address, U proxy, DWORD& jumpback, DWORD dwLen, DWORD jumpbackPos = NULL);
template <class T, class U> static void InstallJmp(T address, U proxy, DWORD dwLen = 5);
static void ApplyJmp(BYTE* pAddress, DWORD dwProxy, DWORD dwLen);
static void Cpy(void* address, const void* src, int size);
private:
static DWORD m_dwUnprotectDummy;
};
template <class T, class U>
void CMem::InstallJmp(T address, U proxy, DWORD& jumpback, DWORD dwLen, DWORD jumpbackPos)
{
if (!jumpbackPos)
jumpbackPos = dwLen;
jumpback = (DWORD)address + jumpbackPos;
ApplyJmp((BYTE*)address, (DWORD)proxy, dwLen);
}
template <class T, class U>
static void CMem::InstallJmp(T address, U proxy, DWORD dwLen)
{
return ApplyJmp((BYTE*)address, (DWORD)proxy, dwLen);
}
template<class T, class U>
void CMem::PutSingle(U pMemory, T value)
{
*(T*)pMemory = value;
}
template<class T, class U, class V>
void CMem::Put(U pMemory, V pValue, unsigned long uiLen)
{
for (size_t i = 0; i < uiLen; ++i)
PutSingle<T, T*>((T*)(pMemory)+i, *((T*)(pValue)+i));
}
template<class T, class U>
T CMem::Get(U pMemory)
{
return *(T*)pMemory;
}
template <class T>
BOOL CMem::Unprotect(T lpMemory, SIZE_T dwSize, DWORD* pOut)
{
return VirtualProtect((void*)lpMemory, dwSize, PAGE_EXECUTE_READWRITE, pOut ? pOut : &m_dwUnprotectDummy);
}
template <class T>
void CMem::Set(T lpMemory, unsigned char ucByte, size_t uiSize)
{
memset((void*)lpMemory, ucByte, uiSize);
}