Advertisement
appo

Kill process [Win32 API] (Option 2)

Dec 27th, 2013
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <windows.h>
  2. #include <tlhelp32.h>
  3. #include <stdio.h>
  4.  
  5. BOOL KillProcessByName(char *szProcessToKill);
  6.  
  7. int main(int argc, char* argv[]){
  8.     if(argc > 1){
  9.         KillProcessByName(argv[1]);
  10.     }else{
  11.         KillProcessByName("notepad.exe"); //process Name
  12.     }
  13.     return 0;
  14. }
  15.  
  16. BOOL KillProcessByName(char *szProcessToKill){
  17.     HANDLE hProcessSnap;
  18.     HANDLE hProcess;
  19.     PROCESSENTRY32 pe32;
  20.     DWORD dwPriorityClass;
  21.  
  22.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  // A snapshot of all processes
  23.  
  24.     if(hProcessSnap == INVALID_HANDLE_VALUE){
  25.         return( FALSE );
  26.     }
  27.  
  28.     pe32.dwSize = sizeof(PROCESSENTRY32);
  29.  
  30.     if(!Process32First(hProcessSnap, &pe32)){
  31.         CloseHandle(hProcessSnap);    
  32.         return( FALSE );
  33.     }
  34.  
  35.     do{
  36.         if(!strcmp(pe32.szExeFile,szProcessToKill)){    //  Checks whether the name of the process to be killed later
  37.             hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID);  // Handle process
  38.             TerminateProcess(hProcess,0);   //Kill process by handle
  39.             CloseHandle(hProcess);  // close handle
  40.         }
  41.     }while(Process32Next(hProcessSnap,&pe32));  
  42.  
  43.     CloseHandle(hProcessSnap);
  44.     return( TRUE );
  45. }
  46.  
  47. // Coded by Appo //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement