Home >Backend Development >C++ >How Can I Programmatically Elevate Process Privileges in Windows?
Programmatically Elevating Process Privileges in Windows
This guide explains how to programmatically elevate process privileges in Windows, a necessary step when running privileged tasks like installing services with InstallUtil.exe
. We'll explore two methods: using the runas
verb and embedding an application manifest.
The simplest method involves using the Verb
property of the ProcessStartInfo
object. Setting Verb
to "runas" prompts the User Account Control (UAC) dialog, requesting user permission to elevate the process. This is similar to right-clicking an executable and selecting "Run as administrator".
However, if you want to avoid repeated UAC prompts, especially during long-running processes, you can elevate the entire host process. This is achieved by creating and including an application manifest file (a UAC manifest) that specifies the highestAvailable
execution level. The UAC prompt appears only once at application startup, and all subsequent child processes inherit these elevated privileges.
Here's how to elevate using the runas
verb:
<code class="language-csharp">ProcessStartInfo startInfo = new ProcessStartInfo(m_strInstallUtil, strExePath); startInfo.UseShellExecute = true; startInfo.Verb = "runas"; System.Diagnostics.Process.Start(startInfo);</code>
Setting UseShellExecute
to true
and Verb
to "runas"
ensures the process runs with elevated permissions, subject to UAC approval. The alternative, using an application manifest, requires more setup but avoids repeated UAC interactions.
The above is the detailed content of How Can I Programmatically Elevate Process Privileges in Windows?. For more information, please follow other related articles on the PHP Chinese website!