# VBForums CodeBank > CodeBank - Visual Basic .NET >  Prevent Windows Shutdown

## gigemboy

Here is a little code snippet that prevents the windows from shutting down if your app is running, it intercepts the WM_QUERYENDSESSION message that is sent to the open windows on a system shutdown, and cancels the message.

VB Code:
'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

----------


## Phill64

you can actually CANCEL the shutdown!? no wonder windows security is full of holes :/ sounds like a nice vulnerability for viruses to me, i mean reading that a shutdown is about to happen is useful so you can make your program stop, but cancelling it.. eek..

Although i guess there is always the power button  :Smilie: 

nice post!

----------


## gigemboy

lol   :LOL:   well there was a seemingly valid reason for it, in this post: http://www.vbforums.com/showthread.php?t=378604

----------


## noahssite

I put that code on my program to test it, then when i ran my program i clicked shutdown and that code didnt cancel it.

----------


## noahssite

Oh i see i made a error when i tryied out ur code.

----------


## JuggaloBrotha

Has anyone tested this in Vista?  The last thing I read from MS is that programs like this Don't prevent Vista from shutting down.  Vista will give it an allotment of 30 seconds to finish up and then it's terminated regardless.  I'm curious if Vista actually does that or not.

----------


## Atheist

> Has anyone tested this in Vista?  The last thing I read from MS is that programs like this Don't prevent Vista from shutting down.  Vista will give it an allotment of 30 seconds to finish up and then it's terminated regardless.  I'm curious if Vista actually does that or not.


I've tried it, this is what appears:

----------


## JuggaloBrotha

It's good to know that MS has included the ability to at least notify you when a program is preventing shut down/ log off

----------


## scotsmanscott

Does anyone know how to check which window or process sent the "WM_QUERYENDSESSION" message?

I'm trying to determine which process is shutting certain PCs down.

I tried "ex.HWnd", but that returned the current window.

Thanks in advance!

----------


## noahssite

> Does anyone know how to check which window or process sent the "WM_QUERYENDSESSION" message?
> 
> I'm trying to determine which process is shutting certain PCs down.
> 
> I tried "ex.HWnd", but that returned the current window.
> 
> Thanks in advance!


You should place that question in the VB.NET forum as a new thread, this is the codebank. You will get more anwsers faster if you do a new thread in the right section.

P.S. Welcome to the forums!

----------

