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
  | 
#include "pch.h"
#include "InHook.h"
InHook* InHook::m_pInstance = NULL;
InHook* InHook::GetInitalize()
{
        if (m_pInstance == NULL)
        {
                m_pInstance = new InHook;
        }
        return m_pInstance;
}
//修改地址页面属性,返回原地址属性
DWORD motifyMemoryAttributes(SELF_PTR myAddr, SELF_PTR attributes = PAGE_EXECUTE_READWRITE)
{
        DWORD oldAttributes;
        VirtualProtect((void*)myAddr, byte_length, attributes, &oldAttributes);
        return oldAttributes;
}
void InHook::Initalize(SELF_PTR oldAddr, SELF_PTR myAddr) 
{
        memcpy(&oldAddr, &m_oldFuncAddr, sizeof(oldAddr));
        memcpy(&myAddr, &m_myFuncAddr, sizeof(myAddr));
        m_myByte[0] = { 0xe9 };
        int offset = m_myFuncAddr - m_oldFuncAddr - byte_length;
        memcpy(&m_myByte[1], &offset, byte_length - 1);
        DWORD attributes = motifyMemoryAttributes(m_oldFuncAddr);
        memcpy(m_oldByte, (void*)m_oldFuncAddr, byte_length);
        motifyMemoryAttributes(m_oldFuncAddr, attributes);
}
// 修改地址
void InHook::motifyAddress()
{
        DWORD attributes = motifyMemoryAttributes(m_oldFuncAddr);
        memcpy((void*)m_oldFuncAddr, m_myByte, byte_length);
        motifyMemoryAttributes(m_oldFuncAddr, attributes);
}
// 恢复地址
void InHook::restoreAddress()
{
        DWORD attributes = motifyMemoryAttributes(m_oldFuncAddr);
        memcpy((void*)m_oldFuncAddr, m_oldByte, byte_length);
        motifyMemoryAttributes(m_oldFuncAddr, attributes);
}
  |