Click here to Skip to main content
14,607,863 members

Get the UpTime on a Windows System

Rate this:
2.48 (10 votes)
Please Sign up or sign in to vote.
2.48 (10 votes)
12 Apr 2005
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

Share

About the Author

dmihailescu
Software Developer (Senior)
United States United States
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.

Comments and Discussions

 
QuestionNot working on Vista. Any ideas? Pin
FirmTools9-Jun-07 5:07
MemberFirmTools9-Jun-07 5:07 
AnswerRe: Not working on Vista. Any ideas? [modified] Pin
Markus Mayer2-Jul-07 12:09
MemberMarkus Mayer2-Jul-07 12:09 
GeneralRe: Not working on Vista. Any ideas? Pin
_Leo_15-Dec-08 11:11
Member_Leo_15-Dec-08 11:11 
GeneralMay fail when changing system clock Pin
peterchen14-Jan-10 23:38
Memberpeterchen14-Jan-10 23:38 
GeneralMemory leak - bugs in code Pin
Gregory Morse10-May-06 7:57
MemberGregory Morse10-May-06 7:57 
GeneralRe: Memory leak - bugs in code Pin
Markus Mayer2-Jul-07 13:06
MemberMarkus Mayer2-Jul-07 13:06 
GeneralYou are right, I can't fix the article so here is the update Pin
dmihailescu2-Jan-08 6:16
Memberdmihailescu2-Jan-08 6:16 
GeneralGood Example but needs work... Pin
Panic2k34-May-05 17:23
MemberPanic2k34-May-05 17:23 
GeneralPretty Good! Pin
WREY12-Apr-05 5:16
MemberWREY12-Apr-05 5:16 
Generaltry GetSystemTimes Pin
ejor11-Apr-05 22:00
Memberejor11-Apr-05 22:00 
GeneralRe: try GetSystemTimes Pin
dmihailescu12-Apr-05 3:43
Memberdmihailescu12-Apr-05 3:43 
GeneralRe: try GetSystemTimes Pin
prcarp16-Jul-12 7:09
Memberprcarp16-Jul-12 7:09 
Also, you also need to divide by number of processors.

#define _FT_SECOND ((__int64) 10000000)
#define _FT_MINUTE (60 * _FT_SECOND)
#define _FT_HOUR   (60 * _FT_MINUTE)
#define _FT_DAY    (24 * _FT_HOUR)

	SYSTEM_INFO sysinfo;
	::GetSystemInfo(&sysinfo);

	FILETIME idletime, kerneltime, usertime;
	::GetSystemTimes(&idletime, &kerneltime, &usertime);

	int kt = (int)((((__int64)kerneltime.dwHighDateTime << 32) + kerneltime.dwLowDateTime) / _FT_SECOND);
	int it = (int)((((__int64)idletime.dwHighDateTime << 32) + idletime.dwLowDateTime) / _FT_SECOND);
	uptime = (kt + it) / sysinfo.dwNumberOfProcessors;	// system uptime in seconds

GeneralGetTickCount Pin
CHR17136-Apr-05 19:45
MemberCHR17136-Apr-05 19:45 
GeneralRe: GetTickCount Pin
dmihailescu7-Apr-05 9:17
Memberdmihailescu7-Apr-05 9:17 
GeneralRe: GetTickCount Pin
David Crow20-Apr-05 3:44
MemberDavid Crow20-Apr-05 3:44 
Generalresource.hm Pin
Roger656-Apr-05 11:40
MemberRoger656-Apr-05 11:40 
GeneralRe: resource.hm Pin
dmihailescu7-Apr-05 9:50
Memberdmihailescu7-Apr-05 9:50 

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

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

Article
Posted 6 Apr 2005

Stats

80.2K views
1.3K downloads
23 bookmarked