当前位置:网站首页>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
边栏推荐
- String class understanding - final is immutable
- Use pagoda + Xdebug + vscode to debug code remotely
- After NPM was upgraded, there was a lot of panic
- Understand the relationship between promise async await
- 2021-10-08
- node中的redis使用--ioredis
- Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
- Getting started with varnish
- 史上最强egg框架的error处理机制
- Generation of straightening body in 3D slicer
猜你喜欢
Getting started with varnish
Create cells through JS (while loop)
相机成像+单应性变换+相机标定+立体校正
C# ,类库
2021-10-12
Laravel [view]
狼叔来找翻译人员了--plato--持续翻译中.....
CPT 104_ TTL 09
Various situations of data / component binding
Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."
随机推荐
what is wifi6?
QT drawpixmap and DrawImage blur problem
The title bar will be pushed to coincide with the status bar
d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
Three of three JS (WEB GL) model deletion / scene emptying / simple sorting of memory release
Vscode settings JSON configuration
After NPM was upgraded, there was a lot of panic
2021-09-28
C# ,类库
Blender programmed terrain production
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
es6数组的使用
The address value indicated by the pointer and the value of the object indicated by the pointer (learning notes)
Write the declaration of a function to return the reference of the array, and the array contains 10 string objects (notes)
egg中的cors和proxy(づ ̄3 ̄)づ╭~踩坑填坑的过程~ToT~
WTL self drawn control library (cqscheckcomboxbox)
If I am PM's performance, movie VR ticket purchase display
Formal parameters, local variables and local static variables
QSslSocket::connectToHostEncrypted: TLS initialization failed
CPT 104_TTL 09