CreateProcess to run as administrator

无人久伴 提交于 2019-12-19 10:28:20

问题


In my Win32 application I have the ability to run child processes with redirected input and output to anonymous pipes that I create and manage - this all works with the CreateProcess() function. However on Win7 (and presumably Vista) if that process is required to be run as administrator then this fails. So what I am looking for is a way to do the equivalent of the "run as administrator" command in explorer that will bring up the standard UAC prompt and then create the process with the elevated permissions. I have seen articles that talk about using the "runas" option to ShellExecute to do this, but then as I see it I don't have the control I need to redirect std input and output to my pipes. Any suggestions?


回答1:


If you put a manifest on the child app, and if you use ShellExecute to launch, it will use the manifest and there will be a UAC prompt. (I prefer this to the "runas" approach in most cases.) This may allow you to launch the app the way you like without worrying about UAC, and get the elevating from the manifest.

However, you may have issues if the "manage" part you refer to involves a lower privilege app communicating to an elevated one. UAC forbids that.




回答2:


It should be possible to set the environment variable __COMPAT_LAYER to RunAsAdmin before the CreateProcess call which tells windows to just force the run as admin via the compatibility flags. The environment variable can be deleted once the process is started.



来源:https://stackoverflow.com/questions/3395015/createprocess-to-run-as-administrator

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