Asked by:
How to get data that are in powercfg / requests?

Question
-
All replies
-
Hi, semiromid
The command powercfg -requests provide a list of processes that are currently preventing a Windows 10 device from automatically entering sleep mode.
However, the command requires administrator permissions..
And there is currently no API to get a list of processes , but you can use CreateProcess to spawn powercfg and then parse its output. That will not address the permissions issue, though. Some things can only be done by an admin.
In addition, can you tell me what the purpose of the data is?
Best regards,
Strive
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Strive Sun-MSFTMicrosoft contingent staff Thursday, August 22, 2019 6:27 AM
-
-
Hi, sermiromid
If you have administrator privileges, you can use CreateProcess to get data.
Because even if you do something else, you can't get this data without administrator privileges.
This is a demo to refer:
#include <Windows.h> #include <string> int main() { BOOL ok = TRUE; HANDLE hStdInPipeRead = NULL; HANDLE hStdInPipeWrite = NULL; HANDLE hStdOutPipeRead = NULL; HANDLE hStdOutPipeWrite = NULL; // Create two pipes. SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; ok = CreatePipe(&hStdInPipeRead, &hStdInPipeWrite, &sa, 0); if (ok == FALSE) return -1; ok = CreatePipe(&hStdOutPipeRead, &hStdOutPipeWrite, &sa, 0); if (ok == FALSE) return -1; // Create the process. STARTUPINFO si = { }; si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdError = hStdOutPipeWrite; si.hStdOutput = hStdOutPipeWrite; si.hStdInput = hStdInPipeRead; PROCESS_INFORMATION pi = { }; LPCWSTR lpApplicationName = L"C:\\Windows\\System32\\cmd.exe"; LPWSTR lpCommandLine = (LPWSTR)L"C:\\Windows\\System32\\cmd.exe /c powercfg -requests"; LPSECURITY_ATTRIBUTES lpProcessAttributes = NULL; LPSECURITY_ATTRIBUTES lpThreadAttribute = NULL; BOOL bInheritHandles = TRUE; DWORD dwCreationFlags = 0; LPVOID lpEnvironment = NULL; LPCWSTR lpCurrentDirectory = NULL; ok = CreateProcess( lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttribute, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, &si, &pi); if (ok == FALSE) return -1; // Close pipes we do not need. CloseHandle(hStdOutPipeWrite); CloseHandle(hStdInPipeRead); // The main loop for reading output from the DIR command. char buf[1024 + 1] = { }; DWORD dwRead = 0; DWORD dwAvail = 0; ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL); while (ok == TRUE) { buf[dwRead] = '\0'; OutputDebugStringA(buf); puts(buf); ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL); } // Clean up and exit. CloseHandle(hStdOutPipeRead); CloseHandle(hStdInPipeWrite); DWORD dwExitCode = 0; GetExitCodeProcess(pi.hProcess, &dwExitCode); return dwExitCode; }
Best regards,
Strive
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- Edited by Strive Sun-MSFTMicrosoft contingent staff Thursday, August 22, 2019 10:17 AM
-
Maybe decompile "powercfg" and see how this function works?
It calls PowerInformationWithPrivileges which calls NtPowerInformation with GetPowerRequestList parameter,
but it is not documented...
- Edited by Castorix31 Thursday, August 22, 2019 11:24 AM
-
Hi, sermiromid
If you have administrator privileges, you can use CreateProcess to get data.
Because even if you do something else, you can't get this data without administrator privileges.
This is a demo to refer:
#include <Windows.h> #include <string> int main() { BOOL ok = TRUE; HANDLE hStdInPipeRead = NULL; HANDLE hStdInPipeWrite = NULL; HANDLE hStdOutPipeRead = NULL; HANDLE hStdOutPipeWrite = NULL; // Create two pipes. SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; ok = CreatePipe(&hStdInPipeRead, &hStdInPipeWrite, &sa, 0); if (ok == FALSE) return -1; ok = CreatePipe(&hStdOutPipeRead, &hStdOutPipeWrite, &sa, 0); if (ok == FALSE) return -1; // Create the process. STARTUPINFO si = { }; si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdError = hStdOutPipeWrite; si.hStdOutput = hStdOutPipeWrite; si.hStdInput = hStdInPipeRead; PROCESS_INFORMATION pi = { }; LPCWSTR lpApplicationName = L"C:\\Windows\\System32\\cmd.exe"; LPWSTR lpCommandLine = (LPWSTR)L"C:\\Windows\\System32\\cmd.exe /c powercfg -requests"; LPSECURITY_ATTRIBUTES lpProcessAttributes = NULL; LPSECURITY_ATTRIBUTES lpThreadAttribute = NULL; BOOL bInheritHandles = TRUE; DWORD dwCreationFlags = 0; LPVOID lpEnvironment = NULL; LPCWSTR lpCurrentDirectory = NULL; ok = CreateProcess( lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttribute, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, &si, &pi); if (ok == FALSE) return -1; // Close pipes we do not need. CloseHandle(hStdOutPipeWrite); CloseHandle(hStdInPipeRead); // The main loop for reading output from the DIR command. char buf[1024 + 1] = { }; DWORD dwRead = 0; DWORD dwAvail = 0; ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL); while (ok == TRUE) { buf[dwRead] = '\0'; OutputDebugStringA(buf); puts(buf); ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL); } // Clean up and exit. CloseHandle(hStdOutPipeRead); CloseHandle(hStdInPipeWrite); DWORD dwExitCode = 0; GetExitCodeProcess(pi.hProcess, &dwExitCode); return dwExitCode; }
Best regards,
Strive
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Hi, Strive Sun-MSFT
Hi, Castorix31
Thank you for your helping and attention!
It may be possible to get a similar function through screensaver?
Before the screensaver turns on, it checks whether it can be turned on or not.
How does a screen saver check if it can be turned on or not?
This is similar topowercfg / requests
Best regards,
Semiromid
-
Hi, semiromid
Do you try SC_SCREENSAVE ?
SC_SCREENSAVE :Executes the screen saver application specified in the [boot] section of the System.ini file.
You can intercept SC_SCREENSAVE message to check if screen saver turn on or not.
For more details , please refer this case.
Best regards,
Strive
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
-
Hi, semiromid
Do you try SC_SCREENSAVE ?
SC_SCREENSAVE :Executes the screen saver application specified in the [boot] section of the System.ini file.
You can intercept SC_SCREENSAVE message to check if screen saver turn on or not.
For more details , please refer this case.
Best regards,
Strive
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Hi, Strive Sun-MSFT
Thank you for your helping and attention!
How is the idle (inactivity, inaction) mode defined in "screensaver PC"?
How is the idle (inactivity, inaction) mode defined in "windows task scheduler"?
I want to determine if the computer is currently in idle (inactivity, inaction) mode or not.
For this, I wanted to use powercfg / requests, that check it, when there are no obstacles to putting the computer: into sleep mode or turning on the screen saver or entering the inactive (inactivity, inaction) mode PC.
Goal: Determine Inactive Mode in PC (Is he now or is he not)
Inactive mode in the PC: this is the state of the computer when it can go into sleep mode or turn on the screen saver.
As example:
1. I set a timer to 60 seconds
2. After every 60 seconds, I need to find out if the computer is idle (inactivity, inaction) or not.
Best regards,
Semiromid- Edited by semiromid Saturday, August 24, 2019 12:31 AM
-
Hi, semiromid
Do you have to use powercfg / requests for detection?
There's actually another way to detect whether a computer is in sleep mode.
When the system enters sleep, it will automatically preserve the state of the entire system and all applications. Therefore, most applications do not need to take any special action. Applications that need to perform specific actions before the system transitions can register for power events.
When the system sends a PBT_APMSUSPEND event, each application has two seconds to perform any necessary actions before the system starts the transition to sleep. Applications must limit what action they take in response to this event to ensure that they complete all operations in the time allotted.
Refer : Entering Sleep
So you can listen for the WM_POWERBROADCAST message in your WndProc handler
case WM_POWERBROADCAST: { if (PBT_APMSUSPEND == wParam) { //Go to Sleep } }
For screen savers, you can use the method I mentioned above.
Best regards,
Strive
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
Hi, semiromid
Do you have to use powercfg / requests for detection?
There's actually another way to detect whether a computer is in sleep mode.
When the system enters sleep, it will automatically preserve the state of the entire system and all applications. Therefore, most applications do not need to take any special action. Applications that need to perform specific actions before the system transitions can register for power events.
When the system sends a PBT_APMSUSPEND event, each application has two seconds to perform any necessary actions before the system starts the transition to sleep. Applications must limit what action they take in response to this event to ensure that they complete all operations in the time allotted.
Refer : Entering Sleep
So you can listen for the WM_POWERBROADCAST message in your WndProc handler
case WM_POWERBROADCAST: { if (PBT_APMSUSPEND == wParam) { //Go to Sleep } }
For screen savers, you can use the method I mentioned above.
Best regards,
Strive
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Hi, Strive Sun-MSFT
Thank you for your helping and attention!
Do you have to use powercfg / requests for detection?There's actually another way to detect whether a computer is in sleep mode.
I do not need to detection whether the computer is in sleep mode or whether it is going to go into it.
I need to detected the state of a computer when it is in an inactive state (inactivity, inaction, standby).
Idle states are not sleep states
Inactive state (Idle):
When the user does not move the mouse;
When does not press a key on the keyboard;
When the video does not play, the game does not start, etc.
As an example, what is an inactive (inactive, inactive, Idle) state:
when there are no obstacles to entering sleep mode or turning on the screen saver.
I could detect inactivity states (standby time, downtime, inactivity, inactivity) of the computer using the “Windows Task Scheduler”, but the determination of the state occurs with a delay of 4 minutes.Detecting the Idle State
In Windows 8, Task Scheduler performs the same general user absence and resource consumption checks. However, Task Scheduler relies on the operating system power subsystem to detect user presence. By default, the user is considered absent after four minutes of no keyboard or mouse input. The resource consumption verification time is shortened to 10 minute intervals when the user is present. When the user is away, the verification time is shortened to 30 second intervals. Task Scheduler makes additional resource consumption checks for the following events:
How can I find out when the computer is in standby mode (inactive)?
I would like to see this in c #
if this is not possible, then c ++ or any other languageBest regards,
Semiromid -
Hi, semiromid
I searched for some information, hoping to help you.
I need to detected the state of a computer when it is in an inactive state (inactivity, inaction, standby).
Such as when the user does not move the mouse, when does not press a key on the keyboard;
I think there is no out-of-idle notification. Best you can do is monitor the mouse/keyboard activity yourself using SetWindowHookEx() or RegisterRawInputDevices()
Refer: How to detect out of idle state in windows?
How can I find out when the computer is in standby mode (inactive)?
I would like to see this in c #
After my observation,I think the best way to check whether the computer is in idle mode is to check if the screen saver works.
Sorry , I'm not familiar with C#. Here is a C# sample, you can refer.
The useful information I can find for you.
Link: How can I know when Windows is going into/out of sleep or Hibernate mode?
Link: System Sleep Criteria
Best regards,
Strive
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- Edited by Strive Sun-MSFTMicrosoft contingent staff Wednesday, August 28, 2019 8:46 AM
- Proposed as answer by Strive Sun-MSFTMicrosoft contingent staff Monday, September 16, 2019 8:16 AM