2

I'm planning to develop a cross-platform application that may need to do processing prior to the user's Operating System shutting down or restarting. Is there a way to temporarily suspend, or override, an OS shutdown/restart in C++?

As an aside, this feature will be entirely the user's choice via configuration and disabled by default.

I feel this might not be possible without being invasive to the Operating System, but I thought I'd ask for the community's thoughts.

If my only option is to somewhat "hack" the OS in order to achieve this feature, then I'll not pursue this idea any further. :)

Thanks in advance!

Aaron
  • 101
  • 1
  • 10
  • 1
    for windows here is a dupe: http://stackoverflow.com/questions/101647/how-to-schedule-a-task-to-run-when-shutting-down-windows – 463035818_is_not_an_ai Apr 26 '17 at 19:23
  • You could create a batch file that shuts down the computer and call it with system("mybatchfile.bat"); – Rafael Apr 26 '17 at 19:32
  • 1
    For Windows, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa376890(v=vs.85).aspx –  Apr 26 '17 at 19:33

5 Answers5

3

Is there a way to temporarily suspend, or override, an OS shutdown/restart in C++?

There is no such functionality in standard C++.

Unless there is a platform-specific API to do that, I am afraid you won't be able suspend or override an OS shutdown/restart operation in C++.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

There's nothing in C++ for this. But a typical GUI (Windows, Mac, etc.) will notify your application of a pending shutdown so that you can do final processing. That's specific to the OS, and subject to its rules.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • in mac, you can ask the os not to shutdown, it'll cancel the operation and ask the user whether to force it off or not, in linux there is no such a thing, it'll shutdown whether you like it or not (as only root usually can order shutdown) in windows, there will be a delay after which everything will be forced shut (at least in 7, never tried 8 or 10) – Qchmqs Apr 26 '17 at 19:57
0

To override a scheduled/in progress shutdown you can:

For Windows 7

system("shutdown /a");

For Linux

system("shutdown -c");
Rama
  • 3,222
  • 2
  • 11
  • 26
0

I have googled the related article that how to detect Linux shutdown/reboot But it seems hard to implement for your purpose.

What comes from my mind is; if you can add your program as a service, the linux system will closing the service in advance of its shutdown.

Good Luck

Community
  • 1
  • 1
Kwang-Chun Kang
  • 351
  • 3
  • 12
0

Thanks to all responses and ideas!

Judging by the responses received, it seems there is no "safe" and cross-platform way of achieving such functionality easily using C++.

Pete Becker mentioned there could be specific rules per Operating System. This makes sense, and these rules are also always subject to change.

Others have suggested using the system() command. However, this would potentially introduce further issues and vulnerabilities (1) (2) into the system (there may be better explanations and/or examples, so please edit this post if/when you find them).

Considering all of the above, my suggestion would be to configure the application to either have the user manually execute these processes via a GUI (leaving it entirely up to them), execute these processes on startup of the Operating System or both.

Thanks again to all who responded!

Community
  • 1
  • 1
Aaron
  • 101
  • 1
  • 10