Skip to content

Commit 9fca342

Browse files
Helpers.h update & change how vtable hooks are installed.
1 parent 074e94c commit 9fca342

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

Dependencies/Helpers.h

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,57 +30,56 @@ const HMODULE MODULE_HANDLE = GetModuleHandle(nullptr);
3030
returnType callingConvention implOf##functionName(__VA_ARGS__)
3131

3232
#define INSTALL_HOOK(functionName) \
33-
{ \
33+
do { \
3434
DetourTransactionBegin(); \
3535
DetourUpdateThread(GetCurrentThread()); \
3636
DetourAttach((void**)&original##functionName, implOf##functionName); \
3737
DetourTransactionCommit(); \
38-
}
38+
} while(0)
3939

4040
#define VTABLE_HOOK(returnType, callingConvention, className, functionName, ...) \
4141
typedef returnType callingConvention className##functionName(className* This, __VA_ARGS__); \
4242
className##functionName* original##className##functionName; \
4343
returnType callingConvention implOf##className##functionName(className* This, __VA_ARGS__)
4444

4545
#define INSTALL_VTABLE_HOOK(className, object, functionName, functionIndex) \
46-
{ \
47-
void** addr = &(*(void***)object)[functionIndex]; \
48-
if (*addr != implOf##className##functionName) \
46+
do { \
47+
if (original##className##functionName == nullptr) \
4948
{ \
50-
original##className##functionName = (className##functionName*)*addr; \
51-
DWORD oldProtect; \
52-
VirtualProtect(addr, sizeof(void*), PAGE_EXECUTE_READWRITE, &oldProtect); \
53-
*addr = implOf##className##functionName; \
54-
VirtualProtect(addr, sizeof(void*), oldProtect, &oldProtect); \
49+
original##className##functionName = (*(className##functionName***)object)[functionIndex]; \
50+
DetourTransactionBegin(); \
51+
DetourUpdateThread(GetCurrentThread()); \
52+
DetourAttach((void**)&original##className##functionName, implOf##className##functionName); \
53+
DetourTransactionCommit(); \
5554
} \
56-
}
55+
} while(0)
5756

5857
#define WRITE_MEMORY(location, type, ...) \
59-
{ \
58+
do { \
6059
const type data[] = { __VA_ARGS__ }; \
6160
DWORD oldProtect; \
6261
VirtualProtect((void*)(location), sizeof(data), PAGE_EXECUTE_READWRITE, &oldProtect); \
6362
memcpy((void*)(location), data, sizeof(data)); \
6463
VirtualProtect((void*)(location), sizeof(data), oldProtect, &oldProtect); \
65-
}
64+
} while(0)
6665

6766
#define WRITE_JUMP(location, function) \
68-
{ \
67+
do { \
6968
WRITE_MEMORY(location, uint8_t, 0xE9); \
70-
WRITE_MEMORY(location + 1, uint32_t, (uint32_t)(function) - (size_t)(location) - 5); \
71-
}
69+
WRITE_MEMORY(location + 1, uint32_t, (uint32_t)((size_t)(function) - (size_t)(location) - 5)); \
70+
} while(0)
7271

7372
#define WRITE_CALL(location, function) \
74-
{ \
73+
do { \
7574
WRITE_MEMORY(location, uint8_t, 0xE8); \
76-
WRITE_MEMORY(location + 1, uint32_t, (uint32_t)(function) - (size_t)(location) - 5); \
77-
}
75+
WRITE_MEMORY(location + 1, uint32_t, (uint32_t)((size_t)(function) - (size_t)(location) - 5)); \
76+
} while(0)
7877

7978
#define WRITE_NOP(location, count) \
80-
{ \
79+
do { \
8180
DWORD oldProtect; \
8281
VirtualProtect((void*)(location), (size_t)(count), PAGE_EXECUTE_READWRITE, &oldProtect); \
8382
for (size_t i = 0; i < (size_t)(count); i++) \
8483
*((uint8_t*)(location) + i) = 0x90; \
8584
VirtualProtect((void*)(location), (size_t)(count), oldProtect, &oldProtect); \
86-
}
85+
} while(0)

0 commit comments

Comments
 (0)