等待ShellExecuteEx(设置Windows进程的访问权限)
我在 C++ 程序中使用 ShellExecuteEx 函数来启动 Uninstall.lnk 文件。 在我的程序中,我想等待卸载程序完成。 我的第一次尝试是在 SHELLEXECUTEINFO
结构中设置 SEE_MASK_NOCLOSEPROCESS
标志,然后在可用的 hProcess
句柄上调用 WaitForSingleObject
在传递给 ShellExecuteEx
的 SHELLEXECUTEINFO
结构中,但这似乎返回得太早了。
我目前的怀疑是,这是因为 ShellExecuteEx 启动的进程(它是否启动新的 shell?)创建了新的子进程,但不等待它们。 所以我试图创建一个“等待我的子进程及其启动的所有子进程”功能。 为此,我尝试使用作业对象。
我使用 CreateJobObject
创建了一个作业对象,将 ShellExecuteEx
返回的进程句柄分配给该作业,然后尝试等待该作业对象。 不幸的是,将进程分配给作业失败,我认为这是由于访问权限不足造成的。
有谁知道如何在进程句柄上设置 PROCESS_SET_QUOTA 和 PROCESS_TERMINATE 访问权限(根据 MSDN,这是 AssignProcessToJobObject
成功所必需的),或者等待 ShellExecuteEx 启动的进程的另一种方法结束?
更新:我应该指出,我还启动了其他应用程序,而不仅仅是Uninstall.lnk
。 其中之一是例如 ClickOnce 应用程序,这实际上是一个简单的 XML 文件,文件扩展名为 .application
。
I'm using the ShellExecuteEx
function in a C++ program to launch an Uninstall.lnk file. In my program, I'd like to wait for the uninstaller to finish. My first attempt was to set the SEE_MASK_NOCLOSEPROCESS
flag in the SHELLEXECUTEINFO
structure and then call WaitForSingleObject
on the hProcess
handle available in the SHELLEXECUTEINFO
structure passed to ShellExecuteEx
, but that still seemed to return way too early.
My current suspicion is that this is because the process launched by ShellExecuteEx (does it launch a new shell?) creates new child processes, but doesn't wait for them. So I'm trying to create a "wait for my child process and all the children it launches" function. To do so, I'm trying to use job objects.
I created a job object using CreateJobObject
, assigned the process handle returned by ShellExecuteEx
to the job and then attempted to wait for the job object. Unfortunately assigning the process to the job failed, and I think this is due to insufficient access rights.
Does anybody know how to set the PROCESS_SET_QUOTA and PROCESS_TERMINATE access rights (which are required for AssignProcessToJobObject
to succeed, according to the MSDN) on a process handle, or another way to wait for the process launched by ShellExecuteEx to finish?
UPDATE: I should point out that I'm also launching other applications, not just Uninstall.lnk
. One of them is e.g. a ClickOnce application, which is effectively a simple XML file with the file extension .application
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Vista 使用作业对象来启动链接。 因此,您尝试分配给另一个作业对象的进程可能已被分配。
请参阅:此问题
Vista uses job objects for launching links. Therefor the process you try to assign to another job object might already be assigned.
See: this question
为什么不执行目标文件而不是打开
Uninstall.lnk
? 您可以使用IShellLink
来获取快捷方式目标。 然后,您将能够使用SEE_MASK_NOCLOSEPROCESS
标志通过 ShellExecuteEx 执行目标文件。Why not to execute target file instead of openning
Uninstall.lnk
? You could useIShellLink
to get shortcut target. Then you'll be able to execute target file via ShellExecuteEx usingSEE_MASK_NOCLOSEPROCESS
flag.