Your problems depend on several fronts:
- UnHookWindowsHook does not unload the injected DLLs, all it does is remove the hook handler. If dlls need to be unloaded, then they need to come up with some sort of unloading mechanism.
- SetWindowLongPtr usually fails when called from a process other than the process to which the window belongs.
The nett result from this is very difficult to safely remove window hooks. First, your OldWindowProc pointer should not be stored in the shared data area. Then, to remove the subclass, you should be able to share the (currently) subclassed process to execute the non-subclass.
What you can do is first register a new unique identifier for the message and put it in your common area using RegisterWindowMessage. WM_REMOVE_HOOK .
UINT idWM_REMOVE_HOOK = RegisterWindowMessage("WM_REMOVE_HOOK");
Now that you need to remove the hook,
SendMessage(hWndSubClass,idWM_REMOVE_HOOK,0,0);
In your subclass of proc:
if(uMsg == WM_DESTROY || uMsg == idWM_REMOVE_HOOK) { Unsubclass(hwnd); }
Remove the UnSubClass call in DLL_PROCESS_DETATCH. This is a dangerous race condition that will cause your DLL to be unloaded into some random process in order to destroy the hook data of a potentially valid hook in another process.
source share