Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As Tamás pointed out you need to launch a new process with elevated rights. I searched a lot in the past but I did not find any way to elevate the rights of the current process.</p> <p>Lets say your primary app is App1.exe and then you call a secondary process App2.exe which requires elevated rights.</p> <hr> <p><strong>A. You can embed a manifest in your App2.exe but the simpler way is to create a manifest file [a text file] named App2.exe.manifest with the following contents and put it in the same directory as App2.exe.</strong> Note: !! Strangely enough, if the name of your application is not App2.exe but App2_install.exe or App2_setup.exe (i.e. if the application name contains the "install" or "setup") an UAC Dialog will appear automatically in Windows Vista / Windows 7 and will ask for elevated rights even there is no manifest file !! This is a sample of the manifest file:</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;trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"&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> <hr> <p><strong>B. You can use a code like the following in App1.exe to launch the App2.exe</strong></p> <pre><code>QString AppToExec = qApp-&gt;applicationDirPath() + "/App2.exe"; // Put any required parameters of App2.exe to AppParams string QString AppParams = ""; if (0 != genWin32ShellExecute(AppToExec, "", // default verb: "open" or "exec" AppParams, false, // run hidden true)) // wait to finish { // (...) handle error } </code></pre> <p>...and finally, this is the code of the Win32 function genWin32ShellExecute() I created to launch a process or open a document when using QT on a Win32 O/S:</p> <p><strong>Header:</strong> </p> <pre><code>#ifdef Q_OS_WIN // Implement genWin32ShellExecute() especially for UAC #include "qt_windows.h" #include "qwindowdefs_win.h" #include &lt;shellapi.h&gt; int genWin32ShellExecute(QString AppFullPath, QString Verb, QString Params, bool ShowAppWindow, bool WaitToFinish); #endif </code></pre> <p><strong>CPP:</strong> </p> <pre><code>// Execute/Open the specified Application/Document with the given command // line Parameters // (if WaitToFinish == true, wait for the spawn process to finish) // // Verb parameter values: // "" The degault verb for the associated AppFullPath // "edit" Launches an editor and opens the document for editing. // "find" Initiates a search starting from the specified directory. // "open" Launches an application. If this file is not an executable file, its associated application is launched. // "print" Prints the document file. // "properties" Displays the object's properties. // // Ret: 0 = success // &lt;0 = error #ifdef Q_OS_WIN int genWin32ShellExecute(QString AppFullPath, QString Verb, QString Params, bool ShowAppWindow, bool WaitToFinish) { int Result = 0; // Setup the required structure SHELLEXECUTEINFO ShExecInfo; memset(&amp;ShExecInfo, 0, sizeof(SHELLEXECUTEINFO)); ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = NULL; if (Verb.length() &gt; 0) ShExecInfo.lpVerb = reinterpret_cast&lt;const WCHAR *&gt;(Verb.utf16()); ShExecInfo.lpFile = NULL; if (AppFullPath.length() &gt; 0) ShExecInfo.lpFile = reinterpret_cast&lt;const WCHAR *&gt;(AppFullPath.utf16()); ShExecInfo.lpParameters = NULL; if (Params.length() &gt; 0) ShExecInfo.lpParameters = reinterpret_cast&lt;const WCHAR *&gt;(Params.utf16()); ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = (ShowAppWindow ? SW_SHOW : SW_HIDE); ShExecInfo.hInstApp = NULL; // Spawn the process if (ShellExecuteEx(&amp;ShExecInfo) == FALSE) { Result = -1; // Failed to execute process } else if (WaitToFinish) { WaitForSingleObject(ShExecInfo.hProcess, INFINITE); } return Result; } #endif </code></pre>
 

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