Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

GetUpTime made easy with PowerTime object and TimeDiff method

  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin/PBCC GetUpTime made easy with PowerTime object and TimeDiff method

    It easy to get the UpTime from your Windows servers and/or workstations using this function.
    I hope that could help others.

    Click image for larger version

Name:	GetUpTime.png
Views:	248
Size:	2.8 KB
ID:	813697

    to use the function

    ' GetUpTime
    Local lBootTime As String
    Local lCurrentTime As String
    Local lUpTime As String
    If GetUpTime(lBootTime, lCurrentTime, lUpTime) = %TRUE Then
       ' do what you want with these 3 variables
       ' see example in the picture
    End If

        BootTime    As Filetime
        CurrentTime As Filetime
    End Type
    Declare Function NtQuerySystemInformation Lib "NtDLL.DLL" Alias "NtQuerySystemInformation"( _
    ByVal SystemInformationClass  As Dword, _
    ByVal SystemInformation       As Dword, _
    ByVal SystemInformationLength As Dword, _
    ByRef ReturnLength            As Dword) As Long
    Function GetUpTime(ByRef pBootTime As String, ByRef pCurrentTime As String, ByRef pUpTime As String) As Long
        Local lGetUpTime As Long : lGetUpTime = %FALSE
        Local lSystemTimeInformation As SYSTEM_TIMEOFDAY_INFORMATION
        ' UpTime
        Local lSign, lDays, lHours, lMinutes, lSeconds As Long        
        ' SystemTime
        Local lSTBootTime    As SystemTime
        Local lSTCurrentTime As SystemTime
        ' IPowerTime
        Local IPTBootTime    As IPowerTime : Let IPTBootTime    = Class "PowerTime"
        Local IPTCurrentTime As IPowerTime : Let IPTCurrentTime = Class "PowerTime"                          
        ' Use NtQuerySystemInformation to retrieve BootTime and CurrentTime information            
        If NtQuerySystemInformation(%SystemTimeInformation, VarPtr(lSystemTimeInformation), SizeOf(lSystemTimeInformation), %Null) = %STATUS_SUCCESS Then
            lGetUpTime = %TRUE            
            ' FileTimeToLocalFileTime
            FileTimeToLocalFileTime(lSystemTimeInformation.BootTime   ,lSystemTimeInformation.BootTime)
            ' FileTime to SystemTime conversion
            FileTimeToSystemTime(lSystemTimeInformation.BootTime   , lSTBootTime)
            FileTimeToSystemTime(lSystemTimeInformation.CurrentTime, lSTCurrentTime)      
            ' IPowerTime BootTime
            IPTBootTime.NewDate(lSTBootTime.wYear,lSTBootTime.wMonth , lSTBootTime.wDay)
            IPTBootTime.NewTime(lSTBootTime.wHour,lSTBootTime.wMinute, lSTBootTime.wSecond)
            pBootTime = IPTBootTime.DateString+" "+IPTBootTime.TimeStringFull)
            ' IPowerTime CurrentTime
            IPTCurrentTime.NewDate(lSTCurrentTime.wYear,lSTCurrentTime.wMonth , lSTCurrentTime.wDay)
            IPTCurrentTime.NewTime(lSTCurrentTime.wHour,lSTCurrentTime.wMinute, lSTCurrentTime.wSecond)
            pCurrentTime = IPTCurrentTime.DateString+" "+IPTCurrentTime.TimeStringFull)
            ' UpTime = timediff CurrentTime - BootTime        
            IPTCurrentTime.timediff IPTBootTime, lSign, lDays, lHours, lMinutes, lSeconds        
            pUpTime = Format$(lDays)+"d "+Format$(lHours)+"h "+Format$(lMinutes)+"m "+Format$(lSeconds)+"s"                                                                               
        End If      
        Function = lGetUpTime
    End Function
    Jean-Pierre LEROY