How to create a process that is not a child of it's creating process?

微笑、不失礼 提交于 2019-11-30 18:11:27

问题


I have two processes, A and B. At some point A creates B. After B is created, if A's process tree is killed, I want B to still be around.

I am using CreateProcess() to create B, and I can't seem to find any way to make it create the process without it being a child. Same thing with ShellExecuteEx(), but I am probably missing some flag.

Does anyone know what I could use to do this?

EDIT: I forgot to mention that both processes need a HANDLE or process ID to the other


回答1:


You can try that process A create process C, which create process B and then process C will be immediatly ended (terminated). In a process B there are exist only information about the direct parent process (process Id of C which is not more running) and not about the process A. So "if A's process tree is killed" the process B will probably stay running.

For example you start Process Explorer (see http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) then start Total Commander. From the Total Commander you start cmd.exe. From cmd.exe you start notepad.exe. Then type "exit" in the cmd.exe. After terminating of cmd.exe you can see that notepad.exe will no more displayed under Total Commander (totalcmd.exe). After you choose in Process Explorer "Kill Process Tree" for the Total Commander (totalcmd.exe) you can see that notepad.exe stay running.




回答2:


You can set the paramaeter dwcreationflags as DETACHED_PROCESS in the createprocess API.



来源:https://stackoverflow.com/questions/3136154/how-to-create-a-process-that-is-not-a-child-of-its-creating-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!