'Windows: How to initiate "modern" standby from session 0?

Windows 8 introduced a new power saving mode called connected standby. In Windows 10, this was extended and re-named "modern" standby. Some Microsoft documents refer to it as “S0 low-power idle mode” or "Always On / Always Connected". The new mode was initially used on the Microsoft Surface range and subsequently seems to be becoming more comment on other high-end laptop such as the Dell XPS.

Behind the scenes, the new power mode works differently to the normal S1-S4 power states. Power saving starts when the display turns off and is transparent to applications. Microsoft claim that this will deliver faster suspend/resume and longer battery life. This has some interesting implications - desktop applications are not aware when this mode starts and must use the new RegisterSuspendResumeNotification() or PowerRegisterSuspendResumeNotification() APIs to be notified. Systems supporting modern standby cannot also support S1-S3 but should support S4 (hibernate). This is used as a fall-back, when the battery gets low, to preserve data.

Traditionally, desktop apps could use the SetSuspendState() to suspend (S1-S3) or hibernate (S4) the PC. However, this no longer works and attempting to suspend will fail with ERROR_NOT_SUPPORTED. A workaround is to turn off the display directly. These seems to start the "modern" standby mode:

SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);

This works fine, but can only be used from an application that has access to the display. It cannot be used in a service due to session 0 isolation.

Question: Is there a more generic API to initiate "modern" standby that can be used from a service running in session 0?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source