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?