I’m trying to run PowerShell in admin using ShellExecute within a Qt program, but the arguments don’t seem to be transferring.
QString ruleName = QCoreApplication::applicationName();
QString exePath = QCoreApplication::applicationFilePath();
// Define the embedded PowerShell scripts as strings
const QString checkFirewallScript = QString(
"param([string]$ruleName,[string]$exePath)"
"a powershell script of 70 lines"
).arg(ruleName, exePath);
// Construct the PowerShell command with the script
QString command = QString("powershell.exe -Command "%1"").arg(checkFirewallScript);
// Convert the command to a wide string
LPCWSTR commandC = (const wchar_t*) command.utf16();
// Use ShellExecute to run the command as administrator
HINSTANCE exitCode = ShellExecute(NULL, L"runas", L"powershell.exe", commandC, NULL, SW_SHOWNORMAL);
My exit code is always 42 though I read where above 32, it is just the instance number. I get a warning within the Qt error box of “QString::arg: 2 argument(s) missing in” and it spits out the script. I have no percent signs and the script works in powershell by itself (when I add the 2 “missing” arguments directly to script). This also worked without issue when I was using Qt Process, but I want UAC to be invoked so I am using ShellExecute (which is ShellExecuteW according to headers).