1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to Abort a System SHutdown (system sleep)

Discussion in 'C#' started by evarmint, Jun 16, 2007.

  1. evarmint

    evarmint New Member

    Joined:
    Jun 16, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I saw on another thread a way to initiate a shutdown, but I would like to do something a little trickier. How can I detect a shutdown is happening and abort it? Basically, I am trying to disable the sleep key on the keyboard. I've tried overridding the system key handler, but the sleep key still makes it through somehow.
     
  2. LynxSI

    LynxSI New Member

    Joined:
    May 23, 2010
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Vancouver, BC, Canada
    Home Page:
    System level keys are tricky. And to make it trickier the situation you are asking about is handled differently in Windows XP, Vista and 7.

    The first thing to try is just to catch your Form_Closing event. Check the event parameter for the CloseReason and set e.Cancel to true when you don't want to shutdown. That will prevent most non-critical shutdowns.

    If you want to get really serious, you need to do some "ugly" pinvoke'ing the windows API for Windows hooks. Do some research about "SetWindowsHookEx" with C#. This will allow you to intercept key stroke at a lower level than your project's internal message loop. This one will also work on newer operating systems like Windows Vista and 7.

    However, there is one key combination that cannot be overridden in the modern operating systems which is Ctrl+Alt+Delete. This is reserved for operating system security and cannot be intercepted or prevented except via Windows Security Policies.

    Best Regards,
    - Ben
     

Share This Page