appo icon

Kill process [Win32 API] (Option 2)

appo | PRO | 12/27/13 02:50:20 PM UTC | 0 ⭐ | 624 👁️ | Never ⏰ | []
C++ |

1.15 KB

|

None

|

0 👍

/

0 👎

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
 
BOOL KillProcessByName(char *szProcessToKill);
 
int main(int argc, char* argv[]){
    if(argc > 1){
        KillProcessByName(argv[1]);
    }else{
        KillProcessByName("notepad.exe"); //process Name
    }
    return 0;
}
 
BOOL KillProcessByName(char *szProcessToKill){
    HANDLE hProcessSnap;
    HANDLE hProcess;
    PROCESSENTRY32 pe32;
    DWORD dwPriorityClass;
 
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  // A snapshot of all processes
 
    if(hProcessSnap == INVALID_HANDLE_VALUE){
        return( FALSE );
    }
 
    pe32.dwSize = sizeof(PROCESSENTRY32);
 
    if(!Process32First(hProcessSnap, &pe32)){
        CloseHandle(hProcessSnap);     
        return( FALSE );
    }
 
    do{
        if(!strcmp(pe32.szExeFile,szProcessToKill)){    //  Checks whether the name of the process to be killed later
            hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID);  // Handle process
            TerminateProcess(hProcess,0);   //Kill process by handle 
            CloseHandle(hProcess);  // close handle
        } 
    }while(Process32Next(hProcessSnap,&pe32));  
 
    CloseHandle(hProcessSnap); 
    return( TRUE );
}
 
// Coded by Appo //

Comments