Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MicroHook.h"
- /*returns pointer to original function*/
- BYTE *MH_VTBLHook(DWORD *pVTBL, DWORD dwIdx, BYTE *pNewFunc)
- {
- BYTE *pOrigFunc;
- DWORD dwOldProt;
- VirtualProtect((void*)&pVTBL[dwIdx], 4, PAGE_EXECUTE_READWRITE, &dwOldProt);
- pOrigFunc = (BYTE*)pVTBL[dwIdx];
- pVTBL[dwIdx] = (DWORD)pNewFunc;
- VirtualProtect((void*)&pVTBL[dwIdx], 4, dwOldProt, &dwOldProt);
- return pOrigFunc;
- }
- /*returns pointer to original function*/
- BYTE *MH_Detour(BYTE *pOrigFunc, BYTE *pNewFunc, BYTE len)
- {
- BYTE bTemp;
- DWORD dwOldProt;
- BYTE *trampoline;
- trampoline = (BYTE*)HeapAlloc(GetProcessHeap(),
- HEAP_ZERO_MEMORY, len+5);
- VirtualProtect((void*)pOrigFunc, len, PAGE_EXECUTE_READWRITE, &dwOldProt);
- bTemp = len;
- while (bTemp-- > 0) trampoline[bTemp] = *(pOrigFunc+bTemp);
- trampoline += len;
- trampoline[0] = 0xE9;
- *(trampoline+1) = (BYTE)((pOrigFunc+len - trampoline) - 5);
- pOrigFunc[0] = 0xE9;
- *(pOrigFunc+1) = (BYTE)((pNewFunc - pOrigFunc) - 5);
- bTemp = 5; while (bTemp++ < len) pOrigFunc[bTemp] = 0x90;
- VirtualProtect((void*)pOrigFunc, len, dwOldProt, &dwOldProt);
- return (trampoline - len);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement