2

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://learn.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.

  • Isn't that information - if there is some API to get it - obsolete once you have it? Processes can all `SetThreadExecutionState` any time. (Much like the problem that "file-exists" is obsolete ones you have the result). – Christian.K Sep 14 '20 at 09:55
  • Your need is to be precise which process is preventing the computer from sleeping. But there are many conditions that prevent the computer from sleeping, such as mouse and keyboard input. Because the scope is too wide, this may be a difficult idea to realize. – Strive Sun Sep 15 '20 at 08:19
  • Except for user input, I want to know if the user is playing a game or watching video. 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. That's the goal. – James Cheng Sep 15 '20 at 15:20

1 Answers1

0

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:

Strive Sun
  • 5,988
  • 1
  • 9
  • 26