Can't put the Surface Book to sleep with DLLCall() because of S0 power state Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Elenkhos
Posts: 31
Joined: 27 Feb 2018, 09:08

Can't put the Surface Book to sleep with DLLCall() because of S0 power state

09 Jun 2019, 07:32

Very simple question: how to put Microsoft Surface Book to sleep with AutoHotkey?

I searched the forum and found this command but it does nothing:

Code: Select all

DllCall("PowrProf\SetSuspendState", "int", 0, "int", 0, "int", 0)
The PC goes to sleep if the sleep is invoked from the start menu or by pressing the power button.
Last edited by Elenkhos on 09 Jun 2019, 16:56, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Can't put the computer to sleep with DLLCall()

09 Jun 2019, 07:49

what is A_LastError after the call fails?
did u run the script as admin?
Elenkhos
Posts: 31
Joined: 27 Feb 2018, 09:08

Re: Can't put the computer to sleep with DLLCall()

09 Jun 2019, 11:24

Thanks for the reply.
swagfag wrote:
09 Jun 2019, 07:49
did u run the script as admin?
Yes.
swagfag wrote:
09 Jun 2019, 07:49
what is A_LastError after the call fails?
A_LastError is set to 50 and ErrorLevel to 0.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Can't put the computer to sleep with DLLCall()

09 Jun 2019, 13:24

ERROR_NOT_SUPPORTED
50 (0x32)
The request is not supported.

run powercfg /a to see which sleep states are supported, assuming there are any that are supported. u should get something like this:
C:\WINDOWS\system32>powercfg /a
The following sleep states are available on this system:
Standby (S3)

The following sleep states are not available on this system:
Standby (S1)
The system firmware does not support this standby state.

Standby (S2)
The system firmware does not support this standby state.

Hibernate
Hibernation has not been enabled.

Standby (S0 Low Power Idle)
The system firmware does not support this standby state.

Hybrid Sleep
Hibernation is not available.
The hypervisor does not support this standby state.

Fast Startup
Hibernation is not available.


if no sleep states are supported, u can try DllCall("PowrProf\SetSuspendState", "int", 1, "int", 0, "int", 1) to hibernate and disable wake timers instead

also consider this https://social.technet.microsoft.com/Forums/en-US/4005e07b-6ad9-484a-b1fb-d70ba5d4f987/psshutdown-surface-book-win-10-to-sleep?forum=pstools
Elenkhos
Posts: 31
Joined: 27 Feb 2018, 09:08

Re: Can't put the computer to sleep with DLLCall()

09 Jun 2019, 15:38

swagfag wrote:
09 Jun 2019, 13:24
ERROR_NOT_SUPPORTED
50 (0x32)
The request is not supported.

run powercfg /a to see which sleep states are supported, assuming there are any that are supported.
Surface Book seems to support only S0 low-power idle mode:
C:\WINDOWS\system32>powercfg /a
The following sleep states are available on this system:
Standby (S0 Low Power Idle) Network Connected

The following sleep states are not available on this system:


In Microsoft documentation it's described as:
Sleep state (Modern Standby)
In the S0 low-power idle mode of the working state, also referred to as Modern Standby, the system remains partially running. During Modern Standby, the system can stay up-to-date whenever a suitable network is available and also wake when real-time action is required, such as OS maintenance. Modern Standby wakes significantly faster than S1-S3. For more info, see Modern Standby.

Note: Modern Standby is only available on some SoC systems. When it's supported, the system does not support S1-S3.
Then the question is how to activate S0 state with AutoHotkey? Any ideas?
Elenkhos
Posts: 31
Joined: 27 Feb 2018, 09:08

Re: Can't put the computer to sleep with DLLCall()

09 Jun 2019, 16:55

swagfag wrote:
09 Jun 2019, 16:14
SendMessage, 0x112, 0xF170, 2,, Program Manager
https://stackoverflow.com/a/48062471
https://stackoverflow.com/a/37177772
You're better at googling than me ;)

