• Welcome to Valhalla Legends Archive.
 

How To: Get System Uptime

Started by Barabajagal, October 20, 2007, 03:54 PM

Previous topic - Next topic

Barabajagal

As most people discover or learn, GetTickCount can be used to determine the system uptime. Those of us who are more devout computer users also discover that after about 25 days, it breaks. Even if you convert the Long to unsigned, it still breaks after around 50 days. Because of this, I have created a function that I call GetTickDouble.


'Declares
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Public Declare Function GetTickCount Lib "kernel32" () As Long

'Code
Public Function GetTickDouble() As Double
Dim Cou As Currency
Dim Fre As Currency
    On Error GoTo Erred
    If QueryPerformanceCounter(Cou) And QueryPerformanceFrequency(Fre) Then
        GetTickDouble = Int(Cou / Fre * 1000)
    Else
        GetTickDouble = LongToUnsigned(GetTickCount)
    End If
Exit Function
Erred:
    GetTickDouble = 0
End Function

Private Function LongToUnsigned(Value As Long) As Double
    If Value < 0 Then
        LongToUnsigned = Value + 4294967296#
    Else
        LongToUnsigned = Value
    End If
End Function


Explanation:
QueryPerformanceCounter and QueryPerformanceFrequency are high quality timer APIs that allow for microsecond counting (6 decimals down from seconds). They're much more accurate at getting times. However, there is a problem with them if you have an older Dual Core processor and haven't installed the correct patch (Google it for more info). Not all OSs support QueryPerformance stuff, so I added GetTickCount and converted it to an Unsigned value just in case. As for the Currency types: QueryPerformance actually uses the Large_Integer type, but in VB6 you can use Currency instead so you don't have to deal with converting the LargeIntegers into a format you can use.

squiggly

- Posso usar um tradutor de língua, devo ser fresco agora!


squiggly

- Posso usar um tradutor de língua, devo ser fresco agora!

Barabajagal

Quite a few people don't know about it, sorry for trying to be helpful.

Warrior

Don't discourge Ripple when he does something productive.

Nice job.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

squiggly

Is this code PowerBASIC optimised?
- Posso usar um tradutor de língua, devo ser fresco agora!

Barabajagal

PB can handle 64 bit and unsigned variables just fine, has an entirely different error handling system, declares APIs automatically, etc... In PB, this entire thing would be much, much easier.

warz


UserLoser

Quote from: devcode on October 20, 2007, 09:26 PM
Quote from: squiggly on October 20, 2007, 04:27 PM
Quote from: squiggly on October 20, 2007, 03:54 PM
Guess who doesn't care

* squiggly raises hand

QPC is soooo 1992

lol.

Yeah, TS, this is extremely old to most of us (pretty much everyone) but thanks and good job anyhow

Dale