Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept Windows Vista shutdown event in C#

I want to be able to intercept the shutdown event in C# for Windows Vista. Due to the advanced security features with Vista, any applications that are running after the shutdown command is called are halted and displayed in a list, prompting the user to do something with them.

Does anybody know how to overcome this and what events I need to be using in Vista.

Thanks.

like image 404
HAdes Avatar asked Sep 29 '08 13:09

HAdes


2 Answers

You can use WPF's application object and subscribe to its SessionEnding event. You can then look at the SessionEndingCancelEventArgs.ReasonSessionEnding enumeration to determine exactly why the session is ending (LogOff or Shutdown).

like image 126
Ken Wootton Avatar answered Oct 26 '22 14:10

Ken Wootton


What you may want to look at is here - Application Shutdown Changes in Windows Vista. Basically, for what you want, it all revolves around WM_QUERYENDSESSION.

Note that this is exposed in the .net framework - instead you will need to use native functions (p/invoke) and hook the wndproc in your code to respond to the windows message.

For an example (showing a reason to not shutdown), you can see Windows Vista - ShutdownBlockReasonCreate in C#.

like image 44
Philip Rieck Avatar answered Oct 26 '22 13:10

Philip Rieck