4

Is there any function in C to check if the computer is going to sleep,hibernate or locked and waking up from these state?

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

Like below is the code I'm using to check the time duration between starting the program and terminating it(shutting down the system will terminate the program so this way time duration can be measured).

#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();
}

In the same way I want to check for locked/sleep/hibernate.

One possible way to wrap the c++ code(provided in the link) in c as mentioned by @ddriver

But is it not possible in C at all?

Community
  • 1
  • 1
Anurag Chakraborty
  • 421
  • 2
  • 5
  • 20
  • tricky question, it depends on what mode C code is running, OS used etc.. for example in sleep mode some processes (including the c code) will sleep as well, thus cannot runand check for sleep mode, furthermore sleep mode has several implementations to check depending on platform and OS – Nikos M. Aug 18 '15 at 18:37
  • How/where do you want the function to run when the processor is sleeping? – user3528438 Aug 18 '15 at 18:37
  • That's like how to check if you are dead. Would you be able to do that if you are dead? – dtech Aug 18 '15 at 18:39
  • the sample code you link to, uses events (or signals) from the OS to processes that power mode changed, again this depends on platform and OS – Nikos M. Aug 18 '15 at 18:39
  • Actually there's a way do it: check the system clock at known time intervals. If there's a big time gap between two measured system clock, then it means sometime between now and the previous measurement, the system went to sleep and woke up. If you choose your measuring interval to be smaller than the fastest possible sleep/wakeup cycle, this method could be very practically usable. – user3528438 Aug 18 '15 at 18:44
  • 1
    sorry i forgot to add the OS. updated the question. anyway it's msdn so OS is windows. – Anurag Chakraborty Aug 18 '15 at 18:45
  • hey why downvotes? can you explain please? – Anurag Chakraborty Aug 18 '15 at 18:53
  • You're reading the .NET documentation (it's not C++, it's C++/CLI) and not the [Windows API documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/aa372721%28v=vs.85%29.aspx). – cremno Aug 18 '15 at 19:06
  • @AnuragChakraborty - down voters do not often reveal themselves for whatever reason. (reprisal?). In any case, other than your code being a little off, I see no reason for the down votes. Your question is interesting, not often asked (I have never seen it) and one for which I think an answer to would be useful to many. +1 – ryyker Aug 18 '15 at 19:50
  • @user3528438, not good solution since it can go wrong for (large) number of reasons – Nikos M. Aug 18 '15 at 20:39

2 Answers2

7

The WinAPI has generally at least the same possibilities as .NET framework. What your are asking for is the PowerManagement API.

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

user3797758
  • 829
  • 1
  • 14
  • 28
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I must admit that this answer if far from complete, but it gives pointers to the MSDN and the functions, messages and callback types to use. A full answer would be way longer, and beyond my current capacity. Hope it helps to start some code so next questions will be more precise and easier to answer with more clear and documented responses. – Serge Ballesta Aug 18 '15 at 19:16
2

The link you provide is about signals, emitted when power mode is changing. So, obviously, you can check when the system is about to go to sleep, or it just woke up.

As of checking if the system currently sleeps, that is simply not possible, as user code will simply not be running during deep sleep states. Maybe some platform specific, very low level BIOS API, but those are usually not public, and far from portable.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • but in C# we are able to do it. In this site we have an answer for that. http://stackoverflow.com/questions/18206183/event-to-detect-system-wake-up-from-sleep-in-c-sharp So why not in C? – Anurag Chakraborty Aug 18 '15 at 18:58
  • @AnuragChakraborty - check again, it is not that. That link is about detecting when the system wakes up from sleeping, not about detecting if the system is sleeping... – dtech Aug 18 '15 at 19:01
  • BTW you can wrap the C++ code from the example from your answer to be usable from C. – dtech Aug 18 '15 at 19:02
  • sorry actually I want to compute the time computer is running effectively. So for that i need to know when it's going to sleep & when it's waking up – Anurag Chakraborty Aug 18 '15 at 19:03
  • @AnuragChakraborty well, in that case, you can use the C++ example code, wrap it into a C API using an opaque pointer and functions and use the power mode change signals to calculate the runtime properly. – dtech Aug 18 '15 at 19:06