关闭进程接口封装

 1 #pragma once
 2 
 3 #ifndef WINDOWS_H
 4 #define WINDOWS_H
 5 #include<Windows.h>
 6 #endif
 7 
 8 #ifndef TLHELP32_H
 9 #define TLHELP32_H
10 #include <TlHelp32.h>
11 #endif
12 
13 BOOL KillProcessbyPid(DWORD ProcessId)
14 {
15     HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, ProcessId);
16     if (hProcess == NULL)
17         return FALSE;
18     if (!TerminateProcess(hProcess, 0))
19         return FALSE;
20     return TRUE;
21 }
22 
23 bool KillProcessbyName(char *threadname)
24 {
25     HANDLE         hProcessSnap;
26     HANDLE         hProcess;
27     PROCESSENTRY32 stcPe32 = { 0 }; 
28     stcPe32.dwSize = sizeof(PROCESSENTRY32);
29     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
30     if (hProcessSnap == INVALID_HANDLE_VALUE)
31         return false;
32 
33     if (!Process32First(hProcessSnap, &stcPe32))
34     {
35         CloseHandle(hProcessSnap);
36         return false;
37     }
38     do {
39         
40         hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
41             stcPe32.th32ProcessID);
42         if (hProcess)
43         {
44             int nPriority = GetPriorityClass(hProcess);
45 
46             CloseHandle(hProcess);                        
47         }
48         
49         if (strcmp(threadname, stcPe32.szExeFile) == 0) {
50             
51             KillProcessbyPid(stcPe32.th32ProcessID);
52             
53         }
54 
55     } while (Process32Next(hProcessSnap, &stcPe32));
56     SetLastError(0);
57     CloseHandle(hProcessSnap);
58     return true;
59 }
原文地址:https://www.cnblogs.com/HadesBlog/p/7894812.html