C: How to check if the computer is locked?

Is there any function in C to check if a computer goes to sleep , hibernate or locked and wakes up from this state?

In msdn, they provided for C# , C++ , but not for C My OS is windows7

Below is the code that I use to check the length of time between starting a program and stopping it (turning off the system terminates the program, so you can measure the length of time in this way).

 #include <stdio.h> #include <stdlib.h> #include <conio.h> #include<time.h> clock_t start_time=0; void bye (void) { FILE *read,*write; write=fopen("F:\\count.txt","w"); clock_t end_time=clock(); fprintf(write,"Time: %d",(end_time-start_time)/CLOCKS_PER_SEC); fclose(write); } int main (void) { start_time=clock(); atexit (bye); //exit (EXIT_SUCCESS); getch(); } 

Similarly, I want to check if / sleep / hibernate is blocked.

One possible way to wrap C ++ code (provided in a link) in c, as pointed out by @ddriver

But is this not possible in C at all?

+2
source share
2 answers

WinAPI has at least the same features as the .NET framework. What you ask for is the PowerManagement API .

You will need to register to receive PowerSettingNotificationEvents using the RegisterPowerSettingNotification function. Unfortunately, it is used differently for a GUI application, where you give a window handle that will then receive a WM_POWERBROADCAST message every time the system is about to change state (one of the suspend modes or sleep mode), and for a non-GUI (usually this service) that registers a HandlerEx with the dwControl SERVICE_CONTROL_POWEREVENT parameter and dwEventType from PBT_POWERSETTINGCHANGE.

+5
source

The link you provide is the signals that are emitted when you change your diet. So, you can check when the system is about to fall asleep, or just woke up.

How to verify that the system is currently sleeping is simply not possible, as custom code simply will not work during deep sleep states. It may be some kind of platform-specific, very low level BIOS API, but they are usually not public and far from portable.

+2
source

All Articles