CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2004
    Posts
    81

    Getting System Uptime

    I am stucked with searching MSDN and google but didn't find an appropriate way to find the uptime for a system. This time can be Current Time - Boot Time or the time for which the primary LAN is enabled. Anyone here knows such an API to do it. How can I get the boot time for a system?
    Thanks in advance for any cooperation.
    while(true)
    cout<<"C++ is divine\n";

    Feroz Zahid

  2. #2
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    You can probably find better code....but...

    http://blogs.msdn.com/gusperez/articles/91734.aspx

  3. #3
    Join Date
    Jun 2004
    Location
    India
    Posts
    432
    Why wouldn't a one line code of

    int nSysUpTime = GetTickCount() / 1000;

    do the job?

    That should work for windows, Unix should have a similar call.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  4. #4
    Join Date
    Feb 2004
    Posts
    81
    Thanks UnderDog, Mick.
    while(true)
    cout<<"C++ is divine\n";

    Feroz Zahid

  5. #5
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Quote Originally Posted by UnderDog
    Why wouldn't a one line code of

    int nSysUpTime = GetTickCount() / 1000;

    do the job?
    No. Unless your saying in a subtle way windows will never run that long

    http://msdn.microsoft.com/library/de...ttickcount.asp

  6. #6
    Join Date
    Jun 2004
    Location
    India
    Posts
    432
    You are right Mick on both the counts. The timer will wrap around in 49 days and for most apps I have worked on, the reboot cycle happens much before that Well actually the wrap around could be just minutes away depending on how long the comp has been up when one starts the app. So we have to put code to deal with that.

    Detecting wrap around is a simple thing. Just maintain a counter of how many times it has wrapped around. Initialise it to 0. Incerement it each time you see that the GetTickCount is less than the last value you got. So it would no longer be one line of code.
    Last edited by UnderDog; August 6th, 2004 at 06:11 AM.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  7. #7
    Join Date
    Aug 2004
    Posts
    184
    Here is a different way to retrieve the uptime info - it uses the perf counters.

    http://blogs.msdn.com/gusperez/articles/91734.aspx

    Microsoft's uptime utility appears to use the event log to determine the system uptime - I am not sure how they do this, but I would guess they get the most recent eventlog start event in the system log to determine this.
    Last edited by f1shrman; August 7th, 2004 at 06:01 AM. Reason: missed a word

  8. #8
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Quote Originally Posted by f1shrman
    Here is a different way to retrieve the uptime info - it uses the perf counters.

    http://blogs.msdn.com/gusperez/articles/91734.aspx

    Microsoft's uptime utility appears to use the event log to determine the system uptime - I am not sure how they do this, but I would guess they get the most recent eventlog start event in the system log to determine this.
    psst...you think maybe my original post and yours look kinda alike??? hmm could it be the link?

  9. #9
    Join Date
    Aug 2004
    Posts
    184
    Not enough coffee yet - sorry about - I saw the discussion about GetTickCount()....... and the rest is history.

  10. #10
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Quote Originally Posted by f1shrman
    Not enough coffee yet - sorry about - I saw the discussion about GetTickCount()....... and the rest is history.
    Yep..but I'm quite sure I do that more than you before my cup of java Coffee is over -----------> there.

  11. #11
    Join Date
    Mar 2002
    Posts
    350
    another way using NtQuerySystemInformation:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <Windows.h>
    #include <tchar.h>
    #include <wchar.h>
    
    typedef LONG (WINAPI *pNtQuerySystemInformation)(UINT,PVOID,ULONG,PULONG);
       
    typedef struct _SYSTEM_TIME_OF_DAY_INFORMATION 
    {
        LARGE_INTEGER BootTime;
        LARGE_INTEGER CurrentTime;
        LARGE_INTEGER TimeZoneBias;
        ULONG CurrentTimeZoneId;
    } SYSTEM_TIME_OF_DAY_INFORMATION, *PSYSTEM_TIME_OF_DAY_INFORMATION;
    
    BOOL UpTime(SYSTEMTIME *pstBootTime)
    {
        pNtQuerySystemInformation       pfNtQuerySystemInformation;
        SYSTEM_TIME_OF_DAY_INFORMATION  SysTimeInfo;
        FILETIME                        ftBootTime;
    
        pfNtQuerySystemInformation = (pNtQuerySystemInformation)
            GetProcAddress(GetModuleHandle(_T("ntdll")),"NtQuerySystemInformation");
    
        if(!pfNtQuerySystemInformation)
            return FALSE;
    
        if(pfNtQuerySystemInformation(3,&SysTimeInfo,sizeof(SysTimeInfo),0) != NO_ERROR)
            return FALSE;
    
        ftBootTime = *(FILETIME *)&(SysTimeInfo.BootTime);
        FileTimeToLocalFileTime(&ftBootTime,&ftBootTime);
        FileTimeToSystemTime(&ftBootTime,pstBootTime);
        return TRUE;
    }
    
    int _tmain(int argc, TCHAR *argv[])
    {
        SYSTEMTIME stBootTime;
    
        if(UpTime(&stBootTime))
        {
            _tprintf(
                _T("System Uptime: %d/%d/%d %d:%d:%d\n"),
                stBootTime.wMonth,
                stBootTime.wDay,
                stBootTime.wYear,
                stBootTime.wHour,
                stBootTime.wMinute,
                stBootTime.wSecond);
            
            return 0;
        }
        return 1;
    }

  12. #12
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Good to see people paying attention. But you also need to pay attenion when using the Native Api (NtQuerySystemInformation). As in when structs change between OS releases and service packs.

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

Featured