Results 1 to 11 of 11

Thread: windows uptime

  1. #1

    Thread Starter
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    windows uptime

    Hi,

    I would like to design similiar to the attached screenshot.

    How can I find windows uptime? Is there any api for that?

    Please give me your inputs.

    Thanks in advance.

    Kanna.
    Attached Images Attached Images  

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: windows uptime

    this should work:
    VB Code:
    1. Private Declare Function GetTickCount Lib "kernel32" () As Long
    2.  
    3. Private Sub Command1_Click()
    4.     Debug.Print GetWinUpTime
    5. End Sub
    6.  
    7. Private Function GetWinUpTime() As String
    8.     Dim lSec As Long
    9.     lSec = GetTickCount \ 1000
    10.     GetWinUpTime = Format$((lSec \ 3600) \ 24, "00 Days ") _
    11.                  & Format$((lSec \ 3600) Mod 24, "00 Hours ") _
    12.                  & Format$((lSec Mod 3600) \ 60, "00 Minutes ") _
    13.                  & Format$((lSec Mod 60), "00 Seconds ")
    14. End Function
    bear in mind that GetTickCount wraps round every 49 days

  3. #3
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: windows uptime

    Nice code Bush!!
    CS

  4. #4
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: windows uptime

    Quote Originally Posted by bushmobile
    bear in mind that GetTickCount wraps round every 49 days
    That can be detected by comparing the new value to the previous value.
    If the previous value is higher then a 'wrap' has occurred.
    You can then increment a counter to keep track of that.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: windows uptime

    Quote Originally Posted by bushmobile
    ...bear in mind that GetTickCount wraps round every 49 days
    You wouldn't have to worry about it if you use timer and datediff function:
    Some of you will say that VB's timer is off a bit and I'll say "So what..."
    VB Code:
    1. Option Explicit
    2.  
    3. Dim sStart As Date
    4.  
    5. Private Sub Form_Load()
    6.     sStart = Now()
    7.     Timer1.Interval = 1000
    8.     Timer1.Enabled = True
    9. End Sub
    10.  
    11. Private Sub Timer1_Timer()
    12. Dim iDays%, iHours%, iMinutes%, iSeconds&
    13.  
    14.     iSeconds = DateDiff("s", sStart, Now)
    15.     iMinutes = (iSeconds \ 3600) \ 24
    16.     iHours = (iSeconds \ 3600) Mod 24
    17.     iDays = (iSeconds Mod 3600) \ 60
    18.    
    19.     Label1.Caption = "elapsed time: " _
    20.                         & iDays & " days, " _
    21.                         & iMinutes & " minutes, " _
    22.                         & iSeconds & " seconds, "
    23.  
    24. End Sub

  6. #6
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: windows uptime

    You would need to start that right after Windows starts and never shut it down.

  7. #7

  8. #8
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Re: windows uptime

    if you want to do this in a program without the 49day problem, check the modified date of the windows swap file (usually c:\pagefile.sys )
    subtract from current date to get uptime.

  9. #9
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Re: windows uptime

    correction on the sample vb code. the iMinutes and iDays are swapped around,
    they should read
    Code:
    15.   iDays = (iSeconds \ 3600) \ 24
    16.   iHours = (iSeconds \ 3600) Mod 24
    17.   iMinutes = (iSeconds Mod 3600) \ 60

    and the seconds displayed should be str(iSeconds mod 60) not iSeconds,
    Code:
    22.                        & str(iSeconds mod 60) & " seconds, "
    unless you want a heck of a lot of seconds, in which case the minutes,hours and days would not be needed.

  10. #10

  11. #11
    New Member
    Join Date
    Oct 2010
    Posts
    3

    Re: windows uptime

    not so dead i didn't find it usefull :-) thanks for the post. i hadn't noticed the datediff function and just subtracting the numbers gives a 1 day error, where datediff does the job.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width