Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Window Messages "Reliable"?

This is somewhat of a general question regarding Windows programming:

Are Window messages "reliable"?

For example (these are just examples):

  • Can you be certain that a WM_MOUSEMOVE will happen before a cursor enters your screen?

  • Can you be certain that you will get a WM_DEVICECHANGE message if a device is inserted?

  • Can you be certain that you will receive a WM_KILLFOCUS message if your window loses focus?

Or, in other words: Can you be certain that you'll get the appropriate message at the appropriate times, or do you always have to code defensively in case that, somehow, you might miss a message for no apparently documented reason?


Example:

  • It is guaranteed (AFAIK) that a file system filter driver will not "miss" a file operation or change notification.

  • By contrast, it is not guaranteed that ReadDirectoryChangesW will not miss a notification. In fact, it can miss quite a few if its buffer overflows.

Note:

I am not talking about a situation against an adversary (e.g. someone hijacking your window procedure or installing a hook/filter); that would pretty much invalidate any guarantee. I'm only asking about obscure situations that could really happen even if no one meant anything bad intentionally, like if some random buffer overflows, if someone uses SendInput, etc., assuming you have control of your own code.

like image 808
user541686 Avatar asked Oct 24 '22 11:10

user541686


1 Answers

No you cannot be certain that a given message will be delivered in a specific order. Here are a couple of reasons why not

  • Messages can be sent progamatically and this can be used to simulate "impossible" scenarios like a WM_KEYUP followed by a WM_KEYDOWN.
  • Another routine could sub-class your window and selectively intercept messages and not send them on to your WNDPROC

It's best to code defensively around any scenarios where ordering is important

like image 167
JaredPar Avatar answered Oct 27 '22 00:10

JaredPar