bool shellExcute(const QString& path, const QString& paramater, bool admin_flag, bool wait_flag)
{
std::wstring file_path = std::wstring((const wchar_t*)file_path.utf16());//绝对路径
std::wstring paramater_line = std::wstring((const wchar_t*)paramater.utf16());
SHELLEXECUTEINFO shell_exec_info = { 0 };
shell_exec_info.cbSize = sizeof(SHELLEXECUTEINFO);
shell_exec_info.fMask = SEE_MASK_NOCLOSEPROCESS;
shell_exec_info.hwnd = NULL;
if (admin_flag)
{
shell_exec_info.lpVerb = TEXT("runas");//提权
}
else
{
shell_exec_info.lpVerb = NULL;//不提权
}
shell_exec_info.lpFile = file_path.c_str();//调用文件位置
shell_exec_info.lpParameters = paramater_line.c_str();//调用参数
shell_exec_info.lpDirectory = NULL;
shell_exec_info.nShow = SW_HIDE;//隐藏窗口执行
shell_exec_info.hInstApp = NULL;
if (!ShellExecuteEx(&shell_exec_info))
{
printf("Error: ShellExecuteEx failed 0x%x\n", GetLastError());//提取报错信息
return false;
}
if (wait_flag)
{
DWORD dw = WaitForSingleObject(shell_exec_info.hProcess, INFINITE);//等待新开程序执行完成
if (dw == WAIT_OBJECT_0)
{
qDebug() << "结束";
}
if (dw == WAIT_TIMEOUT)
{
qDebug() << "超时";
}
if (dw == WAIT_FAILED)
{
qDebug() << "无效";
}
}
return true;
}
//ShellExecuteEx的执行只能得到返回状态码,得不到返回值
ShellExecuteEx完美封装,程序提权执行,脚本vbs、bat、exe均可执行
于 2022-05-20 16:31:43 首次发布