is there a way to tell is windows is shutting down or rebooting?

Hi Guys,
Is there a way to tell if windows is currently shutting down or rebooting?

I have a program which re-runs itself when it shuts down for any reason which is what I want except for when windows is shutting down or rebooting.

hopefully in XOJO/ RealStudio/ Realbasic or in CMD


Back in my VB6 days, 19 years ago, there was a API declare to do this.
In VB.NET this class will do the reboot. Should not be difficult to port to Xojo.

[code]Public Class DeviceReboot
<DllImport(“coredll”)> _
Private Shared Function SetSystemPowerState( _
ByVal psState As String, _
ByVal StateFlags As Integer, _
ByVal Options As Integer) As Integer
End Function

Const POWER_STATE_ON As Integer = &H10000
Const POWER_STATE_OFF As Integer = &H20000
Const POWER_STATE_SUSPEND As Integer = &H200000
Const POWER_STATE_RESET As Integer = &H800000

Public Sub New()

End Sub

Public Function Reboot()
        SetSystemPowerState(Nothing, POWER_STATE_RESET, 0)
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try
End Function

End Class[/code]

Sounds like your app should be a Windows Service. That’ll do the restarts for you and shut it down when the machine restarts.

Your app will receive a WM_QUERYENDSESSION message when your app is being closed due to a log out/shutdown. Then a WM_ENDSESSION will be raised.

Here’s a simple example that detects that:

Make sure you read and all associated links in the lParam entry as you can get into problems :slight_smile:

I’ll not get into whether its right or wrong to do it this way as there are edge cases to justify just about any application requirements :slight_smile:

Skip my post. I was just reading too quickly. My implementation was for the application starting a reboot of Windows.

hi Guys,
thanks for all the great tips and the excellent sample

It was all of these which tweeked my memory to something I used in the past

#If TargetWin32=true Then Declare Function GetSystemMetrics Lib "User32" (Index As Integer) As Integer Const SM_SHUTTINGDOWN = &h2000 If GetSystemMetrics(SM_SHUTTINGDOWN) <> 0 Then //Any non-zero value is "True" 'the system is shutting down else ' the system is NOT shutting down end if #endif

I know I’m not smart enough to have discovered it myself so I cant take any of the credit
It may have even come from Aarons WFS

thanks again

A session shutdown isn’t quite the same as a machine shutdown but if it works for your use case then its all good :slight_smile: