views:

1411

answers:

3

I have an issue with my application when windows shuts down - my app isn't exiting nicely, resulting in the End Task window being displayed. How can I use the debugger to see what's going on? e.g. is there a way to send the windows shutdown message(s) to my application so it thinks windows is shutting down, so i can see exactly how it behaves?

+6  A: 

I believe when Windows is shutting down it sends a "WM_QueryEndSession" to all applications. To simulate a Windows shutdown you could create a little application that just does a PostMessage with this message to your application and see what happens. Windows may send more messages than that to actually close your application (like WM_CLOSE), but whenever your application receives the "WM_QueryEndSession" message it means your application is about to have the rug pulled out from under it.

Jon Tackabury
Also, WM_QueryEndSession is followed (given the all-OK by the apps that receive it) by WM_EndSession.
JMD
Excellent - thanks for the clarification JMD. I figured there had to be an additional message, but I wasn't sure what it was.
Jon Tackabury
Any useful utils that I can use to send such messages instead of crafting one myself?
Rory
I don't know of any applications, but you could do this:[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);Then just call PostMessage(handle, WM_whatever, IntPtr.Zero, IntPtr.Zero);
Jon Tackabury
Sorry, the code doesn't format very well in the comments.
Jon Tackabury
Oh, and to get your window handle from outside your application you can use Spy++ that comes with Visual Studio, or the excellent "Winspector Spy" which is freely available and easier to use.
Jon Tackabury
+1  A: 

You could use the SystemEvents.SessionEnding event, which is fired when a user logs off or shuts down. Be careful when using it though, some resources are not guaranteed to be available. For example, my application needed to hit a server when it was shutting down to clock a user out (a timeclock application), but the network card is sometimes already disabled when this event occurs. Since you're just doing cleanup, this should work fine.

Bit Destroyer
+3  A: 

There is a tool named Restart Manager (rmtool.exe) in the Microsoft's Logo Testing Tools for Windows, which can be used to send shutdown and restart messages to a process. Logo testing tools can be downloaded here:

http://download.microsoft.com/download/d/2/5/d2522ce4-a441-459d-8302-be8f3321823c/LogoToolsv1.0.msi

Then you can simulate shutdown for your process:

rmtool.exe -p [PID] -S

where [PID] is the process ID. According to the Vista Logo Certification Test Cases document,

Restart Manager shutdown messages are:

a. WM_QUERYENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): GUI applications must respond (TRUE) immediately to prepare for a restart.

b. WM_ENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): The application must shutdown within 5 seconds (20 seconds for services).

c. CTRL_SHUTDOWN_EVENT: Console applications must shutdown immediately.

dkl
awesome! (I haven't tried it yet - if anyone uses it and can provide feedback that'd be great)
Rory