Click here to Skip to main content
15,396,239 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello everyone.

Since some time now, I've been experimenting with hooks. I've been trying to intercept WM_PAIT- and WM_ERASEBKGND messages sent to the desktop window, but I can't get the hook to work.

As I understand, one can set a system-wide hook to a specified module's WndProc-method by using SetWindowsHookEx, passing the module's HINSTANCE along. However, when I try to pass the desktop's HINSTANCE (obtained calling GetWindowLong), SetWindowsHookEx returns NULL. I used a sample ErrorExit method found on MSDN to show me the actual problem (it translates an error message from GetLastError into a readable error message), and it claims SetWindowsHookEx cannot find the specified module.

I've checked the instance handle returned by GetWindowLong in Spy++, and it appears to be correct. Does anyone know what the problem might be here? Could it be that hooking the desktop window's functions is some sort of access violation?


Updated 24-Jun-22 1:58am
mbue 23-Mar-11 17:05pm
Why do you want to hijack other windows? You can subclass another window by replace the windowproc but this shouldnt (i think so) work from an injected module.

According to MSDN: The issues above apply equally well to hooking the desktop. If an application were allowed to hook the desktop, it could potentially hang it. This is completely unacceptable and violates one of the design principles of Windows NT: no application should be allowed to bring down the system or hang the user interface.

So it doesn't sound like Windows is going to let you do that.
Morchaiel 24-Mar-11 21:17pm
Right, that makes sense I guess :)

Actually the Desktop is so out-of-bounds such that if you even try to log its messages with the Microsoft Spy++ you get a message that says:

'The specified window cannot be spied upon. Windows will not allow access the message stream for this window.'
Dave Kreskowiak 24-Jun-22 12:20pm
You didn't really read Solution 1, did you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900