user3366592
user3366592

Reputation: 449

How to send/receive a message from another window/program?

I can select any visible window I want and get its main handle, but I can't handle sending or receiving messages. GetMessage() function always returns 0. What if I want to send a message about keystroke when a textbox that belongs to another window is currently active?

MSG msg;
WPARAM wParam;
LPARAM lParam;
UINT message;
while(TRUE)
{
    GetMessage(&msg, rHwnd, 0, 0); // get message from another window
    TranslateMessage(&msg);
    wParam = msg.wParam;
    lParam = msg.lParam;
    message = msg.message;
    switch(message) // check whether an user clicked the 't' key
    {
        case WM_CHAR:
            switch(wParam)
            {
                case 't':
                    MessageBox(NULL, "t", "", 0);
                    break;
            }
            break;
    }
}

Upvotes: 1

Views: 3307

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595329

Sending messages to a window is easy - use PostMessage() or SendMessage...() for that (though for simulating keyboard input, you should be using SendInput() instead). However, GetMessage() can only retreive messages for a window that is owned by the calling thread, it cannot retrieve messages for a window that is owned by another thread/process. If you need to process messages intended for another application, you have to use a message hook via SetWindowsHookEx().

Upvotes: 5

Related Questions