简体   繁体   English

如何将以ShellExecuteEx作为子进程运行的进程在父退出后关闭?

[英]How to run a process with ShellExecuteEx as as a child that closes after parent exit?

I need to spawn a process which need to invoke UAC. 我需要产生一个需要调用UAC的进程。 I have read this post: How can I run a child process that requires elevation and wait? 我已经阅读了这篇文章: 如何运行需要提升并等待的子进程? .

Unfortunatelly I can't run a process as a real child. 不幸的是,我不能像一个真正的孩子那样运行流程。 The new process run as a separate and continue to run even after spawner close. 新进程将作为单独的进程运行,即使在生成器关闭后仍继续运行。 But I need it to close automatically when parent exits (normal exit or by crash or any other reason). 但是我需要它在父母退出时自动关闭(正常退出或由于崩溃或任何其他原因)。

One of the solution that were provided is to use jobs for that. 提供的解决方案之一就是为此使用作业。 Unfortunately I can't assign a process created with ShellExecuteEx to a job. 不幸的是,我无法将用ShellExecuteEx创建的进程分配给作业。 It returns me an error ERROR_ACCESS_DENIED . 它返回错误ERROR_ACCESS_DENIED To handle this I have to pass CREATE_BREAKAWAY_FROM_JOB to CreateProcess . 为了解决这个问题,我必须将CREATE_BREAKAWAY_FROM_JOB传递给CreateProcess And this is the closed circle. 这是一个封闭的圈子。 I have to use ShellExecuteEx but not CreateProcess . 我必须使用ShellExecuteEx而不是CreateProcess

You have the use the shell verb runas to force elevation. 您可以使用外壳动词runas强制提升。 Which rules out CreateProcess . 哪个排除CreateProcess So you can do the following: 因此,您可以执行以下操作:

  1. Use ShellExecuteEx to create the child process. 使用ShellExecuteEx创建子进程。
  2. Arrange for ShellExecuteEx to return a process handle for the child. 安排ShellExecuteEx返回该子项的进程句柄。
  3. When the parent terminates, terminate the child. 父母终止时,终止孩子。

If you want to use jobs to tie the processes lives together then you need to include the requiresAdministrator manifest option of the child. 如果要使用作业将流程的生命联系在一起,则需要包括子项的requiresAdministrator清单选项。 That way you can use CreateProcess and specify the process creation flags as you desire. 这样,您可以使用CreateProcess并根据需要指定进程创建标志。

Now, you may wish not to have the child process manifested that way. 现在,您可能不希望子进程以这种方式表现出来。 If so then you can still get the job done with a call to CreateProcess . 如果是这样,那么您仍然可以通过调用CreateProcess来完成工作。 You need three processes: 您需要三个过程:

  • Parent process, runs as standard user. 父进程,以标准用户身份运行。
  • Launcher process, manifested to be elevated. 启动器进程,表现为被提升。 Created by parent, with appropriate job flags, and passed arguments that specify how to launch child. 由父级创建,带有适当的作业标志,并传递了指定如何启动子级的参数。 Which it does with CreateProcess and placed in same job. 它与CreateProcess并放置在同一工作中。
  • Child process. 子进程。 Not manifested to be elevated. 没有表现为升高。 But because it is launched by the elevated launcher it too is elevated. 但是因为它是由高架发射器发射的,所以它也被升高了。

The point here is that there are two ways to force elevation. 这里的要点是有两种方法可以强制抬高。 Statically at compile time with a manifest. 带有清单的静态编译时。 That allows you to use CreateProcess . 这样就可以使用CreateProcess Or dynamically with the runas shell verb. 或与runas shell动词一起动态使用。 That forces ShellExecuteEx . 这迫使ShellExecuteEx You state that you are pushed to CreateProcess , and so that means the use of a manifest to force elevation. 您声明已被推送到CreateProcess ,因此这意味着使用清单来强制提升。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM