Click to See Complete Forum and Search --> : Getting System Uptime
ferox
August 1st, 2004, 03:15 PM
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. :)
Mick
August 1st, 2004, 05:57 PM
You can probably find better code....but...
http://blogs.msdn.com/gusperez/articles/91734.aspx
UnderDog
August 5th, 2004, 10:46 AM
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.
ferox
August 6th, 2004, 01:33 AM
Thanks UnderDog, Mick. :)
Mick
August 6th, 2004, 06:00 AM
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 :p
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/gettickcount.asp
UnderDog
August 6th, 2004, 06:09 AM
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 :D 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.
f1shrman
August 7th, 2004, 06:00 AM
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.
Mick
August 7th, 2004, 06:03 AM
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?
f1shrman
August 7th, 2004, 06:06 AM
Not enough coffee yet - sorry about - I saw the discussion about GetTickCount()....... and the rest is history.
Mick
August 7th, 2004, 06:09 AM
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.
AdaraCD
August 7th, 2004, 09:54 AM
another way using NtQuerySystemInformation:
#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;
}
Mick
August 7th, 2004, 10:53 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.