Jay
Wed Oct 24 19:44:51 PDT 2007
On Wed, 24 Oct 2007 17:15:21 -0700, "Karl E. Peterson" <karl@mvps.org>
wrote:
>Jay Freedman <jay.freedman@verizon.net> wrote:
>> On Tue, 23 Oct 2007 17:15:37 -0700, "Karl E. Peterson" <karl@mvps.org>
>> wrote:
>>
>>> H. Martins <HJRMartins@gmail.com> wrote:
>>>> What function should I use to get the total number of seconds since
>>>> computer started (or similar).
>>>
>>> If the computer's been up for less than ~43 days, the GetTickCount API would be
>>> the ticket. Of course, that's a hefty *if* there.
>>
>> I figured if it was in there somewhere, you'd know about it. :-)
>>
>> The GetTickCount function returns the number of milliseconds since
>> startup. (According to the documentation at
>>
http://msdn2.microsoft.com/en-us/library/ms724408.aspx, it wraps
>> around to 0 after 49.7 days.)
>
>Ah, 49 days. Sounds more likely, yeah. But there's a catch there. <G> Hint:
>
> ?49.7 * 24 * 60 * 60 * 1000, 2^32
> 4294080000 4294967296
>
>In VB, the return value from GetTickCount "goes negative" at 2^31, which means you
>need to twiddle that signbit (add 2^32 if retval < 0) if you want it to make sense.
>
>Another option, although *far* more complicated for nearly any purpose other than
>curiosity, would be to query the registry like the Uptime utility does.
>
> Uptime.exe Tool Allows You to Estimate Server Availability with Windows NT 4.0
>SP4 or Higher
>
http://support.microsoft.com/kb/232243
>
Actually, according to that page, Uptime.exe "depends on the Event log
for the data it uses to calculate availability." Another page explains
that "although you use the registry to collect performance data, the
data is not stored in the registry database. Instead, calling the
registry functions with the HKEY_PEFORMANCE_DATA key causes the system
to collect the data from the appropriate system object managers."
Apparently you call the RegQueryValueEx function and that in turn asks
the EventLog service for the value. Yeah, it's probably too much work.
>> Here's a VBA sample:
>>
>> Private Declare Function Win32GetTickCounter Lib "Kernel32" _
>> Alias "GetTickCount" () As Long
>>
>> Sub demo3()
>> Dim TickValue As Long
>> Dim ComputerHours As Long
>> Dim ComputerMinutes As Long
>> Dim ComputerSeconds As Long
>>
>> TickValue = Win32GetTickCounter
>>
>> ComputerHours = (TickValue \ 3596400) Mod 24
>> ComputerMinutes = (TickValue \ 59940) Mod 60
>> ComputerSeconds = (TickValue \ 999) Mod 60
>>
>> MsgBox "This computer has been on for " & _
>> CStr(ComputerHours) & " hours, " & _
>> CStr(ComputerMinutes) & " minutes, " & _
>> CStr(ComputerSeconds) & " seconds"
>> End Sub
>
>Where'd you get those numbers? That doesn't seem to be getting me where I want to
>go. Here's how I typically break down milliseconds into H:MM:SS...
>
Where does anyone get weird numbers? I copied something off the web
without thinking about it too hard. ;-) It looks like 3596400 =
3600000 - 600, and something similar for the others. It might even
work...
> Public Function FormatHMS(ByVal Milliseconds As Long, Optional ShowDecimal As
>Boolean) As String
> Dim Rtn As String
> Dim h As Long, m As Long, s As Long, d As Long
>
> h = Milliseconds \ 3600000 'hours
> m = (Milliseconds \ 60000) Mod 60 'minutes
> s = (Milliseconds \ 1000&) Mod 60 'seconds
> d = Milliseconds Mod 1000& 'factional seconds
>
> Rtn = Format$(h, "00") & ":" & _
> Format$(m, "00") & ":" & _
> Format$(s, "00")
> If ShowDecimal Then
> Rtn = Rtn & "." & Format$(d, "000")
> End If
> FormatHMS = Rtn
> End Function
>
>Thanks... Karl
Yeah, I'd go with that.
Thanks to you, too.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:
http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.