0

I'm currently trying to find a reliable method to get notified (not polling something) when the monitor turns on/off within my service (or, if there is no other chance, in it's "helper application" that is running in the user context). Most suggestion I found go the the "WM_SYSCOMMAND" way which does not seems to fit my requirements (for example, this message are not send to windows in the background).

After all it seemed that using RegisterPowerSettingsNotification() is a good solution (also works from within a service), but it requires Windows Vista or higher.

I tried similar with RegisterDeviceNotification() and I could see that SERVICE_CONTROL_DEVICEEVENT occours every time the monitor turns on or off:

...

DEV_BROADCAST_DEVICEINTERFACE filter;
ZeroMemory(&filter, sizeof(DEV_BROADCAST_DEVICEINTERFACE));

filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
filter.dbcc_classguid = GUID_DEVINTERFACE_DISPLAY_ADAPTER;    // ???

// m_hStatus is defined somewhere other and is of type SERVICE_STATUS_HANDLE
HDEVNOTIFY hDevEvents = RegisterDeviceNotification(m_hSvcStatus, &filter, 
                        DEVICE_NOTIFY_SERVICE_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);

...

That works so far Unfortunately I get this notification too if a USB device is (dis-) connected. If I remove DEVICE_NOTIFY_ALL_INTERFACE_CLASSES I get nothing any more. Can anybody tell me how to modify the code above so that I will recieve notification only when monitor turns on or off?

Willy K.
  • 397
  • 2
  • 14
  • Pay attention to the dbcc_classguid you get. What you have now cannot be correct, ought to be GUID_DEVINTERFACE_MONITOR. – Hans Passant Mar 11 '16 at 16:10
  • Related: https://stackoverflow.com/questions/203355/is-there-any-way-to-detect-the-monitor-state-in-windows-on-or-off?rq=1 – Adrian McCarthy Mar 11 '16 at 16:36
  • Thanks for your inputs. The thread mentoined as related by Adrian McCarthy is about how to query (poll) the state of the monitor - but this thread is about how to get notified. Hans Passant pointed into the right direction, but GUID_DEVINTERFACE_MONITOR did not work (I could not find the header where is it defined?). I castet the wParam to DEV_BROADCAST_DEVICEINTERFACE * in SERVICE_CONTROL_DEVICEEVENT has shown the GUID is {E6DFDC31-31D0-46AC-86AF-DA1EB05FC599} - but I could not figure out where this GUID is defined. Any ideas where it comes from? – Willy K. Mar 16 '16 at 17:06

0 Answers0