How to get notified when going in and out of connected standby from a Windows Service?

Always on Connected Standby (AOAC) was introduced in Windows 8.  Here is the definition as described on MSDN:

"Starting with Windows 8 and Windows 8.1, connected standby is a new low-power state that features extremely low power consumption while maintaining Internet connectivity. For a PC that implements the connected standby power model, the connected standby power state serves as the primary off mode for the PC—similar to the off mode that a smartphone enters when the user presses the power button. The Windows UI exposes the connected standby power state to the end user as the system "sleep" state."

When going into connected standby Windows Store Applications are suspended as well as Windows Desktop Applications (via the Desktop Activity Moderator through Job Objects).  Windows Services which are located in session 0 are not suspended but instead are throttled.  Windows Desktop Applications can be notified when going into connected standby by registering for a notification.  See the following for more information:

https://msdn.microsoft.com/en-us/library/windows/desktop/hh848040(v=vs.85).aspx

Unfortunately this doesn't apply to Windows Services which are located in session 0 and have no GUI (you can send a message box via WTSSendMessage) and thus have no display.

Connected Standby is synonymous with the display where on an AOAC capable system the display is turned off when going in to connected standby and turned on when leaving this state.

The solution for a service is to monitor on/off notifications for the display.  What you need to do from your service is the following:

  1. Determine if the system is AOAC capable by calling CallNtPowerInformation and looking at the AoAc value of the SYSTEM_POWER_CAPABILITIES struct).
  2. To get monitor on/off notifications you can call:

RegisterPowerSettingNotification
(https://msdn.microsoft.com/en-us/library/windows/desktop/aa373196(v=vs.85).aspx)

And use the GUID_MONITOR_POWER_ON power setting GUID.

Follow us on Twitter, www.twitter.com/WindowsSDK