Click here to Skip to main content
Licence 
First Posted 6 Apr 2005
Views 53,619
Bookmarked 23 times

Get the UpTime on a Windows System

By | 12 Apr 2005 | Article
Get the uptime on a Windows system.

Sample Image - stayon.png

Introduction

Some may have heard about a Unix command that would display the time the server has been running. Windows won't give you such a command but you can easily put together an application that does just that. Sometimes you might be in situation when your application relies on some resources like services or other apps that are slow to start and can't be synchronized against easily. The uptime might be the last resort in implementing a delay under such conditions, to allow your application to wait until all resources are properly initialized.

Background

Windows allows a wide range of queries to be performed on the machine and processes. This functionality is found in pdh.dll, and we are going to use it to retrieve the UpTime.

Using the code

We will add a counter, will collect the data, format it and close the query.

The code is self-explanatory:

    PDH_STATUS  status;
    HQUERY        perfQuery = NULL;
    HCOUNTER    uptimeCounter;
    char        uptimeCounterPath[] = "\\\\.\\System\\System Up Time";
    PDH_FMT_COUNTERVALUE uptimeValue;
    //.......................
    seconds = 0;
    //
    // Create a PDH query
    //
    if( PdhOpenQuery(NULL, 0, perfQuery ) != ERROR_SUCCESS )
        return FALSE;

    //
    // Associate the uptime counter with the query
    //
    status = PdhAddCounter(perfQuery, uptimeCounterPath,
                            0, &uptimeCounter );
    if( status != ERROR_SUCCESS )
        return FALSE;


    status = <CODE>PdhCollectQueryData</CODE>( perfQuery );
    if( status != ERROR_SUCCESS )
        return FALSE;

    //
    // Get the formatted counter value
    //

    status = <CODE>PdhGetFormattedCounterValue</CODE>( uptimeCounter, 
                    PDH_FMT_LARGE , NULL, &uptimeValue );
    if( status != ERROR_SUCCESS )
        return FALSE;

    //
    // Close the query
    //
    <CODE>PdhCloseQuery</CODE>( &perfQuery );

    seconds = (DWORD) (uptimeValue.largeValue);

Points of Interest

As an example of this functionality, I've decided to enhance an existing tool submitted by T.Yogaramanan on this site. I've added the ability to select the Time Origin for performing an action. You should be able to choose from the current moment and the UpTime (that's the BOOT radio button). For Windows NT, you might have to drop a pdh.dll file from Windows 2000 OS into the search path of the executable.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

dmihailescu

Software Developer (Senior)

United States United States

Member

Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNot working on Vista. Any ideas? PinmemberFirmTools5:07 9 Jun '07  
AnswerRe: Not working on Vista. Any ideas? [modified] PinmemberMarkus Mayer12:09 2 Jul '07  
GeneralRe: Not working on Vista. Any ideas? Pinmember_Leo_11:11 15 Dec '08  
GeneralMay fail when changing system clock Pinmemberpeterchen23:38 14 Jan '10  
GeneralMemory leak - bugs in code PinmemberSnatch7:57 10 May '06  
GeneralRe: Memory leak - bugs in code PinmemberMarkus Mayer13:06 2 Jul '07  
GeneralYou are right, I can't fix the article so here is the update Pinmemberdmihailescu6:16 2 Jan '08  
GeneralGood Example but needs work... PinmemberPanic2k317:23 4 May '05  
GeneralPretty Good! PinmemberWREY5:16 12 Apr '05  
Generaltry GetSystemTimes Pinmemberejor22:00 11 Apr '05  
GeneralRe: try GetSystemTimes Pinmemberdmihailescu3:43 12 Apr '05  
GeneralGetTickCount PinmemberCHR171319:45 6 Apr '05  
GeneralRe: GetTickCount Pinmemberdmihailescu9:17 7 Apr '05  
GeneralRe: GetTickCount PinmemberDavidCrow3:44 20 Apr '05  
Generalresource.hm PinmemberRoger6511:40 6 Apr '05  
GeneralRe: resource.hm Pinmemberdmihailescu9:50 7 Apr '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120515.1 | Last Updated 12 Apr 2005
Article Copyright 2005 by dmihailescu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid