• 终止其他进程


    #include"stdafx.h"
    #include <windows.h>
    #include <TlHelp32.h>
    #include <iostream>
    using namespace std;

    BOOL KillProcess(DWORD ProcessId)
    {
        HANDLE hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,ProcessId);
        if(hProcess==NULL)
            return FALSE;
        if(!TerminateProcess(hProcess,0))
            return FALSE;
        return TRUE;
    }

    DWORD GetSpecifiedProcessId(const char *pszProcessName)
    {
      DWORD id=0;
      //获得系统快照句柄 (通俗的讲, 就是得到当前的所有进程)
      HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0) ;
      PROCESSENTRY32 pInfo; //用于保存进程信息的一个数据结构
      pInfo.dwSize = sizeof(pInfo); //使用这个结构之前,先设置它的大小
      //从快照中获取进程列表
      Process32First(hSnapShot, &pInfo) ; //从第一个进程开始循环
      do
      {
      //这里的 pszProcessName 为进程名称
        if(strcmp(pInfo.szExeFile, pszProcessName) == 0)
        {
          id = pInfo.th32ProcessID ;
          break ;
        }
      }while(Process32Next(hSnapShot, &pInfo) != FALSE);

      CloseHandle(hSnapShot);
      return id; //id 就是你要的进程PID 了..
    }


    int _tmain (int argc, LPTSTR argv[])
    {
        

        if(KillProcess( GetSpecifiedProcessId("QQProtect.exe")))
        {
            cout<<"成功"<<endl;
        }else{
            cout<<"失败"<<endl;
        }
        
        system("pause");
        return 0;
    }

  • 相关阅读:
    237. Delete Node in a Linked List
    430. Flatten a Multilevel Doubly Linked List
    707. Design Linked List
    83. Remove Duplicates from Sorted List
    160. Intersection of Two Linked Lists
    426. Convert Binary Search Tree to Sorted Doubly Linked List
    142. Linked List Cycle II
    类之间的关系
    初始化块
    明确类和对象
  • 原文地址:https://www.cnblogs.com/duyy/p/3699863.html
Copyright © 2020-2023  润新知