Frage

i'm trying to make my hook work globaly inside a process, it worked for me using the _LL (LowLevel) one, when setting hMod and dwTID to 0.

is there a way to make it work without a .dll ?

War es hilfreich?

Lösung

This is not possible. The requirement is mentioned nearly in every place that talks about or give examples of global hooks on MSDN. Some examples:

Hook Procedures

... A global hook procedure can be called in the context of any application in the same desktop as the calling thread, so the procedure must be in a separate DLL module. ...


Installing and Releasing Hook Procedures

... You must place a global hook procedure in a DLL separate from the application installing the hook procedure. ...


SetWindowsHookEx function (Windows)

... All global hook functions must be in libraries. ...


Note that the reason might be that the code can be run in context of other applications as per the documentation, but this is not always the case - also mentioned in the documentation. From SetWindowsHookEx:

... Be aware that the WH_MOUSE, WH_KEYBOARD, WH_JOURNAL*, WH_SHELL, and low-level hooks can be called on the thread that installed the hook rather than the thread processing the hook. ...

I don't really know what the can really means in that statement. Is it sometimes that way and sometimes the othwer way, but I only conducted one test, and the hook procedure is called indeed in the context of the thread that installed the hook, rendering unnecessary any interprocess communication. This doesn't change the requirement for the dll however.


The processing of low level hooks is simply different. As explained to some extent in the documentation, the call to the hook is done by sending a message to the thread that installed the hook and then switching the context to that thread - which does not require installing a dll.

Andere Tipps

Just to clarify, WH_KEYBOARD_LL and WH_MOUSE_LL are low level global hooks that do not require a dll (the other hooks like WH_KEYBOARD do require a dll to inject into other processes):

Applying low-level keyboard hooks with Python and SetWindowsHookExA

If you intend to hook into one or more external processes, you must implement the hook inside of a DLL as the hook code needs to be injected into the address space of those processes, and only a DLL can do that.

If you intend to hook just your own calling process, you do not need a DLL, but you must call SetWindowsHookEx() on a per-thread basis to install thread-specific hooks, ie you have to set the hMod value to NULL and the dwThreadId parameter to a non-zero value, such as from GetCurrentThreadId() or CreateThread().

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top