Times at which a local computer was turned on

Windows specific forum
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Times at which a local computer was turned on

Post by Little John »

Hi all!

For instance the tool TurnedOnTimesView by NirSoft provides detailed information about time ranges when a computer was turned on. It does do so by reading the system event log.

How can I get this information with a PureBasic program? I only want to get say the last 3 times when my local PC was turned on.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Times at which a local computer was turned on

Post by mk-soft »

Only Window ...

MSDN
https://docs.microsoft.com/en-us/window ... ttickcount
https://docs.microsoft.com/en-us/window ... ickcount64

Code: Select all

Import "" ; Kernel32.lib
  GetTickCount64()
EndImport

;cnt.i = (GetTickCount_() / 1000) & $FFFFFFFF
cnt.q = (GetTickCount64() / 1000)

days = cnt / 86400
cnt = cnt % 86400 
hours = cnt / 3600
cnt = cnt % 3600
minutes = cnt / 60
cnt = cnt % 60
seconds = cnt
Debug "" + days + "d " + hours + "h " + minutes + "m " + seconds + "s "
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re: Times at which a local computer was turned on

Post by BarryG »

Little John wrote:How can I get this information with a PureBasic program?
We need someone to update this -> https://www.purebasic.fr/english/viewto ... 69#p552969 Then we can parse it for the last 3 power on/off times.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Times at which a local computer was turned on

Post by Little John »

Yes, currently I need only a solution on Windows ( that's why I posted this question in the "Windows" subforum. ;-) )
However, GetTickCount_() does not provide the information I am looking for.
BarryG wrote:We need someone to update this -> https://www.purebasic.fr/english/viewto ... 69#p552969 Then we can parse it for the last 3 power on/off times.
That seems to put me on the right track, thank you!
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Times at which a local computer was turned on

Post by mk-soft »

Code: Select all

Import "" ; Kernel32.lib
  GetTickCount64()
EndImport

;cnt.i = (GetTickCount_() / 1000) & $FFFFFFFF
cnt.q = (GetTickCount64() / 1000)

TurnOn = Date() - (GetTickCount64() / 1000)
Debug FormatDate("Computer turn on: %YYYY-%MM-%DD %HH:%II:%SS", TurnOn)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Times at which a local computer was turned on

Post by Little John »

mk-soft wrote:

Code: Select all

Import "" ; Kernel32.lib
  GetTickCount64()
EndImport

;cnt.i = (GetTickCount_() / 1000) & $FFFFFFFF
cnt.q = (GetTickCount64() / 1000)

TurnOn = Date() - (GetTickCount64() / 1000)
Debug FormatDate("Computer turn on: %YYYY-%MM-%DD %HH:%II:%SS", TurnOn)
*Sigh*
Please read before "replying"!
Little John wrote:However, GetTickCount_() does not provide the information I am looking for.
If you want to know what I am looking for, read my first post in this thread thoroughly.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Times at which a local computer was turned on

Post by mk-soft »

Sorry,

I thought you wanted to know when the computer was started and then filter the data from date and time.

But I still find my code very helpful :mrgreen:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Everything
Enthusiast
Enthusiast
Posts: 224
Joined: Sat Jul 07, 2018 6:50 pm

Re: Times at which a local computer was turned on

Post by Everything »

-TurnedOnTimesView based on a few types of events on the event log of Windows
-If you clear your system event log, TurnedOnTimesView will not be able to detect the shutdown/startup times
-On systems prior to Windows Vista, Shutdown Reason, Shutdown Type, and Shutdown Process fields are usually empty

TurnedOnTimesView uses the following events to determine the turn off/turn on times:
EventID 41 (Microsoft-Windows-Kernel-Power): The system has rebooted without cleanly shutting down first. This error could be caused if the system stopped responding, crashed, or lost power unexpectedly.
EventID 42 (Microsoft-Windows-Kernel-Power): The system is entering sleep
EventID 1 (Microsoft-Windows-Power-Troubleshooter): The system has resumed from sleep.
EventID 1074 (USER32): The process xxx has initiated the xxx of computer xxx on behalf of user xxx for the following reason: xxx
EventID 6005 (EventLog): The Event log service was started.
EventID 6006 (EventLog): The Event log service was stopped
Windows event logs with PB
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Times at which a local computer was turned on

Post by Little John »

mk-soft wrote:But I still find my code very helpful :mrgreen:
I am glad that I was able to give you the opportunity to post offtopic blah blah and feel helpful.
dige
Addict
Addict
Posts: 1247
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Times at which a local computer was turned on

Post by dige »

mk-soft wrote:Sorry,

I thought you wanted to know when the computer was started and then filter the data from date and time.

But I still find my code very helpful :mrgreen:
@mk-soft: Since Windows only restarts on reboot, your value shows the time of the last reboot.
"Daddy, I'll run faster, then it is not so far..."
Post Reply