SHARE
TWEET

Kill process [Win32 API] (Option 2)

appo Dec 27th, 2013 364 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 //
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top