Kiramm
Kiramm

Reputation: 351

WM_DEVICECHANGE comes twice when disconnecting/connecting device

I'm listening in my message loop for WM_DEVICECHANGE, and when I remove or thrust my webcam, WM_DEVICECHANGE comes twice, with identical MSG (with same HWND, UINT, WPARAM and LPARAM). How to fix it?

Upvotes: 0

Views: 1083

Answers (1)

selbie
selbie

Reputation: 104524

This is to be expected. Although the wParam and lParam should be different to indicate a different stage of the device state change, it would not surprise me to know there are duplicate messages.

If I recall correctly, what we did in our code is that when we received a WM_DEVICECHANGE event is to do any of the following:

  1. Once the first one came in, ignore all subsequent notifications for the next couple of seconds.

  2. Or just use the WM_DEVICECHANGE as a hint and not the truth. When we received such a message, we would re-enumerate the device list with the appropriate API and see if anything changed since the last time we enumerated. If the new list was the same as the original list, then there was nothing to do.

Upvotes: 5

Related Questions