Windows/C++ 中是否有“重启”功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1462932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a 'restart' function in Windows/C++
提问by coppro
In a windows project I am working on, I intend to have a menu selection that copletely restarts the app. Is there a Windows or C++ function that does this?
在我正在处理的 Windows 项目中,我打算有一个可以完全重新启动应用程序的菜单选择。是否有执行此操作的 Windows 或 C++ 函数?
回答by coppro
There isn't a built-in for this, but a well-designed application can simply stop everything that's going on and then loop back to the start. If you want a true 'fresh start', you will have to spawn a new process (possibly as the last thing you do before the old one shuts down.)
没有内置的功能,但是一个设计良好的应用程序可以简单地停止正在发生的一切,然后循环回到开始。如果你想要一个真正的“重新开始”,你将不得不产生一个新进程(可能是你在旧进程关闭之前做的最后一件事。)
回答by Wacek
No, you must do it yourself. For instance, you can run external process which will wait until you exit your application, and then run it again.
不,你必须自己做。例如,您可以运行外部进程,它会等到您退出应用程序,然后再次运行它。
回答by Tom Kirby-Green
Actually you might want to take a look at the Restart Manager API that came in with Windows Vista. As ever you can p-invoke this to your hearts content and theirs explicit support coming for it in Visual C++ 2010.
实际上,您可能想看看 Windows Vista 附带的重新启动管理器 API。与以往一样,您可以将其调用到您的内心深处,并在Visual C++ 2010 中提供明确的支持。
回答by Atmocreations
Already needed to do this. The easiest way without any further reading would be to write a simple .bat-file (either by hand or dynamically by your application) starting your program and then calling that bat-file from your application.
已经需要这样做了。无需任何进一步阅读的最简单方法是编写一个简单的 .bat 文件(手动或由您的应用程序动态)启动您的程序,然后从您的应用程序调用该 bat 文件。
The bat-file may even contain a line to remove itself after having started your app...
bat 文件甚至可能包含一行在启动您的应用程序后自行删除...
回答by Atmocreations
You want to call CreateProcessand then close your current instance of the application gracefully with ExitProcess(), or if you link to the C runtime, just return from main(). But first you should ask yourself why you need to recreate the process in the first place.
您想调用CreateProcess,然后使用ExitProcess()优雅地关闭应用程序的当前实例,或者如果您链接到 C 运行时,只需从 main() 返回。但首先你应该问自己为什么首先需要重新创建流程。
回答by Byron Whitlock
ExitWindowsExis what you want. You can also run the shutdown.exeutility built into windows.
ExitWindowsEx就是你想要的。您还可以运行Windows 中内置的shutdown.exe实用程序。
shutdown -t0 -r (restart the system after 0 seconds)