Discussion:
SetWindowsHookEx
(too old to reply)
Frank
2011-06-20 14:37:44 UTC
Permalink
Dear people,

I'd like to install a hook to intercept all messages
sent to any program on the computer. This is my
code so far:

HMODULE hMod = GetModuleHandle(0);
HHOOK hHook = SetWindowsHookEx(WH_CALLWNDPROC, HookProc, hMod, 0);
ULONG ulError = GetLastError();

Now what I get is a NULL handle for hHook and
an ulError of ERROR_HOOK_NEEDS_HMOD
("Cannot set nonlocal hook without a module handle").

But I provided a valid hMod. What am I doing wrong?
Joseph M. Newcomer
2011-06-20 17:33:52 UTC
Permalink
See below...
Post by Frank
Dear people,
I'd like to install a hook to intercept all messages
sent to any program on the computer. This is my
****
This code is completely useless because you have said nothing about where it is executed,
or where HookProc is located.

I have no idea what GetModuleHandle(0) means. GetModuleHandle(NULL) would make sense.
Please do not throw random zeroes in where NULL is the correct way to write the call.

This will only work if hMod is the module handle of a DLL, and HookProc is in that DLL,
and even then it will not work on Vista and beyond for capturing "all messages" sent to
"any program", because of how "integrity levels" are used in Vista, and there is no way to
defeat this security feature that I am aware of.

I suspect your error is because you are using the HMODULE of the main .exe, which, of
course, would be completely nonsensical for a system-wide hook.

See my essay on hooks on my MVP Tips site. Note also that it would be a Very, Very Bad
Idea to use ANY MFC in the DLL.
joe
Post by Frank
HMODULE hMod = GetModuleHandle(0);
HHOOK hHook = SetWindowsHookEx(WH_CALLWNDPROC, HookProc, hMod, 0);
ULONG ulError = GetLastError();
Now what I get is a NULL handle for hHook and
an ulError of ERROR_HOOK_NEEDS_HMOD
("Cannot set nonlocal hook without a module handle").
But I provided a valid hMod. What am I doing wrong?
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Frank
2011-06-21 11:59:28 UTC
Permalink
Post by Joseph M. Newcomer
See below...
Post by Frank
Dear people,
I'd like to install a hook to intercept all messages
sent to any program on the computer. This is my
****
This code is completely useless because you have said nothing about where it is executed,
or where HookProc is located.
It is executed in a small MFC program which should
record messages sent to other programs. The HookProc
is in the same program.
Post by Joseph M. Newcomer
I have no idea what GetModuleHandle(0) means.  GetModuleHandle(NULL) would make sense.
Please do not throw random zeroes in where NULL is the correct way to write the call.
This will only work if hMod is the module handle of a DLL, and HookProc is in that DLL,
and even then it will not work on Vista and beyond for capturing "all messages" sent to
"any program", because of how "integrity levels" are used in Vista, and there is no way to
defeat this security feature that I am aware of.
I suspect your error is because you are using the HMODULE of the main .exe, which, of
course, would be completely nonsensical for a system-wide hook.
Ok, so I'll have to put the HookProc in a
DLL of its own. If I want to evaluate the
messages in the main program, what kind
of communication between the two would
you recommend?
Post by Joseph M. Newcomer
See my essay on hooks on my MVP Tips site.  Note also that it would be a Very, Very Bad
Idea to use ANY MFC in the DLL.  
Thank you, I'll have a look shortly.

Loading...