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:
... 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.