Kalvyn
asked on
obtaining the last boot timestamp
The following code will give me the last boot timestamp. Is there a C API call or registry location where I can get this info?
On Error Resume Next
strComputer = "."
'strComputer = "useaihp348"
'strComputer = "ihp130"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("S elect * from
Win32_OperatingSystem",,48 )
For Each objItem in colItems
Wscript.Echo "LastBootUpTime: " & objItem.LastBootUpTime Next
On Error Resume Next
strComputer = "."
'strComputer = "useaihp348"
'strComputer = "ihp130"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("S
Win32_OperatingSystem",,48
For Each objItem in colItems
Wscript.Echo "LastBootUpTime: " & objItem.LastBootUpTime Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Any feedback?
ASKER
Using normal counter logic to get the last boot timestamp would be a very complicated way of getting this information. Plus the current PDH methods for processing counter information is not available on some pre W2K OS levels. However, this reply did help me track a related way of getting this information. I changed my search criteria from "last boot" to "up time" per the counter documentation. This lead me to the QueryPerformanceCounter and QueryPerformanceFrequency calls for getting this counter information via API calls.
So I would give him some points for the help. BTW, my current code is as follows.
void GetLastBoot()
{
// This procedure:
// -Calculates system up time. Note that this is not exactly the same as // reported by SrvInfo but it is within a few seconds. // -Calculates last boot date/time. Note that if there was an odd number of // daylight savings time switches since the last boot date, this will be // one hour off.
char temp[256];
long uptime;
time_t seconds;
struct tm *local;
LARGE_INTEGER PerformanceCount;
LARGE_INTEGER Frequency;
if (QueryPerformanceCounter(& Performanc eCount))
{
if (QueryPerformanceFrequency (&Frequenc y))
{
time(&seconds);
uptime= PerformanceCount.QuadPart/ Frequency. QuadPart;
seconds= seconds- uptime;
local= localtime(&seconds);
sprintf(temp,"%02i/%02i/%0 4i %02i:%02i:%02i",
local->tm_mon+1,
local->tm_mday,
local->tm_year +1900,
local->tm_hour,
local->tm_min,
local->tm_sec);
cout << " LastBoot=" << temp << endl;
cout << " SystemUpTime=";
cout << (uptime/86400) << " Days ";
cout << (uptime%86400)/3600 << " Hr ";
cout << ((uptime%86400)%3600)/60 << " Min ";
cout << uptime%60 << " Sec" << endl;
}
else
cout << " Error getting last boot (QueryPerformanceFrequency ): " <<
GetLastError() << endl;
}
else
cout << " Error getting last boot (QueryPerformanceCounter): " <<
GetLastError() << endl;
} // end GetLastBoot
So I would give him some points for the help. BTW, my current code is as follows.
void GetLastBoot()
{
// This procedure:
// -Calculates system up time. Note that this is not exactly the same as // reported by SrvInfo but it is within a few seconds. // -Calculates last boot date/time. Note that if there was an odd number of // daylight savings time switches since the last boot date, this will be // one hour off.
char temp[256];
long uptime;
time_t seconds;
struct tm *local;
LARGE_INTEGER PerformanceCount;
LARGE_INTEGER Frequency;
if (QueryPerformanceCounter(&
{
if (QueryPerformanceFrequency
{
time(&seconds);
uptime= PerformanceCount.QuadPart/
seconds= seconds- uptime;
local= localtime(&seconds);
sprintf(temp,"%02i/%02i/%0
local->tm_mon+1,
local->tm_mday,
local->tm_year +1900,
local->tm_hour,
local->tm_min,
local->tm_sec);
cout << " LastBoot=" << temp << endl;
cout << " SystemUpTime=";
cout << (uptime/86400) << " Days ";
cout << (uptime%86400)/3600 << " Hr ";
cout << ((uptime%86400)%3600)/60 << " Min ";
cout << uptime%60 << " Sec" << endl;
}
else
cout << " Error getting last boot (QueryPerformanceFrequency
GetLastError() << endl;
}
else
cout << " Error getting last boot (QueryPerformanceCounter):
GetLastError() << endl;
} // end GetLastBoot
ASKER
jkr, since your reply pointed my college in the right direction i am awarding the points for your response.
I actually had the uptime in my mind :o)
Thank you!
Thank you!