I tried the Windows API functions, but I did not understand which one meets my request. Like GetTickCount64
, QueryInterruptTime
etc. How can I calculate it?
Asked
Active
Viewed 166 times
-5
-
I always use the duration from the network adapter. Maybe you can get that? – NathanOliver Oct 17 '17 at 16:22
-
3Possible duplicate of [Programmatically getting system boot up time in c++ (windows)](https://stackoverflow.com/questions/10853985/programmatically-getting-system-boot-up-time-in-c-windows) – ZerosAndOnes Oct 17 '17 at 16:29
-
Are you really sure you need something as specific as "time since power button was pressed" rather than something more sensible like "time since last boot"? – glennsl Oct 17 '17 at 16:32
-
There is no way for you to determine the last time the power button was pressed. For all we know, a machine may not even have a power button. If you meant to ask a different question, [edit](https://stackoverflow.com/posts/46794795/edit) it. – IInspectable Oct 17 '17 at 17:02
2 Answers
-2
You can also use GetTickCount()
. Like this:
#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;
}

Software_Designer
- 8,490
- 3
- 24
- 28
-
2`GetTickCount()` wraps back to 0 every 49.7 days of continuous running. A computer may run much longer than that. Use `GetTickCount64()` instead. – Remy Lebeau Oct 18 '17 at 14:54