如何运行需要提升和等待的子进程? [英] How can I run a child process that requires elevation and wait?

查看:134
本文介绍了如何运行需要提升和等待的子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Win 7 / UAC让我疯狂。

Win 7/UAC is driving me crazy.

在我的C ++应用程序中,我需要运行一个可执行文件,这个东西关闭,等待它完成,然后继续。

From within my C++ application, I need to run an executable that requires elevation on Windows 7. I want to fire this thing off and wait for it to finish before proceeding. What's the easiest way to do this?

我通常通过 CreateProcess()来做这种事情,但它

I normally do this kind of thing via CreateProcess(), but it fails for executables that require elevation.

我尝试使用 cmd.exe / c ... 运行<$

我正在读取 ShellExecute ()将允许提升,但是当使用 ShellExecute()时,似乎不容易等待exe完成。
system()这样简单的工作吗?

I am reading that ShellExecute() will allow elevation, but it doesn't appear to be easy to wait for the exe to finish when using ShellExecute(). Will something simple like system() work?

任何其他想法都非常感谢! p>

Any other ideas are greatly appreciated!

推荐答案

使用 ShellExecuteEx ,而不是 ShellExecute 。此函数将为创建的过程提供一个句柄,您可以使用该句柄调用 WaitForSingleObject ,直到该进程终止。最后,只需致电 CloseHandle

Use ShellExecuteEx, rather than ShellExecute. This function will provide a handle for the created process, which you can use to call WaitForSingleObject on that handle to block until that process terminates. Finally, just call CloseHandle on the process handle to close it.

示例代码(为了清晰和简洁,省略了大部分错误检查):

Sample code (most of the error checking is omitted for clarity and brevity):

SHELLEXECUTEINFO shExInfo = {0};
shExInfo.cbSize = sizeof(shExInfo);
shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExInfo.hwnd = 0;
shExInfo.lpVerb = _T("runas");                // Operation to perform
shExInfo.lpFile = _T("C:\\MyApp.exe");       // Application to start    
shExInfo.lpParameters = "";                  // Additional parameters
shExInfo.lpDirectory = 0;
shExInfo.nShow = SW_SHOW;
shExInfo.hInstApp = 0;  

if (ShellExecuteEx(&shExInfo))
{
    WaitForSingleObject(shExInfo.hProcess, INFINITE);
    CloseHandle(shExInfo.hProcess);
}

指定 lpVerb 是什么导致UAC提升即将启动的应用程序。这相当于将应用程序清单中的权限级别设置为requireAdministrator。它需要管理员和有限用户的UAC提升。

Specifying the "runas" verb for the lpVerb is what causes UAC to elevate the application that's about to be launched. This is the equivalent of setting the permissions level in the application's manifest to "requireAdministrator". It will require UAC elevation for both an administrator and a limited user.

但值得注意的是,除非绝对必要,您应该更喜欢添加清单的标准方式到要启动的应用程序,指定其所需的执行级别。如果你走这条路线,你将简单地通过开放作为 lpVerb 。样本清单如下所示:

But it's worth noting that unless absolutely necessary, you should prefer the "standard" way of adding a manifest to the application you want to launch that specifies its required execution level. If you go this route, you will simply pass "open" as the lpVerb. A sample manifest is shown below:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
        <dependency>
                <dependentAssembly>
                        <assemblyIdentity
                                type="win32"
                                name="Microsoft.Windows.Common-Controls"
                                version="6.0.0.0"
                                processorArchitecture="X86"
                                publicKeyToken="6595b64144ccf1df"
                                language="*"
                        />
                </dependentAssembly>
        </dependency>
        <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
                <security>
                        <requestedPrivileges>
                                <requestedExecutionLevel 
                                       level="requireAdministrator" 
                                       uiAccess="false"/>
                        </requestedPrivileges>
                </security>
        </trustInfo>
</assembly>

最后,确保应用程序中的任何元素触发执行需要UAC升级的进程相应标记。这是你的工作,在用户界面中建模; Windows不会为您处理它。这是通过在入口点上显示盾牌图标来实现的;例如:

Finally, make sure that whatever element in your application triggers execution of the process requiring UAC elevation is marked accordingly. It's your job to model this in the user interface; Windows doesn't handle it for you. This is done by displaying the shield icon on the entry point; for example:

                                               

                                            

这篇关于如何运行需要提升和等待的子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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