windows下强制杀死指定进程

void kill_process(std::wstring p_name)
{
    HANDLE h_process_snap = NULL;
    PROCESSENTRY32 pe32;
    DWORD dw_priority_class;

    bool bFind = false;
    // Take a snapshot of all processes in the system.
    h_process_snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (h_process_snap == INVALID_HANDLE_VALUE){
        return;
    }

    // Set the size of the structure before using it.
    pe32.dwSize = sizeof(PROCESSENTRY32);

    // Retrieve information about the first process,
    // and exit if unsuccessful
    if (!Process32First(h_process_snap, &pe32)){
        CloseHandle(h_process_snap);          // clean the snapshot object
        return;
    }

    // Now walk the snapshot of processes, and
    // display information about each process in turn
    do
    {
        // Retrieve the priority class.
        dw_priority_class = 0;      

       if (std::wcscmp(pe32.szExeFile, pName.c_str()) ==0){
            find = true;
        }

        if (bFind) {
            HANDLE h_process = ::OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
            ::TerminateProcess(h_process, 0);
            CloseHandle(h_process);
            break;
        }
    } while (Process32Next(h_process_snap, &pe32));

    CloseHandle(h_process_snap);
}

发布了63 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_33048069/article/details/103824211
今日推荐