Thanks for finding that information. In fact, I was already using that command to turn off the monitor but didn't realize that it also put the Surface Book to "sleep".

Many thanks for your help. Problem solved.
LorenAmelang
Posts: 6
Joined: 25 Feb 2016, 12:40

Re: Can't put the Surface Book to sleep with DLLCall() because of S0 power state

31 Oct 2021, 19:13

Thought I had found the solution for my original Surface Book...

Added a hotkey for:
SendMessage, 0x112, 0xF170, 2,, Program Manager

And it really does turn off the screen. For a few seconds... And then it wakes back up, even on battery, with no mouse or other input. The Start -> Power -> Sleep route stays off.

Any thoughts?
LorenAmelang
Posts: 6
Joined: 25 Feb 2016, 12:40

Re: Can't put the Surface Book to sleep with DLLCall() because of S0 power state

31 Oct 2021, 19:47

So I looked in Event Viewer. It logs each of the AHK "sleep" keypresses, and a second later:

"Reason: Input Keyboard."

I'm not touching the physical keyboard, built-in or Bluetooth. Does it maybe think AHK is sending keystrokes? Maybe it sees the key releases after AHK is triggered?

Log Name: System
Source: Microsoft-Windows-Kernel-Power
Date: 10/31/2021 4:41:30 PM
Event ID: 507
Task Category: (158)
Level: Information
Keywords: (1024),(512),(4)
User: SYSTEM
Computer: DESKTOP-OMMHHCL
Description:
The system is exiting connected standby

Reason: Input Keyboard.
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Microsoft-Windows-Kernel-Power" Guid="{331c3b3a-2005-44c2-ac5e-77220c37d6b4}" />
<EventID>507</EventID>
<Version>9</Version>
<Level>4</Level>
<Task>158</Task>
<Opcode>0</Opcode>
<Keywords>0x8000000000000604</Keywords>
<TimeCreated SystemTime="2021-10-31T23:41:30.0795270Z" />
<EventRecordID>51222</EventRecordID>
<Correlation />
<Execution ProcessID="4" ThreadID="12264" />
<Channel>System</Channel>
<Computer>DESKTOP-OMMHHCL</Computer>
<Security UserID="S-1-5-18" />
</System>
<EventData>
<Data Name="EnergyDrain">0</Data>
<Data Name="ActiveResidencyInUs">697747</Data>
<Data Name="NonDripsTimeActivatedInUs">0</Data>
<Data Name="FirstDripsEntryInUs">0</Data>
<Data Name="DripsResidencyInUs">0</Data>
<Data Name="DurationInUs">697747</Data>
<Data Name="DripsTransitions">0</Data>
<Data Name="FullChargeCapacityRatio">90</Data>
<Data Name="AudioPlaying">false</Data>
<Data Name="Reason">31</Data>
<Data Name="AudioPlaybackInUs">0</Data>
<Data Name="NonActivatedCpuInUs">0</Data>
<Data Name="PowerStateAc">true</Data>
<Data Name="HwDripsResidencyInUs">0</Data>
<Data Name="ExitLatencyInUs">10</Data>
<Data Name="DisconnectedStandby">false</Data>
<Data Name="AoAcCompliantNic">true</Data>
<Data Name="NonAttributedCpuInUs">0</Data>
<Data Name="ModernSleepEnabledActionsBitmask">0</Data>
<Data Name="ModernSleepAppliedActionsBitmask">0</Data>
<Data Name="LidOpenState">true</Data>
<Data Name="ExternalMonitorConnectedState">false</Data>
<Data Name="ScenarioInstanceId">77</Data>
<Data Name="IsCsSessionInProgressOnExit">false</Data>
<Data Name="BatteryRemainingCapacityOnExit">53767</Data>
<Data Name="BatteryFullChargeCapacityOnExit">62415</Data>
<Data Name="ScenarioInstanceIdV2">75</Data>
<Data Name="BootId">101</Data>
<Data Name="InputSuppressionActionCount">0</Data>
<Data Name="NonResiliencyTimeInUs">697747</Data>
<Data Name="ResiliencyDripsTimeInUs">0</Data>
<Data Name="ResiliencyHwDripsTimeInUs">0</Data>
<Data Name="GdiOnTime">0</Data>
<Data Name="DwmSyncFlushTime">0</Data>
<Data Name="MonitorPowerOnTime">694223</Data>
<Data Name="SleepEntered">false</Data>
<Data Name="ScreenOffEnergyCapacityAtStart">53767</Data>
<Data Name="ScreenOffEnergyCapacityAtEnd">53767</Data>
<Data Name="ScreenOffDurationInUs">942866</Data>
<Data Name="SleepEnergyCapacityAtStart">0</Data>
<Data Name="SleepEnergyCapacityAtEnd">0</Data>
<Data Name="SleepDurationInUs">0</Data>
<Data Name="ScreenOffFullEnergyCapacityAtStart">62415</Data>
<Data Name="ScreenOffFullEnergyCapacityAtEnd">62415</Data>
<Data Name="SleepFullEnergyCapacityAtStart">0</Data>
<Data Name="SleepFullEnergyCapacityAtEnd">0</Data>
</EventData>
</Event>
LorenAmelang
Posts: 6
Joined: 25 Feb 2016, 12:40

