Detecting, Possibly Keeping Computer from Going to Sleep/Suspend/Hibernation

I have a client app that needs to be up 24/7. I’m looking for any way to detect if a Windows computer is going to to go Sleep, Hibernation, Shutdown, Restart, or Suspend mode (did I get all the permutations?). In the ideal world I want to be able to keep that from happening.

I looked at the WindowsPowerStateMBS class from MBS and it doesn’t appear to work as advertised (at least in Windows 8.1). Anyone have any ideas/suggestions to look at?

Is there an equivalent command-line tool to the Mac’s caffeinate CLI perhaps?

It looks like there are Win API functions that should allow you to do this.

If this trick still works, it would solve the issue
http://community.spiceworks.com/topic/249162-is-there-a-registry-key-for-disabling-sleep-completely-on-xp

I just checked. Unfortunately, what I linked to is not valid anymore.

Well, WindowsPowerStateMBS is made to detect shutdown/sleep, so app can do things just before system goes to sleep.
You may need to configure windows manually to not go to sleep.

Return true in QuerySuspend may ask the system to not suspend, but well, nobody guarantees that.

this may do it:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx

with call like this:
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED)

The easiest solution would be to set the power management options such that the computer will not go to sleep, or otherwise turn itself down. It is pretty easy to do, but it requires that you know in advance and have an admin access to the computer.

In the run text box, type powercfg.cpl and press enter. You now have access to the power management options and can prevent disk spin-off, screen out, sleep, etc. You can also access the settings through the control panel.

Well, for now I can get the event that the computer is going to sleep and can at least log it. I think my client will be happy enough with that.

[quote=205954:@Christian Schmitz]this may do it:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx

with call like this:
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED)[/quote]

This is exactly what you use, using Windows Vista and above. For XP and below, use:
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)

Just tried this… seems to work (of course, use your own method for determining OS version):

[code] soft declare function SetThreadExecutionState Lib “kernel32” (ByVal esFlags as uInt32) as uInt32

const ES_AWAYMODE_REQUIRED = &h40
const ES_DISPLAY_REQUIRED = &h2
const ES_SYSTEM_REQUIRED = &h1
const ES_CONTINUOUS = &h80000000

dim result as uInt32

'if vista or newer (use your own method to determine windows version)
result = setThreadExecutionState(ES_CONTINUOUS or ES_SYSTEM_REQUIRED or ES_AWAYMODE_REQUIRED)

'if xp
'result = setThreadExecutionState(ES_CONTINUOUS or ES_SYSTEM_REQUIRED)[/code]

if result = 0, there was an error