zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    flask 基础
    新的项目部署
    linux (01) linux基础
    linux (04) linux安装mysql
    linux (06) redis安装
    linux (09) nginx反向代理,负载均衡
    linux (08) nginx入门详解
    linux (07) redis详解
    linux(05) 编译安装py3
    spring-boot war包部署(二)
  • 原文地址:https://www.cnblogs.com/ydxt/p/3598686.html
Copyright © 2011-2022 走看看