QProcess方法
1.判断进程是否存在
bool checkProcessByQProcess(const QString &strExe)
{
bool bResult = false;
// 判断进程是否存在
QProcess tasklist;
tasklist.start("tasklist",
QStringList() << "/NH"
<< "/FO" << "CSV"
<< "/FI" << QString("IMAGENAME eq %1").arg(strExe));
tasklist.waitForFinished();
QString strOutput = tasklist.readAllStandardOutput();
//如果进程存在,则结束进程
if (strOutput.startsWith(QString("\"%1").arg(strExe)))
{
qInfo() << "check process";
bResult = true;
}
return bResult;
}
2.结束指定进程
void killProcessByQProcess(const QString &strExe)
{
qInfo() << "killProcess start" << strExe;
//调用cmd的方法结束进程
QString strKill = QString("taskkill /im %1 /f").arg(strExe);
if (0 != QProcess::execute(strKill)) {
qInfo() << "killProcess error" << strExe;
}
}
系统API方法
1.检测进程是否存在(通过进程名)
void Wchar_tToString(std::string& szDst, wchar_t *wchar)
{
wchar_t * wText = wchar;
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用
char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用
szDst = psText;// std::string赋值
delete []psText;// psText的清除
}
bool checkProcessByName(const QString &strExe)
{
bool bResult = false;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
for (BOOL ret = Process32First(hSnapshot, &pe); ret; ret = Process32Next(hSnapshot, &pe)) {
wchar_t* process_str = pe.szExeFile;
std::string current_process_name;
Wchar_tToString(current_process_name, process_str);
if (current_process_name == strExe.toStdString()) {
qInfo() << "check process";
bResult = true;
break;
}
}
CloseHandle(hSnapshot);
return bResult;
}
2.检测进程是否存在(通过进程Id)
bool checkProcessById(DWORD processId)
{
bool bResult = false;
// 进程ID有效(非零)
if (0 != processId)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processId);
if (NULL == hProcess)
{
qInfo() << "exit ProcessID:" << processId << "OpenProcess fail" << GetLastError();
}
else
{
DWORD dwExitCode = 0;
BOOL bGet = GetExitCodeProcess(hProcess, &dwExitCode);
if (bGet && (STILL_ACTIVE == dwExitCode)) // 正在运行
{
qInfo() << "check process";
bResult = true;
}
else
{
qInfo() << "exit ProcessID:" << processId << "flag:" << bGet << "code:" << dwExitCode;
}
CloseHandle(hProcess);
hProcess = NULL;
}
}
else
{
qInfo() << "exit ProcessID invalid";
}
return bResult;
}
3.根据进程名获取进程id
DWORD getProcessIdByName(const char* strExe)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
for (BOOL ret = Process32First(hSnapshot, &pe); ret; ret = Process32Next(hSnapshot, &pe)) {
if (strcmp(_bstr_t(pe.szExeFile), strExe) == 0) {
CloseHandle(hSnapshot);
qInfo() << "getProcessIdByName: " << pe.th32ProcessID;
return pe.th32ProcessID;
}
}
CloseHandle(hSnapshot);
qInfo() << "getProcessIdByName error ";
return (DWORD)(-1);
}
4.根据进程id获取进程名
QString getProcessNameById(DWORD processId)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return QString();
}
PROCESSENTRY32 pe = { sizeof(pe) };
for (BOOL ret = Process32First(hSnapshot, &pe); ret; ret = Process32Next(hSnapshot, &pe)) {
if (pe.th32ProcessID == processId) {
CloseHandle(hSnapshot);
qInfo() << "getProcessNameById: " << pe.szExeFile;
return QString::fromWCharArray(pe.szExeFile);
}
}
CloseHandle(hSnapshot);
qInfo() << "getProcessNameById error ";
return QString();
}
5.结束指定进程
void killProcessById(DWORD processId)
{
HANDLE hProcess = NULL;
//打开目标进程
hProcess = OpenProcess(PROCESS_TERMINATE,FALSE, processId);
if (hProcess == NULL) {
qInfo() << "error :Open Process failed"<< GetLastError();
return;
}
//结束目标进程
DWORD ret=TerminateProcess(hProcess,0);
if(ret == 0) {
qInfo()<<"error :kill task failed" << GetLastError();
return;
}
}