Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Windows C++ or C# can you ask the OS if it is currently shutting down/restarting/logging off

I have an issue that occurs during shutdown. I have piece of code written in C++ that is wrapped in a .dll and injected into other applications. It does a bunch of stuff including starting another application (sever) that is written in C#.

However, after a shutdown is initiated, the c++ code starts the C# application, because its been killed by the shutdown, which promptly crashes because the system is about to shutdown.

Then an error box halts the shutdown until the user deals with it, which is annoying. Is there a way to ask the OS, "Hey, are you shutting down/rebooting/logging out?" so I do not start an application while that's happening? I suppose a solution on either end would be fine.

== EDIT ==

For a little more clarification. The code that is running as an injected .dll, can't assume that the application its injected into has any kind of "window". Also, I would like something that I could query, not an event I have to listen to because I may miss that event due to timing. I'm wondering if there is any sort of function that would effectively give me something like this

bool IsSystemShutting();

And in this respect at C# option would work for me as well as a C++ option. But in the .NET framework SystemEvents.SessionEnding and SystemEvents.SessionEnded are both events versus properties. Its very often the case that I'd be signing up for this event AFTER the event has occurred, so it does me no good right.

like image 453
Ultratrunks Avatar asked Feb 05 '13 19:02

Ultratrunks


1 Answers

The WM_ENDSESSION and WM_QUERYENDSESSION messages are meant for this scenario. Windows will send these window messages to a window when a shutdown, restart, or logoff process is initiated.

like image 126
syazdani Avatar answered Oct 04 '22 01:10

syazdani