[RESOLVED] catching and terminating shutdown command
Hello everyone,
I have an off the shelve application that has some irritating behavior. Everytime you close it, it will log you out of Windows. I spoke to the vendor today and they told me that is the way it is supposed to work.
Anyway, I am wondering if there is a way in vb.net to catch this shutdown command and terminate it. I basically want to have an application or service running in the background looking for shutdown commands.
Thanks :thumb:
Re: catching and terminating shutdown command
I think you can try to detect the WM_QUERYENDSESSION message in your form, then cancel it...
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
Re: catching and terminating shutdown command
Hi Gigemboy,
Thanks for the code. Do I need to add a reference or anything to my project? I keep getting the error "Type Message is not defined."?
Thanks
Re: catching and terminating shutdown command
Also, running the command "shutdown -a" in the console aborts a shutdown. Not sure if that helps any but you could always have your program send that command or use it yourself if you get in a pinch.
Unsure about the posted API code though
Re: catching and terminating shutdown command
FrustratedFlash,
What version of vb are you runnig.
Re: catching and terminating shutdown command
Hi Safewithyou247 and everyone else.
I figured out my problem. I was trying to get this to work as a console application instead of a Windows application. I have it in a form now and it is working great.
Thank you all for your support.
Re: catching and terminating shutdown command
Good deal. If your thread is resolved, you can go to the top inder "thread tools" and mark the thread as "resolved" :)
Re: [RESOLVED] catching and terminating shutdown command
Does this work in VB 2005? If so, how do I call it?
Thanks
Re: [RESOLVED] catching and terminating shutdown command
Quote:
Originally Posted by GedOfEarthsea
Does this work in VB 2005? If so, how do I call it?
Thanks
What is gigemboy's code but two member variables and a method. How would you normally use two member variables and a method in a WinForms app?
Re: [RESOLVED] catching and terminating shutdown command
You would do:
WndProc()
But what do I put in the parenthesis?
Re: [RESOLVED] catching and terminating shutdown command
Quote:
Originally Posted by GedOfEarthsea
You would do:
WndProc()
But what do I put in the parenthesis?
WndProc is the method that processes messages from Windows. That's why it has an argument of type System.Windows.Forms.Message. The idea of overriding the WndProc method is to detect one or more specific messages from Windows and change the behaviour of the window by executing custom code when those messages are received. You don't call the WndProc method yourself. It gets executed every time the OS sends a message to the window.