如何重新启动在Windows中编写的控制台C应用程序?
我正在使用 MSVS 2008。我正在用 c 编写一个应用程序,并且想知道在 Windows 中重新启动应用程序的最佳方法是什么。
我环顾四周,有人正在做类似的事情,但我不确定这是否是最好的方法,或者是否创建了一个全新的流程。
if(command == restart)
{
printf("program exiting!\n");
Sleep(2000);
system("cls");
WinExec("my_app.exe", SW_SHOW);
exit(0);
}
谢谢
I am using MSVS 2008. I am writing an application in c, and would like to know what's the best way to restart an application in windows.
I looked around and someone was doing something like this, but I am not sure if that is the best way or if that even creates a whole new process.
if(command == restart)
{
printf("program exiting!\n");
Sleep(2000);
system("cls");
WinExec("my_app.exe", SW_SHOW);
exit(0);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您必须有一个额外的过程。
您将在您的应用程序中启动该流程。它将等待您的主应用程序退出,然后等待它应该发生的任何事情(更新,...),然后重新启动它杀死自己。
从内部启动应用程序是不行的,因为您将无法更新任何内容。
You'll have to have one process extra for this.
From your application, you'll launch that process. It will wait for your main application to exit, then wait for whatever that it should happen (update, ...) and then restart it killing itself.
Starting app from within itself won't be ok since you won't be able to update anything.
简短的回答:对我来说看起来不错,但是如果您绑定到套接字,那么您的两个程序在父 exit() 之前发生冲突的可能性很小。
恐怕要求“最好”的方式会产生“这取决于上下文”的答复之一。
首先,根据 MSDN 文档 WinExec,“提供此函数只是为了与 16 位 Windows 兼容。应用程序应使用 CreateProcess 函数”。这意味着它是 'exec' 的 dos 时代函数包装器 -像函数一样。当然,CreateProcess 是一种只有 MS 才会创建的怪物,但如果这个应用程序非常重要,那么可能应该采纳建议。
有趣的是,MS 在 CreatProcess 的文档中提到“关闭进程的首选方法是使用 ExitProcess 函数”。
所以你可以看到,就像很多问题一样,有很多解决方案。可能会完善这里的回复的问题是:
平台独立性?
程序应该采取什么?
等等,
希望对您有所帮助!
Short answer: Looks ok to me, but if you're binding to sockets, there's a really small chance your two programs might collide before the parent exit()s.
I'm afraid asking for "the best" way is going to produce one of those "it's context-dependent" replies.
To start, according to the MSDN docs on WinExec, "This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function". This implies it's a dos-era function wrapper for an 'exec'-like function. Of course CreateProcess is some kind of monster only MS would create, but if this application is going to be at all important the advice should probably be taken.
Interestingly, MS mentions in the docs for CreatProcess that "the preferred way to shut down a process is by using the ExitProcess function."
So you can see, like with so many problems, there are many solutions. Questions to answer that might hone the replies here would be:
platform-independence?
program is supposed to take?
etc.
I hope that helps you out!
这是一些关于如何启动计算器的愚蠢示例。
STARTUPINFO 启动信息 = { 0 };
PROCESS_INFORMATION procInfo = { 0 };
startUpInfo.cb = sizeof( startUpInfo );
而( 1 )
{
CreateProcess( L"C:\windows\System32\calc.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &startUpInfo, &procInfo );
WaitForSingleObject( procInfo.hProcess, INFINITE );
如
您所见,这将启动一个新进程“calc.exe”。它将等待它终止,然后重新开始。请记住,我没有关闭这里的任何句柄!!!
Here is some dumb example on how to start the calculator.
STARTUPINFO startUpInfo = { 0 };
PROCESS_INFORMATION procInfo = { 0 };
startUpInfo.cb = sizeof( startUpInfo );
while( 1 )
{
CreateProcess( L"C:\windows\System32\calc.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &startUpInfo, &procInfo );
WaitForSingleObject( procInfo.hProcess, INFINITE );
}
As you can see, this will start a new process "calc.exe". It will wait for it to terminate, and start it all over again. Keep on you mind that I didn't close any handles here!!!