Results 1 to 11 of 11

Thread: [RESOLVED] catching and terminating shutdown command

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    51

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. 'constants needed, form level
    2.     Private Const WM_QUERYENDSESSION As System.Int32 = &H11
    3.     Private Const WM_CANCELMODE As System.Int32 = &H1F
    4.  
    5.     'the sub to intercept the windows messages
    6.     Protected Overrides Sub WndProc(ByRef ex As Message)
    7.         If ex.Msg = WM_QUERYENDSESSION Then
    8.             'cancel the message
    9.             Dim MyMsg As New Message
    10.             MyMsg.Msg = WM_CANCELMODE
    11.             MyBase.WndProc(MyMsg)
    12.         Else
    13.             'send the message as normal
    14.             MyBase.WndProc(ex)
    15.         End If
    16.     End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    51

    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

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  5. #5
    Lively Member safewithyou247's Avatar
    Join Date
    Dec 2005
    Posts
    64

    Re: catching and terminating shutdown command

    FrustratedFlash,

    What version of vb are you runnig.
    "A candle loses nothing by lighting another candle."

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2004
    Posts
    51

    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.

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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"

  8. #8
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [RESOLVED] catching and terminating shutdown command

    Does this work in VB 2005? If so, how do I call it?

    Thanks

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Addicted Member GedOfEarthsea's Avatar
    Join Date
    May 2006
    Location
    USA
    Posts
    145

    Re: [RESOLVED] catching and terminating shutdown command

    You would do:

    WndProc()

    But what do I put in the parenthesis?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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