Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <strong><a href="http://msdn.microsoft.com/en-us/library/bb762154.aspx" rel="noreferrer"><code>ShellExecuteEx</code></a></strong>, rather than <code>ShellExecute</code>. This function will provide a handle for the created process, which you can use to call <strong><a href="http://msdn.microsoft.com/en-us/library/ms687032.aspx" rel="noreferrer"><code>WaitForSingleObject</code></a></strong> on that handle to block until that process terminates. Finally, just call <strong><a href="http://msdn.microsoft.com/en-us/library/ms724211.aspx" rel="noreferrer"><code>CloseHandle</code></a></strong> on the process handle to close it.</p> <p>Sample code (most of the error checking is omitted for clarity and brevity):</p> <pre><code>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(&amp;shExInfo)) { WaitForSingleObject(shExInfo.hProcess, INFINITE); CloseHandle(shExInfo.hProcess); } </code></pre> <p>Specifying the "runas" verb for the <code>lpVerb</code> 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.</p> <p>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 <code>lpVerb</code>. A sample manifest is shown below:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"&gt; &lt;dependency&gt; &lt;dependentAssembly&gt; &lt;assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="X86" publicKeyToken="6595b64144ccf1df" language="*" /&gt; &lt;/dependentAssembly&gt; &lt;/dependency&gt; &lt;trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"&gt; &lt;security&gt; &lt;requestedPrivileges&gt; &lt;requestedExecutionLevel level="requireAdministrator" uiAccess="false"/&gt; &lt;/requestedPrivileges&gt; &lt;/security&gt; &lt;/trustInfo&gt; &lt;/assembly&gt; </code></pre> <p>Finally, make sure that whatever element in your application triggers execution of the process requiring UAC elevation is <a href="http://msdn.microsoft.com/en-us/library/aa511445.aspx" rel="noreferrer">marked accordingly</a>. 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:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="https://i.stack.imgur.com/ZNRqr.png" alt="UAC shield displayed on a button"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="https://i.stack.imgur.com/bVhs2.png" alt="UAC shield displayed on a menu item"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload