1

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 ?

  • What do you mean "without a DLL?" Please try and give some more detail on your exact scenario. – David Heffernan Jan 29 '13 at 09:39
  • 4
    You NEED a DLL because that DLL will be loaded into each and every process that runs and needs to call the hook. The hook will not be called in the context of your EXE, and you're going to have a lot of fun "talking" to your own exe! See this: http://stackoverflow.com/questions/9550074/setwindowshookex-creates-a-local-hook-how-to-make-it-global – Cosmin Prund Jan 29 '13 at 09:53
  • @DavidHeffernan: Cosmin understood me... –  Jan 29 '13 at 10:13
  • @CosminPrund: out of curiousity, how come the LowLevel one doesn't need a .dll ? –  Jan 29 '13 at 10:13
  • @SertacAkyuz Yes, i should have opened both eyes :o) – Sir Rufo Jan 29 '13 at 21:13

3 Answers3

6

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.

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
3

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

Community
  • 1
  • 1
Kevin Edwards
  • 360
  • 1
  • 3
  • 6
0

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().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770