views:

83

answers:

3

Hi, I'm quite new to the Windows API and would like to know how to peek at messages another process receives. As an example, I would like to get the HWND of, say, notepad and peek at all messages that are sent to this window. I code in C/C++.

Thank you

+1  A: 

You want to look into SetWindowsHookEx

Mongus Pong
+1  A: 

You are looking for Windows Hooks .

http://msdn.microsoft.com/en-us/library/ms997537.aspx

You can trap SendMessage in the target process using CallWndProc hook procedure.

Ashish
+5  A: 

You can use SetWindowsHookEx function, with WH_CALLWNDPROC or some other type of hook, and here is an example.

The WH_CBT can give you great opportunities because you can get a HCBT_CREATEWND code from it and it's sent to you right before a window is created, thus giving you a possibility to provide your own window proc instead of the real one and then be able to get all messages possible with it.

Remember though, greater possibilities also mean greater responsibility. Say you "subclassed" some window, providing your window proc, if your application that set a hook exits, next thing you'll see is the application, whose messages you were peeking at, crashes if you didn't put the address of the original window proc back to where it belongs. The benefit of this kind of hooking is the ability to wait for a certain window (say with a certain window class, or name) to be created and get into that process before any window you're interested in would even be created.

Dmitry