Re: Can't put the Surface Book to sleep with DLLCall() because of S0 power state

31 Oct 2021, 21:27

Confused and Amazed - this actually works:

Code: Select all

; {{Win+0 --> Sleep
#0::
SoundBeep
SoundBeep
SendMessage, 0x112, 0xF170, 2,, Program Manager
SoundBeep
return
[Mod edit: [code][/code] tags added.]

I tried adding various "Sleep, <time>" commands various places, and none of those versions blacked the screen at all.

With the beeps, you hear the first one just about as the screen goes black, and then a pause, and then two more beeps together. Or sometimes one of them glitches and you actually hear three more.

But the screen stays dark!
User avatar
takanuva15
Posts: 24
Joined: 22 Nov 2019, 23:05
Location: USA
Contact:

Re: Can't put the Surface Book to sleep with DLLCall() because of S0 power state

30 Jan 2022, 21:25

LorenAmelang wrote:
31 Oct 2021, 21:27
Confused and Amazed - this actually works:
Hey, I just wanted to give an update on this (THANK YOU Loren for posting your script; it was the first thing that worked after hours of futile research). I did some debugging and was able to simplify LorenAmelang's script to work without the random SoundBeeps:

Code: Select all

#s Up::
Sleep 500
SendMessage, 0x112, 0xF170, 2,, Program Manager
return
Explanation: Basically, the SoundBeeps that were present in the previous working script were just serving as delays to give the user enough time to release the hotkeys before the actual SendMessage command went through. (You could actually replace all the SoundBeeps with just a single SoundBeep, 37, 300 before the SendMessage call and the previous script would still work). The problem with not including the Soundbeeps is that as soon as you pressed the hotkey, the SendMessage call would execute. Then, within a few ms, you would take your fingers off the keyboard to release the hotkey and those "key releases" would register as key-up events in Windows, and thus Windows would immediately cancel trying to go into sleep mode. I put a Sleep instead to give you time to release the hotkeys, and then the laptop goes into Sleep mode easily. I've discussed it further in my full answer in this post (please read it for more info and follow-up quirks).

Currently running AutoHotkey v1.1.33.10 on a Windows 10 machine. (Also testing v2 with v2.0-a133)
All code I post on the AutoHotkey forum is under the MIT License unless otherwise noted in the post. (Basically, it's free for anyone to use/modify, but please credit me if you decide to use it as part of another post or project that will be public.)
"The tomorrow of yesterday is today, so don't make an excuse to delay!"

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], newbieforever and 114 guests