c++结束进程的程序

 1 //#include <winbase.h>
 2 #include <windows.h>
 3 #include <process.h>
 4 #include <Tlhelp32.h>
 5 #include <tchar.h>
 6 
 7 
 8 BOOL FindAndKillProcessByName(LPCTSTR strProcessName)
 9 {
10 if(NULL == strProcessName)
11 {
12 return FALSE;
13 }
14 
15 HANDLE handle32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
16 
17 if (INVALID_HANDLE_VALUE == handle32Snapshot)
18 {
19 return FALSE;
20 }
21 
22 
23 
24 PROCESSENTRY32 pEntry;      
25 pEntry.dwSize = sizeof( PROCESSENTRY32 );
26 
27 
28 
29 //Search for all the process and terminate it
30 
31 if(Process32First(handle32Snapshot, &pEntry))
32 {
33 BOOL bFound = FALSE;
34 if (!_tcsicmp(pEntry.szExeFile, strProcessName))
35 {
36 bFound = TRUE;
37 }
38 while((!bFound)&&Process32Next(handle32Snapshot, &pEntry))
39 {
40 if (!_tcsicmp(pEntry.szExeFile, strProcessName))
41 {
42 bFound = TRUE;
43 }
44 }
45 if(bFound)
46 {
47 CloseHandle(handle32Snapshot);
48 HANDLE handLe =  OpenProcess(PROCESS_TERMINATE , FALSE, pEntry.th32ProcessID);
49 BOOL bResult = TerminateProcess(handLe,0);
50 return bResult;
51 }
52 }
53 CloseHandle(handle32Snapshot);
54 return FALSE;    
55 }
56 int main(){
57 
58 FindAndKillProcessByName("Notepad.exe");
59 return 0;
60 }

 

posted on 2014-03-13 15:36  鸳都学童  阅读(2131)  评论(0编辑  收藏  举报

导航