Windows Keep Awake - 64bit

Windows specific forum
SniffTheGlove
Enthusiast
Enthusiast
Posts: 122
Joined: Sat Nov 19, 2011 6:51 pm

Windows Keep Awake - 64bit

Post by SniffTheGlove »

Hi,

For quite a while I have been using this procedure to keep PC's awake during program run.

Code: Select all

#ES_CONTINUOUS        = $80000000     
#ES_DISPLAY_REQUIRED  = $00000002
#ES_SYSTEM_REQUIRED   = $00000001

; ### Prevent Sleep
Procedure Set_SetThreadExecutionState( Flag.l )
  Define ReturnValue.l
  If OpenLibrary(0, "kernel32")
    ReturnValue.l = CallFunction(0, "SetThreadExecutionState", Flag )
    If ReturnValue.l = 0
      MessageRequester( "Error", "Lib call Failed" )
    EndIf
    CloseLibrary(0)
  Else
    MessageRequester( "Error", "Lib open Failed" )
  EndIf
EndProcedure



; ### Set to non-SLEEP
Set_SetThreadExecutionState(#ES_CONTINUOUS | #ES_DISPLAY_REQUIRED | #ES_SYSTEM_REQUIRED)
I am now moving the program onto 64bit exclusively and I am concerned how this might effect things as this procedure is using the 32bit Kernel Library.

The procedure still works on 64bit but I would like to know if there is a 64bit way of doing the same.

Thanks
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Windows Keep Awake - 64bit

Post by Thunder93 »

Don't let the filename fool you. Shifting to x64 PB IDE Compiler, it'll use \System32\kernel32.dll instead of \SysWOW64\kernel32.dll

They aren't the same;

44aac4307be433e5c730124eb9043543 \SysWOW64\kernel32.dll
1c9c6933a94c594de7366124b4dd6075 \System32\kernel32.dll
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Windows Keep Awake - 64bit

Post by Dude »

I've seen lots of apps in software sites that prevent sleeping and all they do is simulate a keypress periodically, like pressing Ctrl. They get good ratings from their users so I assume they must work. So, to prevent sleeping on Windows, it can be as simple as periodically doing this:

Code: Select all

keybd_event_(#VK_CONTROL,0,0,0) ; Key down.
keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Key up.
User avatar
Roger Hågensen
User
User
Posts: 47
Joined: Wed Mar 25, 2015 1:06 pm
Location: Norway

Re: Windows Keep Awake - 64bit

Post by Roger Hågensen »

@Dude

Holy hell. No, no no no no.

Ever stop and think? What happens if the user is typing in a chat and by chance he press the v key at the same time as the program sends Ctrl. Boom the users secret shame is pasted for the world to see.


MicroSoft created a API specifically for this which SniffTheGlove is correctly using.


I did't even know that sending ghost control events was a thing. No wonder people complain that windows is crappy if programs do shit like that.

@SniffTheGlove

Do you really need the display to be on?
ES_DISPLAY_REQUIRED should only be needed for video players or programs that continuously shows status reports or changing information to the user.

Do note that for media (like music and video) there exists other APIs that let you specify if the program is a media app, this should improve latency for audio and video (the process is classed as a media process).
Also note that for a video app you may want to ensure/allow the display to go black when the video is paused. Screen burn in is less of an issue now with LCD/LCD-LED/Plasma (unsure about OLED) than back in the old days of CRTs.
4 music albums under CC BY license available for free (any use, even commercial) at Skuldwyrm.no
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Windows Keep Awake - 64bit

Post by netmaestro »

@Roger: You've been spending too much time with that Rescator rogue! :lol:

Some potentially useful info here:https://msdn.microsoft.com/en-us/librar ... s.85).aspx
BERESHEIT
User avatar
Roger Hågensen
User
User
Posts: 47
Joined: Wed Mar 25, 2015 1:06 pm
Location: Norway

Re: Windows Keep Awake - 64bit

Post by Roger Hågensen »

netmaestro wrote:@Roger: You've been spending too much time with that Rescator rogue! :lol:
:P
netmaestro wrote:Some potentially useful info here:https://msdn.microsoft.com/en-us/librar ... s.85).aspx
What has the broadcast of the power status got to do with keybd_event, you confused me somewhat with this.
As far as I know keybd_event just blindly sends a keyboard event to the keyboard driver/system in general and not just that one window.
https://msdn.microsoft.com/en-us/librar ... p/ms646304
4 music albums under CC BY license available for free (any use, even commercial) at Skuldwyrm.no
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Windows Keep Awake - 64bit

Post by netmaestro »

Not a thing, I'm just looking at alternatives for a more professional approach to keeping an application alive when the OS tries to go to sleep. Responding to changes in the power status seems logical, as that's probably the whole purpose of the broadcast in the first place.
BERESHEIT
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Windows Keep Awake - 64bit

Post by Dude »

Roger Hågensen wrote:Ever stop and think? What happens if the user is typing in a chat and by chance he press the v key at the same time as the program sends Ctrl
Well, if you think about it, the idea is that the PC waits for an idle state first, and THEN does the simulated keystrokes to stop it actually sleeping. Thus, the user won't actively be typing anything during this period. Remember, the "keep awake" thing is for when nobody is using the PC at the time. ;)
User avatar
Roger Hågensen
User
User
Posts: 47
Joined: Wed Mar 25, 2015 1:06 pm
Location: Norway

Re: Windows Keep Awake - 64bit

Post by Roger Hågensen »

@Dude

One word (two actually): Race condition

At the moment the idle is triggered the user may type again.
if the code is slow, from the time the idle detection is triggered until your code send the fake key presses the user may already be holding down a key
4 music albums under CC BY license available for free (any use, even commercial) at Skuldwyrm.no
Post Reply