Announcement

Collapse
No announcement yet.

How to Write a C++ Detour Function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Write a C++ Detour Function

    Want to do a mid function hook or hook a game function? Learn the basics here!



    Code:
    bool Hook(void * toHook, void * ourFunct, int len) 
    {
    	if (len < 5) {
    		return false;
    	}
    
    	DWORD curProtection;
    	VirtualProtect(toHook, len, PAGE_EXECUTE_READWRITE, &curProtection);
    
    	memset(toHook, 0x90, len);
    
    	DWORD relativeAddress = ((DWORD)ourFunct - (DWORD)toHook) - 5;
    
    	*(BYTE*)toHook = 0xE9;
    	*(DWORD*)((DWORD)toHook + 1) = relativeAddress;
    
    	DWORD temp;
    	VirtualProtect(toHook, len, curProtection, &temp);
    
    	return true;
    }
    Last edited by [GH]Rake; 04-14-2016, 08:50:01 AM.
Working...
X