Results 1 to 22 of 22

Thread: [RESOLVED] Detecting Sleep / Standby mode

Threaded View

  1. #9
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,672

    Re: Detecting Sleep / Standby mode

    Listen for WM_POWERBROADCAST messages, or for Win8+ PowerRegisterSuspendResumeNotification.

    Or if you want to know but weren't running at the time to log it,

    Code:
    Public Enum POWER_INFORMATION_LEVEL
        SystemPowerPolicyAc
        SystemPowerPolicyDc
        VerifySystemPolicyAc
        VerifySystemPolicyDc
        SystemPowerCapabilities
        SystemBatteryState
        SystemPowerStateHandler
        ProcessorStateHandler
        SystemPowerPolicyCurrent
        AdministratorPowerPolicy
        SystemReserveHiberFile
        ProcessorInformation
        SystemPowerInformation
        ProcessorStateHandler2
        LastWakeTime                                   ' Compare with KeQueryInterruptTime()
        LastSleepTime                                  ' Compare with KeQueryInterruptTime()
        SystemExecutionState
        SystemPowerStateNotifyHandler
        ProcessorPowerPolicyAc
        ProcessorPowerPolicyDc
        VerifyProcessorPowerPolicyAc
        VerifyProcessorPowerPolicyDc
        ProcessorPowerPolicyCurrent
        SystemPowerStateLogging
        SystemPowerLoggingEntry
        SetPowerSettingValue
        NotifyUserPowerSetting
        PowerInformationLevelUnused0
        SystemMonitorHiberBootPowerOff
        SystemVideoState
        TraceApplicationPowerMessage
        TraceApplicationPowerMessageEnd
        ProcessorPerfStates
        ProcessorIdleStates
        ProcessorCap
        SystemWakeSource
        SystemHiberFileInformation
        TraceServicePowerMessage
        ProcessorLoad
        PowerShutdownNotification
        MonitorCapabilities
        SessionPowerInit
        SessionDisplayState
        PowerRequestCreate
        PowerRequestAction
        GetPowerRequestList
        ProcessorInformationEx
        NotifyUserModeLegacyPowerEvent
        GroupPark
        ProcessorIdleDomains
        WakeTimerList
        SystemHiberFileSize
        ProcessorIdleStatesHv
        ProcessorPerfStatesHv
        ProcessorPerfCapHv
        ProcessorSetIdle
        LogicalProcessorIdling
        UserPresence
        PowerSettingNotificationName
        GetPowerSettingValue
        IdleResiliency
        SessionRITState
        SessionConnectNotification
        SessionPowerCleanup
        SessionLockState
        SystemHiberbootState
        PlatformInformation
        PdcInvocation
        MonitorInvocation
        FirmwareTableInformationRegistered
        SetShutdownSelectedTime
        SuspendResumeInvocation                        ' Deprecated
        PlmPowerRequestCreate
        ScreenOff
        CsDeviceNotification
        PlatformRole
        LastResumePerformance
        DisplayBurst
        ExitLatencySamplingPercentage
        RegisterSpmPowerSettings
        PlatformIdleStates
        ProcessorIdleVeto                              ' Deprecated.
        PlatformIdleVeto                               ' Deprecated.
        SystemBatteryStatePrecise
        ThermalEvent
        PowerRequestActionInternal
        BatteryDeviceState
        PowerInformationInternal
        ThermalStandby
        SystemHiberFileType
        PhysicalPowerButtonPress
        QueryPotentialDripsConstraint
        EnergyTrackerCreate
        EnergyTrackerQuery
        UpdateBlackBoxRecorder
        SessionAllowExternalDmaDevices
        SendSuspendResumeNotification
        BlackBoxRecorderDirectAccessBuffer
        PowerInformationLevelMaximum
    End Enum
    Public Declare Function NtPowerInformation Lib "ntdll" (ByVal InformationLevel As POWER_INFORMATION_LEVEL, InputBuffer As Any, ByVal InputBufferLength As Long, OutputBuffer As Any, ByVal OutputBufferLength As Long) As Long
    
            Sub LastSleepWake()
                Dim t1 As LongLong, t2 As LongLong
                Dim status As Long
                status = NtPowerInformation(LastSleepTime, ByVal 0, 0, t1, LenB(t1))
                Debug.Print "System last slept " & CStr(t1 / 10000000^) & " seconds ago"
                status = NtPowerInformation(LastWakeTime, ByVal 0, 0, t2, LenB(t2))
                Debug.Print "System last woke " & CStr(t2 / 10000000^) & " seconds ago"
                Debug.Print "System last slept for " & CStr((t2 - t1) / 10000000^) & " seconds"
            End Sub
    'Or without LongLong
            Sub LastSleepWake()
                Dim t1 As Currency, t2 As Currency
                Dim status As Long
                status = NtPowerInformation(LastSleepTime, ByVal 0, 0, t1, LenB(t1))
                Debug.Print "System last slept " & CStr((t1 / 10000000@) * 10000) & " seconds ago"
                status = NtPowerInformation(LastWakeTime, ByVal 0, 0, t2, LenB(t2))
                Debug.Print "System last woke " & CStr((t2 / 10000000@) * 10000) & " seconds ago"
                Debug.Print "System last slept for " & CStr(((t2 - t1) / 10000000^) * 10000) & " seconds"
            End Sub
    Last edited by fafalone; Mar 7th, 2024 at 03:10 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width