Does the hook function used in SetWindowsHookExA need to be in dll?

273 views Asked by At

I am trying to read system keyboard events in my c++ application using SetWindowsHookExA. Application is able to get the system keyboard events as long as the hooking is done in a dll. If I try to create hook inside my application code (without a dll), I am not getting keyboard events. Is it mandatory for hook functions to be in a dll? If yes, is there a reason?

1

There are 1 answers

0
MSalters On

No, a hook does not need to be in a DLL. See the description of the parameters:

[in] lpfn

Type: HOOKPROC

A pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process.

You can have it in your EXE, but then you'll have to use a thread from your process. And IIRC this thread will need to pump messages.