Simple Hooking library

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
Stearells
Posts: 1
Joined: Feb 15, 2023 14:59

Simple Hooking library

Post by Stearells »

HookEngine is a library for hooking windows API calls such as MS Detours.

Main library class (CHook) methods:

Code: Select all

' Install hook to function (using ret instruction)
' pSource: pointer to source function
' pDestination: pointer to destination function
' Result: Method returns true if the hook is successfully installed, else false.
declare function Install(pSource as any ptr, pDestination as any ptr) as boolean


' Uninstall hook from function (already installed with Install())
' Result: Method returns true if the hook is successfully uninstalled or false if hook not installed now.
declare function Uninstall() as uint_auto ptr


' Check if hook is installed.
' Method returns true if the hook is installed, else false.
declare function IsInstalled() as boolean
Example code:

Code: Select all

dim shared my_hook as HookEngine.CHook

function test(num as long) as long
	return num * 2
end function

function test_hooked(num as long) as long
	dim reHookAddr as any ptr = my_hook.Uninstall()
	dim result as long = test(num)
	my_hook.Install(reHookAddr, @test_hooked)
	
	printf(!"hello from hook\n")
	return result * 10
end function

printf(!"un-hooked call: %d\n", test(2))
my_hook.Install(@test, @test_hooked)
printf(!"hooked call: %d\n", test(2))
Library: GitHub
Post Reply