.netPlus / Samples / VB

 

UpTime

UpTime screenshot

 

Filename: uptime.zip
Last update: 11/25/01
Size: 8 kb

 

Overview
The sample demonstrates how to determine for how long the system has been running (the uptime) from Visual Basic. When running on a Windows NT/2000 system, the code can also query the uptime for another Windows NT/2000 computer on the network. The information is retrieved from performance counters or using the GetTickCount API, depending on the platform it runs on.

 

Details
On Windows 9x/Me, the code uses the GetTickCount API. I think that's the best way of doing it, but if someone knows a better way, please let me know. Since GetTickCount returns the number of milliseconds since the system was started in a 32 bit integer (Long in VB), the information is only correct for about 50 days, after that the value starts over at zero. But who has seen a Windows 9x system run for more than 50 days anyway? :-)

On Windows NT/2000, the uptime is instead read from the System Up Time counter of the System performance object, a more reliable way of doing it. The code uses the Performance Data Helper library (pdh.dll) to query the counters. It's possible to do without it, by reading directly from the HKEY_PREFORMANCE_DATA key in the Registry, but it would require a lot more work. Pdh.dll is included with Windows 2000, and available as a reditributable component in the Platform SDK for Windows NT4. The counter returns the number of seconds in a 32 bit value (actually 64 bit, but only 32 bits seem to be valid), which should be more than enough (>100 years).

There is a slight difference in how the two methods work. GetTickCount returns the entire time difference between when the function is called and when the system was started, while the performance counter returns the time the system actually has been running. "So what's the difference?", you might ask. Well, before Windows 2000, there was probably no difference. But in Windows 2000, the time a computer is in hibernated state is not included by the performance counter, while it would be if GetTickCount was used.

 

See also
Performance Monitoring

 

©2000-2024, .netPlus