qt WINDOWS环境结束进程
其他
2018-07-27 16:19:51
阅读次数: 0
虽然方法能查到,这里总结一下,以后方便回顾一下
1、QProcess
-
-
QString c =
"taskkill /im DingTalk.exe /f";
-
-
直接执行CMD中的命令,注意空格!!!
2、系统API
根据进程名称先找到进程PID,再根据PID杀死进程
-
-
-
-
-
-
-
-
-
int killTaskl(const QString& exe)
-
-
-
-
-
-
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,
0);
-
if (hProcessSnap == INVALID_HANDLE_VALUE)
-
-
-
-
-
pe32.dwSize =
sizeof(PROCESSENTRY32);
-
-
if (!Process32First(hProcessSnap, &pe32))
-
-
CloseHandle(hProcessSnap);
-
-
-
-
-
-
while (Process32Next(hProcessSnap, &pe32))
-
-
-
int iLn = WideCharToMultiByte (CP_UTF8,
0,
const_cast<LPWSTR> (pe32.szExeFile),
static_cast<
int>(
sizeof(pe32.szExeFile)),
NULL,
0,
NULL,
NULL);
-
std::
string result (iLn, 0);
-
WideCharToMultiByte (CP_UTF8,
0, pe32.szExeFile,
static_cast<
int>(
sizeof(pe32.szExeFile)),
const_cast<LPSTR> (result.c_str()), iLn,
NULL,
NULL);
-
if (
0 ==
strcmp(exe.toStdString().c_str(), result.c_str ()))
-
-
dwPid = pe32.th32ProcessID;
-
-
-
-
-
-
-
CloseHandle(hProcessSnap);
-
-
-
-
-
hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,dwPid);
-
-
qDebug()<<
"Open Process fAiled ,error:"<<GetLastError();
-
-
-
-
DWORD ret=TerminateProcess(hProcess,
0);
-
-
qDebug()<<
"kill task faild,error:"<<GetLastError();
-
-
-
-
-
转载自blog.csdn.net/u013934107/article/details/81000695