-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
103 lines (78 loc) · 2.53 KB
/
example.cpp
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
#include "vdetour.h"
class MainClass
{
public:
virtual void FunctionA(int arg0, int arg1)
{
printf("\tMainClass::FunctionA arg0:%d arg1:%d\n", arg0, arg1);
}
virtual double FunctionB(int arg0, int arg1, int arg2)
{
printf("\tMainClass::FunctionB arg0:%d arg1:%d arg2:%d\n", arg0, arg1, arg2);
return m_MemberValue;
}
float m_MemberValue;
};
class AttachedClass
{
public:
void FunctionA(int arg0, int arg1)
{
printf("\tAttachedClass::FunctionA arg0:%d arg1:%d\n", arg0, arg1);
}
void FunctionB(int arg0, int arg1, int arg2)
{
printf("\tAttachedClass::FunctionB arg0:%d arg1:%d arg2:%d\n", arg0, arg1, arg2);
}
};
class AttachedClass2
{
public:
void FunctionA(int arg0, int arg1)
{
printf("\tAttachedClass2::FunctionA arg0:%d arg1:%d\n", arg0, arg1);
}
void FunctionB(int arg0, int arg1, int arg2)
{
printf("\tAttachedClass2::FunctionB arg0:%d arg1:%d arg2:%d\n", arg0, arg1, arg2);
}
double FunctionB_Replacement(int arg0, int arg1, int arg2)
{
printf("\tAttachedClass2::FunctionB_Replacement arg0:%d arg1:%d arg2:%d\n", arg0, arg1, arg2);
return 9999999.0;
}
};
int main()
{
MainClass *testObject = new MainClass();
testObject->m_MemberValue = 500;
// Call FunctionA normally.
printf("FunctionA, no hooks:\n");
testObject->FunctionA(1, 2);
printf("\n\n");
// Create the vtable modifier.
CVTable *vtable = new CVTable(*((void ***)testObject));
// Give CVTable information about FunctionA.
vtable->Hint(0, 2, "FunctionA");
vtable->CallHook<void (AttachedClass:: *)(int, int)>(0, &AttachedClass::FunctionA);
vtable->ReturnHook<void (AttachedClass2:: *)(int, int)>(0, &AttachedClass2::FunctionA);
printf("FunctionA, 2 hooks:\n");
testObject->FunctionA(1, 2);
printf("\n\n");
// Give CVTable information about FunctionB.
vtable->Hint(1, 3, "FunctionB");
vtable->CallHook<void (AttachedClass:: *)(int, int, int)>(1, &AttachedClass::FunctionB);
vtable->ReturnHook<void (AttachedClass2:: *)(int, int, int)>(1, &AttachedClass2::FunctionB);
vtable->Detour<double (AttachedClass2:: *)(int, int, int)>(1, &AttachedClass2::FunctionB_Replacement);
printf("FunctionB, 2 hooks + detour:\n");
printf("\tFunctionB returned: %f\n", testObject->FunctionB(1, 2, 3));
printf("\n\n");
// Delete the CVTable object, restoring the table.
delete vtable;
printf("FunctionB, restored:\n");
printf("\tFunctionB returned: %f\n", testObject->FunctionB(1, 2, 3));
printf("\n\n");
delete testObject;
system("pause");
return 0;
}