Question Shutdown Intercept Service

conan1989

Member
Joined
Jul 1, 2008
Messages
14
Programming Experience
1-3
Hi Guys,

OK, so i need something that can listen for shutdown requests and intercept and stop them. then run some code (prompts the user) that does some magic and then closes it's self and issues a new shutdown command.

This will aparently do it, but i have no idea how to use it.
Source: Prevent Windows Shutdown - VBForums


VB.NET:
      'constants needed, form level

          Private Const WM_QUERYENDSESSION As System.Int32 = &H11

          Private Const WM_CANCELMODE As System.Int32 = &H1F



          'the sub to intercept the windows messages

          Protected Overrides Sub WndProc(ByRef ex As Message)

              If ex.Msg = WM_QUERYENDSESSION Then

                  'cancel the message

                  Dim MyMsg As New Message

                  MyMsg.Msg = WM_CANCELMODE

                  MyBase.WndProc(MyMsg)

              Else

                  'send the message as normal

                  MyBase.WndProc(ex)

              End If

          End Sub
 
WindowsShutDown is also a possible CloseReason of the cancellable FormClosing event of Forms. Then there is the SessionEndReasons.SystemShutdown Reason of SystemEvents.SessionEnding event, also cancellable. Both these options is managed implementations of the WM_QUERYENDSESSION windows message. Getting windows messages in a windows service is only possible if it is configured to interact with desktop. This is normally not recommended, instead you can use a UI application that act as middleman between service and desktop. Services also has it's own shutdown notification system, set CanShutdown to enable and override OnShutdown, this is not cancellable and only intended to buy some extra time for cleanup.

This article has much info about how the shutdown system works: Application Shutdown Changes in Windows Vista (Windows)
 
Back
Top