0

I'm trying to monitor a message sent by another window handle (particularly WM_TIMER) using WH_GETMESSAGE hook but it seems that I can only get the receiver handle, not the sender. Here's my code:

    LRESULT WINAPI GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam){
        if (nCode < 0){
             return CallNextHookEx(hGetMsgHook, nCode, wParam, lParam);
        }
        else{
             MSG* msg = (MSG*)lParam;
             HWND window = msg->hwnd;
             unsigned int msgCode=LOWORD(msg->message);
             char* className = new char[50];
             if (msgCode == WM_TIMER){
                 GetClassNameA(window, className, 50);

                 //className of the receiver handle
             }
        }
        return CallNextHookEx(hGetMsgHook, nCode, wParam, lParam);
    }

How do I get the sender hwnd?

Kara
  • 6,115
  • 16
  • 50
  • 57
dumbQ
  • 3
  • 2
  • Possible duplicate of [How can my app find the sender of a windows message?](http://stackoverflow.com/questions/910991/how-can-my-app-find-the-sender-of-a-windows-message) - if you look at that question, you'll see that it's not possible. I was [previously asking a similar question](http://stackoverflow.com/q/15700863/2065121). For WM_TIMER though it's even more complicated because this is a pseudo-message, not a "real" message. – Roger Rowland Mar 08 '14 at 08:15
  • I don't get it. When I use Spy++, it does show the message was post from that handle window, which means Spy++ somehow caught the message. What a headache... – dumbQ Mar 08 '14 at 08:22
  • 3
    WM_TIMER is not a sent message. There is no sender. Spy++ shows the recipient not the sender. – Raymond Chen Mar 08 '14 at 08:53

1 Answers1

2

You are asking to find information that has no meaning. The WM_TIMER message is not sent from one window to another. It is a pseudo-message that is synthesised by the system.

Certain low priority messages are implemented this way. The other common example is WM_PAINT. Your program has a message loop that repeatedly calls GetMessage which pulls messages off the queue. When the queue of real messages is empty, pseudo-messages are generated if needed. At this point the system will synthesise WM_TIMER or WM_PAINT messages.

So, these messages are never actually sent. They are just synthesised on demand.

On top of that, messages are not sent by windows. Messages are sent by calls to SendMessage, PostMessage etc. These functions do not ask the caller to supply a window handle for the sender. And they can be called from anywhere and have no clear affinity with any sender window. So even for tradition true messages, the concept of sender window has no meaning.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Actually, I rely on this message to trigger my injected code since it's closest to the event I'm monitoring. Guess I'll have to find another way. Much appreciated! – dumbQ Mar 08 '14 at 08:59
  • Use a custom message via RegisterWindowMessage(), and post it to the message queue of the target window/thread using PostMessage() or PostThreadMessage(). – Remy Lebeau Mar 08 '14 at 11:36