Skip to content

Commit 5a5edbb

Browse files
committed
resolve #9
1 parent a145049 commit 5a5edbb

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

CodeFunctions.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#define RPS_ARGUMENT_LIMIT 20
55

6+
#define JMP_OR_CALL_REQUIRED_BYTE_COUNT 5
7+
68
// forward declaration
79
void LuaLandingFromCpp();
810

@@ -34,15 +36,15 @@ bool DoCreateCallHook(DWORD from_address, DWORD to_address, int hookSize, DWORD&
3436
constexpr INT8 CALL = (INT8)0xE8;
3537

3638
int size = hookSize;
37-
if (size < 5) return FALSE;
39+
if (size < JMP_OR_CALL_REQUIRED_BYTE_COUNT) return FALSE;
3840

3941
BYTE* fun_o_ptr = (BYTE*)from_address;
4042
BYTE* fun_h_ptr = (BYTE*)to_address;
4143

42-
// create gateway
43-
BYTE* gateway = (BYTE*)VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
44+
// create gateway: size + 5
45+
BYTE* gateway = (BYTE*)VirtualAlloc(0, size + JMP_OR_CALL_REQUIRED_BYTE_COUNT, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
4446
memcpy_s(gateway, size, fun_o_ptr, size);
45-
uintptr_t gatewayRelAddress = fun_o_ptr - gateway - 5;
47+
uintptr_t gatewayRelAddress = fun_o_ptr - gateway - JMP_OR_CALL_REQUIRED_BYTE_COUNT;
4648

4749
*(gateway + size) = JMP;
4850
*(uintptr_t*)((uintptr_t)gateway + size + 1) = gatewayRelAddress;
@@ -54,7 +56,7 @@ bool DoCreateCallHook(DWORD from_address, DWORD to_address, int hookSize, DWORD&
5456

5557
memset(fun_o_ptr, NOP, size); // needs to be done, otherwise this confuses the CE disassmbler
5658

57-
uintptr_t relAddress = fun_h_ptr - fun_o_ptr - 5;
59+
uintptr_t relAddress = fun_h_ptr - fun_o_ptr - JMP_OR_CALL_REQUIRED_BYTE_COUNT;
5860

5961
*fun_o_ptr = CALL;
6062
*(uintptr_t*)(fun_o_ptr + 1) = relAddress;
@@ -896,7 +898,7 @@ void __declspec(naked) detourLandingFunction() {
896898
mov ecx, esp; // store a pointer to the register values on the stack.
897899

898900
mov eax, [esp + (9 * 0x04)]; // the 9th element will be the return address from the detour.
899-
sub eax, 5; // subtract 5 because a jump is 5 long to get the origin address.
901+
sub eax, JMP_OR_CALL_REQUIRED_BYTE_COUNT; // subtract 5 because a jump is 5 long to get the origin address.
900902
push ecx; // push the register array;
901903
push eax; // set this as an argument to the function.
902904

0 commit comments

Comments
 (0)