0

I have an service application that spawns a child user process and monitors for its termination. I will need to handle a case where a shutdown is initiated BUT later aborted by some desktop application so I can re-spawn the child process. I am totally aware of all the system event notifications BUT they wont do the job as there is no way to detect that shutdown is aborted afterwards.

So the question is whether there is an API function that could get me a current state of OS shutdown operation.

Thanks!

NullReference
  • 862
  • 1
  • 11
  • 27
  • Shutdown abort? Whats that? – Hirasawa Yui Jun 09 '21 at 11:38
  • This api can make it happen https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-shutdownblockreasoncreate Many stock windows apps use it such as notepad, so effectively an open notepad can allow user to veto the shutdown. – NullReference Jun 09 '21 at 11:42
  • There is probably no need for you to do anything special beyond monitoring termination and unconditionally restarting the process after a reasonable time. Unless your process blocks termination itself, it will not prevent the system from shutting down; at worst it will re-launch pointlessly just before a shutdown that is not aborted. – Jeroen Mostert Jun 09 '21 at 13:02
  • Yes an timer callback is how I plan to handle it but was hoping for an shutdown state that could be used as a hint. – NullReference Jun 09 '21 at 13:18
  • As far as I know there is no such state -- you can initiate a shutdown or abort it, but not query if a shutdown is imminent, except for some functions returning an error state if the system is in the process of shutting down. – Jeroen Mostert Jun 09 '21 at 13:48
  • If you are aware of such functions I would appreciate if you shared some info on them and I will experiment. – NullReference Jun 09 '21 at 13:52
  • 1
    As long as we're talking services, [`QueryServiceStatusEx`](https://learn.microsoft.com/windows/win32/api/winsvc/nf-winsvc-queryservicestatusex) is one such function -- at least it's documented to return `ERROR_SHUTDOWN_IN_PROGRESS` if a shutdown is happening, though I have no idea as to the timing. In C# this would be exposed through `ServiceController.Status`, though it's notable that this property is not documented as producing an exception during a shutdown, and it calls `QueryServiceStatus`, which is not documented as returning an error on shutdown either. – Jeroen Mostert Jun 09 '21 at 14:03

1 Answers1

1

How about System Power States and this link ? And according to How to detect Windows shutdown or logoff thread, maybe you can use SM_SHUTTINGDOWN to detect shutdown abort.

YangXiaoPo-MSFT
  • 1,589
  • 1
  • 4
  • 22