Your code is very inefficient if multiple hooks are to be removed at once.
I'am removing 16 hooks with MH_RemoveHook() and each time all threads are enumerated and each time all threads are frozen.
This is very slow.
The function MH_RemoveHook should be replaced by:
MH_RemoveHooks(LPVOID pTargetArray, UINT uArrayCount)
{
Enumerate all threads,
Freeze all threads,
Delete uArrayCount hook entries,
Unfreeze all threads
}
Another very useful improvement would be that MH_RemoveHooks takes the Detour functions instead of the Target functions.
Why ?
Because I store all detour functions in variables in my class which I need to call the original function.
But the target functions are not used anymore after I have set the hook.
So if I want to later remove hooks I need 32 variables!
I need 16 variables for the detours that I call inside the hook.
And I need 16 additional variables to store the targets which I need only to later remove the hooks.
So the perfect function would be:
MH_RemoveHooks(LPVOID pDetourArray, UINT uArrayCount)
{
.....
}
Then I don't need to store the target function anymore.
And the absolute perfect MH_RemoveHooks function would allow me to pass a NULL pointer for pDetourArray which will remove ALL existing hooks.
Your code is very inefficient if multiple hooks are to be removed at once.
I'am removing 16 hooks with MH_RemoveHook() and each time all threads are enumerated and each time all threads are frozen.
This is very slow.
The function MH_RemoveHook should be replaced by:
Another very useful improvement would be that MH_RemoveHooks takes the Detour functions instead of the Target functions.
Why ?
Because I store all detour functions in variables in my class which I need to call the original function.
But the target functions are not used anymore after I have set the hook.
So if I want to later remove hooks I need 32 variables!
I need 16 variables for the detours that I call inside the hook.
And I need 16 additional variables to store the targets which I need only to later remove the hooks.
So the perfect function would be:
Then I don't need to store the target function anymore.
And the absolute perfect MH_RemoveHooks function would allow me to pass a NULL pointer for pDetourArray which will remove ALL existing hooks.