0

I'm using ::SendInput to send a mouse click event:

void LeftDown (LONG x_cord, LONG y_cord)
{  
    INPUT    Input={0};
    // left down 
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags  = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;
    Input.mi.dx = x_cord;
    Input.mi.dy = y_cord;
    Input.mi.dwExtraInfo = 0x12345;   //Is this how to use it?
    ::SendInput(1,&Input,sizeof(INPUT));
}

I want to set the dwExtraInfo to some self defined value and extract it in the WndProc at the target application. Then (for example) I will ignore that click if the dwExtraInfo is set to some specific value:

LRESULT CALLBACK OSRWindow::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ 
    if(message == WM_LBUTTONDOWN)
    {          
        if(GetMessageExtraInfo() == 0x12345)     //Is this how to use it?
            //ignore
        else
            //do something
    }
}

Is this naive way is the proper way to use dwExtraInfo or is there a better practice? Thanks!

Sanich
  • 1,739
  • 6
  • 25
  • 43
  • This invariably fails the "what if two programs do this" sanity test. – Hans Passant Sep 06 '15 at 17:57
  • @HansPassant What do you mean? – Sanich Sep 07 '15 at 08:43
  • If the machine is running another program that also injecting input and using dwExtraInfo to pass info then your hook just can't tell anymore what it is supposed to mean. This is more common than you might think btw, google 0xFF515700 to see what kind of trouble is looming. So it probably works just fine on your machine but you'll have no idea what can happen on another. – Hans Passant Sep 07 '15 at 09:15
  • Expanding on Hans Passant's comment, using *dwExtraInfo* is never safe when used with standard Windows messages. Passing information through *dwExtraInfo* with custom control messages (`WM_USER+x`) or application private messages (`WM_APP+x`) should be safe, though. – IInspectable Sep 09 '15 at 12:49

1 Answers1

3

The documentation says:

dwExtraInfo

An additional value associated with the mouse event. An application calls GetMessageExtraInfo to obtain this extra information.

So yes, use it just as you have shown it in your question.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490