【问题标题】:How do we find out how long it's been since we pressed the power button on a computer?我们如何知道我们按下计算机上的电源按钮有多久了?
【发布时间】:2017-10-17 16:20:46
【问题描述】:

我尝试了 Windows API 函数,但我不明白哪一个符合我的要求。比如GetTickCount64QueryInterruptTime等。我该如何计算呢?

【问题讨论】:

  • 我总是使用网络适配器的持续时间。也许你能得到那个?
  • 您确定需要像“自按下电源按钮后的时间”这样的具体信息,而不是像“自上次启动后的时间”这样更合理的信息吗?
  • 您无法确定上次按下电源按钮的时间。据我们所知,一台机器甚至可能没有电源按钮。如果你想问一个不同的问题,edit它。

标签: c++ c windows winapi


【解决方案1】:

GetTickCount64 返回自上次启动以来的毫秒数。应该够了。

【讨论】:

    【解决方案2】:

    您也可以使用GetTickCount()。像这样:

    #include <Windows.h>
    #include <stdio.h>
    
    int main(){
    
        int hours;
        int min;
        int sec;
        int rem1;
    
        int nSysUpTime = GetTickCount() / 1000;
        int days = nSysUpTime / 60 / 60 / 24;
        hours = nSysUpTime / 3600;
        rem1 = nSysUpTime % 3600;
        min = rem1 / 60;
        sec = rem1 % 60;
    
    
        printf( "\nComputer Uptime   %02d:%02d:%02d \n\n",hours , min  ,sec  );
    
    
    return 0;
    }
    

    【讨论】:

    • GetTickCount() 每连续运行 49.7 天返回 0。计算机的运行时间可能比这长得多。请改用GetTickCount64()
    猜你喜欢
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    相关资源
    最近更新 更多