benstpierre
benstpierre

Reputation: 33581

SendMessage vs SendNotifyMessage in user32.dll api

I've inherited some rather narly "screen scraping" code at work. It all seems to work fine EXCEPT on some Windows XP machines where click events never make it to the button we wish to programatically "click".

The following snippet is used in various parts of our code to "click" a button.

    [DllImport("user32.dll")]
    public extern static int SendMessage(IntPtr hwnd, uint msg, uint wParam, uint lParam);
    [DllImport("user32.dll")]
    public extern static int SendNotifyMessage(IntPtr hwnd, uint msg, uint wParam, uint lParam);
    static public void SendClick(SystemWindow w, bool WAIT = true, int delay=100)
    {
        if (w != null)
            if (WAIT == true)
                SendMessage(w.HWnd, 0x00F5, 0, 0);
            else
            {
                SendNotifyMessage(w.HWnd, 0x00F5, 0, 0);
                System.Threading.Thread.Sleep(delay);
            }
    }

What confuses me is the difference between SendMessage and SendNotifyMessage. Any idea why you would want one over the other?

Upvotes: 0

Views: 3393

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596216

0x00F5 is BM_CLICK. Its documentation contains the following note:

If the button is in a dialog box and the dialog box is not active, the BM_CLICK message might fail. To ensure success in this situation, call the SetActiveWindow function to activate the dialog box before sending the BM_CLICK message to the button.

Upvotes: 2

Related Questions