虽然方法能查到,这里总结一下,以后方便回顾一下
1、QProcess
-
QProcess p;
-
QString c = "taskkill /im DingTalk.exe /f";
-
p.execute(c);
-
p.close();
直接执行CMD中的命令,注意空格!!!
2、系统API
根据进程名称先找到进程PID,再根据PID杀死进程
-
-
-
-
using namespace std;
-
/*根据进程名称杀死进程
-
*1、根据进程名称找到PID
-
*2、根据PID杀死进程
-
*/
-
int killTaskl(const QString& exe)
-
{
-
//1、根据进程名称找到PID
-
HANDLE hProcessSnap;
-
PROCESSENTRY32 pe32;
-
-
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
-
if (hProcessSnap == INVALID_HANDLE_VALUE)
-
{
-
return -1;
-
}
-
-
pe32.dwSize = sizeof(PROCESSENTRY32);
-
-
if (!Process32First(hProcessSnap, &pe32))
-
{
-
CloseHandle(hProcessSnap);
-
return -1;
-
}
-
-
BOOL bRet = FALSE;
-
DWORD dwPid = -1;
-
while (Process32Next(hProcessSnap, &pe32))
-
{
-
//将WCHAR转成const char*
-
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;
-
bRet = TRUE;
-
qDebug()<< "zhaodao";
-
break;
-
}
-
}
-
-
CloseHandle(hProcessSnap);
-
qDebug()<<dwPid;
-
2、根据PID杀死进程
-
HANDLE hProcess= NULL;
-
//打开目标进程
-
hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,dwPid);
-
if (hProcess== NULL) {
-
qDebug()<< "Open Process fAiled ,error:"<<GetLastError();
-
return -1;
-
}
-
//结束目标进程
-
DWORD ret=TerminateProcess(hProcess, 0);
-
if(ret== 0) {
-
qDebug()<< "kill task faild,error:"<<GetLastError();
-
return -1;
-
}
-
-
return 0;