How to detect if any of the process prevents Windows from sleep?

Viewed 222

I want to check if any of the processes/applications use any approach to keep the windows system awake.

I find there is an API SetThreadExecutionState that can do this.

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate

Even I find this kind of API, I still cannot find a way to get the state.

I think video players will use SetThreadExecutionState to keep the system awake temporarily. But the document also said


Applications such as word processors, spreadsheets, browsers, and games do not need to call SetThreadExecutionState.


As a windows service, I wanna determine if they set the state ON or not including the Applications that didn't need to call SetThreadExecutionState.

1 Answers

The simplest way for me is to detect if there is any application use "certain" API to tell OS that don't sleep now even there is no user input.

This is actually not a very simple method, detours can do this, but the premise is that you need to spend a lot of time to learn how to use it.

Detours is a software package for monitoring and instrumenting API calls on Windows. Detours has been used by many ISVs and is also used by product teams at Microsoft. Detours is now available under a standard open source license (MIT). This simplifies licensing for programmers using Detours and allows the community to support Detours using open source tools and processes.

It is distributed in source code form: https://github.com/microsoft/Detours

Related:

Related