当前位置:网站首页>Usage and difference of shellexecute, shellexecuteex and winexec in QT
Usage and difference of shellexecute, shellexecuteex and winexec in QT
2022-04-23 05:30:00 【Oriental forgetfulness】
Today I want to introduce several ways to start the process with administrator privileges .
Namely ShellExecute,ShellExecuteEx,WinExec.
Their respective characteristics :
ShellExecute: It's through windows The shell opens any file , Non executable files are automatically opened through the associated program , For executable files , Not much difference , however shellexcute You can specify the working path of the runtime
ShellExecuteEx: It goes with ShellExecute almost , The difference is its return value and support blocking operation .
WinExec: Only for executable files , It's an old function , Although easy to use , But the compatibility is not good .
ShellExecute Usage mode :
filename = "xxx.exe";
std::wstring operate;
if (info.installAuth == 0)// Have administrator rights
operate = QString("runas").toStdWString();
else// No administrator rights
operate = L"open";
std::wstring file = QString("\"" + filename + "\""). toStdWString();// Executable files
filename = filename.left(filename.size()-QString(filename.split("/").last()).size());// Get the path of the execution file
std::wstring path = filename.toStdWString();// Execution file path .
QString para = ”1“;// Parameters 1
QString para2 = “test”;// Parameters 2
QString tmp = para+“ ”+para2;
std::wstring lpParameters = tmp.toStdWString();// Merging parameters
int ret = (int)ShellExecute(NULL,operate.c_str(), file.c_str(),lpParameters.c_str(),path.c_str(),SW_SHOWNORMAL);
QString sRet;
switch(ret)
{
case 0:
sRet = QString("memory lack.");
break;
case 2:
sRet = QString("filename is error.");
break;
case 3:
sRet = QString("file path is error.");
break;
case 11:
sRet = QString("exe is invaliable.");
break;
case 26:
sRet = QString("shared error.");
break;
case 27:
sRet = QString("file is error or be short.");
break;
case 28:
sRet = QString("open time out.");
break;
case 29:
sRet = QString("DDE task failed.");
break;
case 30:
sRet = QString("undering other's DDE");
break;
case 31:
sRet = QString("no linked process.");
break;
default:
sRet = QString("unknow error.");
break;
}
if(ret <= 32){// Return value greater than 32 Express ShellExecute Successful implementation
}
else{
}
ShellExecuteEx Usage mode :
filename = "xxx.exe";
std::wstring operate;
if (info.installAuth == 0)// Have administrator rights
operate = QString("runas").toStdWString();
else// No administrator rights
operate = L"open";
std::wstring file = QString("\"" + filename + "\""). toStdWString();// Executable files
filename = filename.left(filename.size()-QString(filename.split("/").last()).size());// Get the path of the execution file
std::wstring path = filename.toStdWString();// Execution file path .
QString para = ”1“;// Parameters 1
QString para2 = “test”;// Parameters 2
QString tmp = para+“ ”+para2;
std::wstring lpParameters = tmp.toStdWString();// Merging parameters
SHELLEXECUTEINFO shExecinfo = {0};// Create an executable object
shExecinfo.cbSize = sizeof(SHELLEXECUTEINFO);// Structure size
shExecinfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExecinfo.hwnd = NULL;// Parent window handle
shExecinfo.lpVerb = operate.c_str();// jurisdiction
shExecinfo.lpFile = file.c_str();// Executable files
shExecinfo.lpParameters = lpParameters.c_str();// Parameters
shExecinfo.lpDirectory = path.c_str();// route
shExecinfo.nShow = SW_SHOWNORMAL// Display mode ;
shExecinfo.hInstApp = NULL;
int ret = (int)ShellExecuteEx(&shExecinfo);
if (ret == 0)// Execution failure
{}
else// Successful implementation
{}
WaitForSingleObject(shExecinfo.hProcess, INFINITE);// Blocking , until ShellExecuteEx() The program executed is closed
WinExec Usage mode :
WinExec("cmd /c xxx.exe",SW_HIDE);// Direct execution , and system() similar , It's just system() Executive cmd Without administrator privileges
版权声明
本文为[Oriental forgetfulness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220543306899.html
边栏推荐
- Three 之 three.js (webgl)模型的删除/场景的清空/内存的释放 的简单整理
- [triangle Yang Hui triangle printing odd even cycle JS for break cycle]
- Differences between auto and decltype inference methods (learning notes)
- Executable program execution process
- Processus d'exécution du programme exécutable
- QSS, qdateedit, qcalendarwidget custom settings
- Requirements for SQL server to retrieve SQL and user information
- Create cells through JS (while loop)
- 史上最强egg框架的error处理机制
- what is wifi6?
猜你喜欢

Modèle axé sur le domaine DDD (III) - gestion des transactions à l'aide de Saga

Double click The jar package cannot run the solution

Requirements for SQL server to retrieve SQL and user information

what is wifi6?

2021-11-01

Branch and loop statements

uni使用的一些坑
![Laravel [view]](/img/39/71db98d8832d9419bcc1097594d1b6.png)
Laravel [view]

領域驅動模型DDD(三)——使用Saga管理事務

Getting started with varnish
随机推荐
[no title] Click the classification jump page to display the details
If the route reports an error after deployment according to the framework project
Three of three JS (webgl) is simple to draw lines / arcs according to points (based on linegeometry / line2 / linematerial, draw two arc segments based on the center of the circle)
Escape characters \ splicing of data formats
varnish入门
QT displays the specified position and size of the picture
相机成像+单应性变换+相机标定+立体校正
[the background color changes after clicking a line]
Use of uniapp native plug-ins
Source code analysis of how to use jump table in redis
Redis的基本知识
phphphphphphphp
Create cells through JS (while loop)
Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."
可執行程序執行流程
Uniapp hot update with progress bar
open3d材质设置参数分析
Basic knowledge of redis
2021-10-12
TSlint注释忽略错误和RESTful理解