windows下杀死任务管理器中进程

目的

了解windows下如何强制性杀死任务管理器进程

code

#include "mainwindow.h"

#include 
#include 
#include 
#include 

void killTask(const QString& processName);
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    killTask("notepad++.exe");
    return a.exec();
}

void killTask(const QString& processName)
{
    qInfo() << __FUNCTION__ << __LINE__;
    DWORD calcID = 0;
    PROCESSENTRY32 pe32;
    pe32.dwSize=sizeof(pe32);
    HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    BOOL bMore=::Process32First(hProcessSnap,&pe32);
#define ProceseLength 2048
    char* m_char = new char[ProceseLength];
    while(bMore){
        int len= WideCharToMultiByte(CP_ACP,0,pe32.szExeFile,wcslen(pe32.szExeFile),NULL,0,NULL,NULL);
        if(len > ProceseLength){
            delete[] m_char;
            m_char = new char[len];
        }
        WideCharToMultiByte(CP_ACP,0,pe32.szExeFile,wcslen(pe32.szExeFile),m_char,len,NULL,NULL);
        m_char[len]='\0';
        // eg: "calc.exe"
        qInfo() << __FUNCTION__ << "process name: " << QString(m_char);
        if(strcmp(processName.toLocal8Bit().constData(), m_char) == 0){
            calcID = pe32.th32ProcessID;
        }
        if(calcID != 0)
            bMore = FALSE;
        else
            bMore=::Process32Next(hProcessSnap, &pe32);
    }
    delete[] m_char;
    if(calcID != 0){
          HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, calcID);
          if(hProcess != NULL){
              TerminateProcess(hProcess, 0);
          }
    }
    qInfo() << __FUNCTION__ << __LINE__;
}

你可能感兴趣的:(多技术领域栏,windows)