问题
I have an EXE running with normal privileges, but there are cases (for example using VSS / volume shadow copy) where I need admin privileges (if I'm not mistaken)
The idea is to put the code that needs extra privileges in a separate EXE and launch it as needed.
It's something that I'll rarely need, but I still need to have it as a last resort option (this is to say that I'll rarely need to call it, maybe once/twice a day on average)
My question is: How can I call an admin-elevated process from a normal EXE?
回答1:
On Vista and later with UAC enabled, the best option is to provide the second EXE with a UAC manifest (preferrably embedded as a resource, but can also be done with an external .manifest file) that sets the requestedExecutionLevel
value to requireAdministrator
. Then you can just CreateProcess()
the EXE file normally. If providing a manifest is not an option, or if you need to support XP and/or non-UAC setups, then you can use ShellExecute/Ex()
specifying the "runas"
verb instead. Under any of these setups, the OS will prompt the user for permission and account credentials, and then apply the appropriate security rights to the new process as needed.
Another option is the third-party CreateProcess...Elevated()
(and ShellExecute...Elevated()
) implementations provided in this article:
Vista UAC: The Definitive Guide
来源:https://stackoverflow.com/questions/14130282/launch-an-exe-with-elevated-privileges-from-a-normal-non-elevated-one