zoukankan      html  css  js  c++  java
  • 检查程序进程是否存在/强制杀掉程序进程

    原文:http://www.cnblogs.com/walker-lc/p/3480573.html

    #include <Windows.h>
    #include <TlHelp32.h>
    
    bool IsAppRunning()
    {
        bool m_running = false;
    
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (hSnapshot == INVALID_HANDLE_VALUE)
        {
            return m_running;
        }
    
        PROCESSENTRY32 pe = {0};
        pe.dwSize = sizeof(PROCESSENTRY32);
        if (!Process32First(hSnapshot, &pe))
        {
            CloseHandle(hSnapshot);
            return m_running;
        }
    
        while ( 1 )
        {
            if (!Process32Next(hSnapshot, &pe))
            {
                break;
            }
    
            if (lstrcmpi(pe.szExeFile, APP_PROG_NAME)==0)
            {
                m_running = true;
                break;
            }
        }
    
        CloseHandle(hSnapshot);
    
        return m_running;
    }
    
    int ShutDownApp()
    {
        int result = -1;
    
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (hSnapshot == INVALID_HANDLE_VALUE)
        {
            return result;
        }
    
        PROCESSENTRY32 pe = {0};
        pe.dwSize = sizeof(PROCESSENTRY32);
        if (!Process32First(hSnapshot, &pe))
        {
            CloseHandle(hSnapshot);
            return result;
        }
    
        while ( 1 )
        {
            if (!Process32Next(hSnapshot, &pe))
            {
                break;
            }
    
            if (lstrcmpi(pe.szExeFile, APP_PROG_NAME)==0)
            {
                HANDLE process = OpenProcess(PROCESS_TERMINATE, 0, pe.th32ProcessID);
                if (process)
                {
                    result = 0;
                    TerminateProcess(process, 0);
                    CloseHandle(process);
                }
            }
        }
    
        CloseHandle(hSnapshot);
        ::Sleep(100);
    
        return result;
    }
  • 相关阅读:
    mysql_day04
    四则运算_EX
    第二阶段站立会议9
    对搜狗输入法的评价
    找水王
    第二阶段站立会议8
    第二阶段站立会议7
    第二次站立冲刺会议6
    第二阶段站立冲刺会议5
    第二阶段站立冲刺会议4
  • 原文地址:https://www.cnblogs.com/lizhigang/p/7168620.html
Copyright © 2011-2022 走看看