如何在Windows中创建进程并终止父进程 [英] How to create a process and kill the parent in windows

查看:180
本文介绍了如何在Windows中创建进程并终止父进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个mfc - 应用程序:foo.exe。



现在我希望这个foo.exe创建一个自己的进程并自行杀死。



(我只想创建一个自己的新进程并杀死旧进程。)



我试过这样:



CreateProcess(foo.exe的路径,NULL,NULL ,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,foo.exe目录,&启动,&info);



PostQuitMessage(0);





但是:这不起作用,它出现了一个消息框....如果我点击确定那里,它的工作原理。 (在德语中,messagebox的文本是server ausgelastet,这意味着服务器正忙。(这是一条消息)



有没有人有解决方案这个问题?



很多!

Hi all

I have an mfc - application: foo.exe.

Now i want this foo.exe to create a process of itself and kill itself.

(I simply want to create a new process of itself and kill the old one.)

I tried this like that:

CreateProcess("path of foo.exe", NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, "directory of the foo.exe", &startup, &info);

PostQuitMessage(0);


But: This does not work, it comes up a message box.... if i click ok there, it works. (in german, the text of the messagebox is "server ausgelastet" which means that the server is busy. (this is an ole message)

Does anyone has a solution to this problem?

Thx alot!

推荐答案

问题是: PostQuiteMessage 在所有情况下都不起作用。它发布 WM_QUITE ,因此您应该假设所有条件都以最终导致应用程序终止的方式运行:消息循环仍在执行,消息正在处理中,依此类推。请参阅: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644945%28v=vs.85%29.aspx [ ^ ] 。



情况可能不是这样。因为你没有提供任何通知关于你的过程状态,我不知道它是什么。它可能是任何东西,真的。



终止过程的困难方法(是的,你可以终止当前的过程)是:

https://msdn.microsoft.com/en -us / library / windows / desktop / ms683179%28v = vs.85%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686714%28v = vs.85%29.aspx [ ^ ]。



最好不要一直这样做。



很多人会说以这种方式杀死当前进程的想法是滥用的。我同意这个意见。我只想补充一下终止当前进程的整个想法(甚至很好)并启动同一应用程序的另一个实例本身就是技术滥用的一个非常强烈的迹象。如果您需要更多帮助,您需要解释您正在做的事情的动机以及您的最终目标。有可能,你会被建议更合理的选择。



-SA
The trouble is: PostQuiteMessage does not work in all situations. It posts WM_QUITE, so you should assume that all conditions work the way that it would lead to application termination eventually: the message loop is being still executed, messages are being processed, and so on. Please see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644945%28v=vs.85%29.aspx[^].

This is probably not the case. As you did not provide any information on the state of your process, I have no idea what is it. It could be anything, really.

The hard way of terminating the process (yes, you can terminate your current process) is this:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683179%28v=vs.85%29.aspx[^],
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686714%28v=vs.85%29.aspx[^].

Better avoid doing it by all means.

Many would say that the idea to kill the current process in this way is abusive. And I will agree with this opinion. I will only add that the whole idea to terminate the current process (even nicely) and start another instance of the same application is itself a pretty strong indication of technology abuse. If you need more help, you will need to explain some motivation of what you are doing, and your ultimate goals. Chances are, you will be suggested more reasonable alternative.

—SA


这篇关于如何在Windows中创建进程并终止父进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