当前位置:网站首页>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
边栏推荐
- 可執行程序執行流程
- Requirements for SQL server to retrieve SQL and user information
- phphphphphphphp
- Vscode settings JSON configuration
- Open source rule engine - Ice: dedicated to solving flexible and complex hard coding problems
- X86 assembly syntax: at & T and Intel
- Cross domain CORS relationship~
- Create process memory management copy_ Mm - processes and threads (IX)
- 五一劳动节期间什么理财产品会有收益?
- Introduction to qqueue
猜你喜欢

Project manager's thinking mode worth trying: project success equation

!!!!!!!!!!!!!!!!!!

Uncle wolf is looking for a translator -- Plato -- ongoing translation
![Laravel [view]](/img/39/71db98d8832d9419bcc1097594d1b6.png)
Laravel [view]

Nécessité de précharger les cookies dans le sélénium

2021-11-08

Three methods of list rendering

Parameter analysis of open3d material setting

what is wifi6?
![[no title] Click the classification jump page to display the details](/img/a6/fb302129c06e07b8d1eddeaabc9d2d.png)
[no title] Click the classification jump page to display the details
随机推荐
npm升级后问题,慌得一批
catkin_package到底干了什么
QT displays the specified position and size of the picture
egg的static的前缀是可以修改惹,靴靴
Cross domain CORS relationship~
Generation of straightening body in 3D slicer
Intel SGX preliminary learning and understanding notes (continuously updated)
日志简介和构建web应用
相机成像+单应性变换+相机标定+立体校正
Laravel implements the Holy Grail model with template inheritance
JVM memory and memory overflow exceptions (personal summary)
Parameter analysis of open3d material setting
Arithmetic and logical operations
X86 assembly syntax: at & T and Intel
Why can't V-IF and V-for be used together
CMake基础教程(39)pkgconfig
selenium預先加載cookie的必要性
Traversal array, object parent-child communication props / $emit
Create a tabbar component under the components folder, which is public
五一劳动节期间什么理财产品会有收益?