C:如何检查如果计算机被锁定/睡眠? [英] C: How to check if the computer is locked/sleep?

查看:338
本文介绍了C:如何检查如果计算机被锁定/睡眠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何C函数来检查计算机是否将睡觉休眠锁定并从这些状态唤醒?

MSDN 他们提供的 C# C ++ 而不是 C
我的操作系统是Windows7的

像下面是code我使用的检查程序启动和终止它的持续时间(关闭系统将终止程序,所以这种方式持续时间可以测量)。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&CONIO.H GT;
#包括LT&;&time.h中GT;
clock_t表示START_TIME = 0;
无效再见(无效)
{
    FILE *读,写*;
    写= FOPEN(F:\\\\ count.txt,W);
    clock_t表示END_TIME =时钟();
    fprintf中(写时间:%D(END_TIME-START_TIME)/ CLOCKS_PER_SEC);
    FCLOSE(写);
}INT主要(无效)
{
     START_TIME =时钟();
  的atexit(再见);
  //退出(EXIT_SUCCESS);
  残培();
}

在相同的方式,我想检查锁定/睡眠/休眠。

一种可能的方式来包装在C C ++ code(中提供的链接),如提及@ddriver

但是,它不可能用C呢?


解决方案

该WinAPI的具有一般至少相同的可能性,.NET框架。你的要求的是在<一个href=\"https://msdn.microsoft.com/en-us/library/windows/desktop/bb968807%28v=vs.85%29.aspx\">PowerManagement API 。

您必须注册才能获得与 RegisterPowerSettingNotification 功能PowerSettingNotificationEvents。不幸的是,它是用来以不同的方式,你给一个处理一个窗口,然后将收到 WM_POWERBROADCAST 消息每次系统即将改变状态的GUI应用程序(一暂停模式或休眠模式),而<对于与SERVICE_CONTROL_POWEREVENT的dwControl参数和PBT_POWERSETTINGCHANGE的dwEventType注册一个 HandlerEx 回调非GUI的(通常为服务)。 / p>

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?

解决方案

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.

这篇关于C:如何检查如果计算机被锁定/睡眠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