Code archives/Miscellaneous/Hook/Redirect functions

This code has been declared by its author to be Public Domain code.

Download source code

Hook/Redirect functions by Azathoth2006
Basically you pass the address of the function you wish to hook and the address of the function you want called instead, returns non-zero if successful.

HookFunc(somefunction,newfunction) ' all calls to somefunction gets redirected to newfunction
Extern "Win32"
	Function GetCurrentProcess:Int()
	Function WriteProcessMemory:Int(hProcess:Int,lpBaseAddress:Byte Ptr,lpBuffer:Byte Ptr,nSize:Int,lpNumberOfBytesWritten:Byte Ptr)
EndExtern

Function HookFunc:Int(func:Byte Ptr,newfunc:Byte Ptr)
	Local d:Byte[6]
	Local c=(Byte Ptr(newfunc)-Byte Ptr(func)-5)
	Local cp:Byte Ptr=Varptr c
	Local rb
	
	d[0]=$e9
	d[5]=$c3
	
	d[1]=cp[0]
	d[2]=cp[1]
	d[3]=cp[2]
	d[4]=cp[3]
	
	Return WriteProcessMemory(GetCurrentProcess(), func, d, 6, Varptr rb)
EndFunction

Comments

None.

Code Archives Forum