码迷,mamicode.com
首页 > 系统相关 > 详细

关闭进程接口封装

时间:2017-11-25 13:15:54      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:ret   char   erro   sse   int   read   return   dna   roc   

 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 }

 

关闭进程接口封装

标签:ret   char   erro   sse   int   read   return   dna   roc   

原文地址:http://www.cnblogs.com/HadesBlog/p/7894812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!