microsoft / Windows-Dev-Performance

A repo for developers on Windows to file issues that impede their productivity, efficiency, and efficacy
MIT License
438 stars 21 forks source link

SetWinEventHook and SetWindowsHookEx does not provide a way to pass user data #28

Open sylveon opened 4 years ago

sylveon commented 4 years ago

Currently, these methods have no way for users to provide user data parameters if the hook is ran in the hooker process (in process). This can be problematic in advanced scenarios, where multiple hooks can be desired (I scope my hooks as much as possible because hooking all events can cause system slowdowns).

Currently, none of the possible solutions are pretty:

As far as I know, these two functions are some of the only callback-based functions having this limitation. Even window procedures have a way to store a custom user data pointer.

Additionally, adding this feature for out of process hooks would also be useful as it would allow the hooker process to pass information like a handle acquired with DuplicateHandle, or the handle of a window to use for communication with, to the callback running in the hooked process. Currently, I rely on Detours' DetoursCreateRemotePayload to transfer this information but this solution is far less than ideal, and does some pretty questionable things to the target process (like create a memory allocation that's practically not freeable because Detours never gives you the base pointer and doesn't have an API to free such payloads).