Tuesday, January 6, 2015

WinAPI process kill by name / C++ source

This program source is very easy to use and understand.

There are only 2 functions for killing process.

One is ListProcessInfo(void).
This function just prints all of the process name.

The other is KillProcess(TCHAR* TargetProcess);
Using ListPRocessInfo() function, You can get the process name you want to kill.

I used tchar.h.
so, TCHAR, _tprintf _tscanf .. etc.. is not different with char, printf, scanf.
you can use char, printf, scanf as well, don't worry.





#include<stdio.h>
#include<locale.h>
#include<string.h>
#include<stdlib.h>
#include<tchar.h>
#include<windows.h>
#include<tlhelp32.h>

void ListProcessInfo(void);
void KillProcess(TCHAR* TargetProcess);


int _tmain(int argc, TCHAR* argv[]){
 TCHAR TargetProcess[BUFSIZ];
 //Target Process name. It would be killed.
 while(1){
    ListProcessInfo(); // This function prints Process list
    _tprintf( _T("Input TargetProcess name (End :: EXIT) :: "));
    _tscanf( _T("%s"),TargetProcess);
    _tprintf(_T("input :: %s"), TargetProcess);

  if(_tcscmp(TargetProcess, _T("EXIT")) == 0)
   exit(EXIT_SUCCESS);
  KillProcess(TargetProcess);
  //Process be killed by this function.
 }
 return 0;
}


//Just print the process list.
void ListProcessInfo(void){
 HANDLE hProcessSnap =
  CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
   //The status of current processes. processes are dynamic, it's changing

  if(hProcessSnap == INVALID_HANDLE_VALUE){
    _tprintf( _T("CreateToolhelp32Snapshot error \n"));
    exit(EXIT_FAILURE);
 }
 PROCESSENTRY32 pe32; //This is just struct for saving process status
 pe32.dwSize = sizeof(PROCESSENTRY32);


 if( !Process32First(hProcessSnap, &pe32)){
  _tprintf( _T("Process32First error ! \n"));
  CloseHandle(hProcessSnap);
  return;
 }
//Using Process32First, you can get System process information.
//This is parent of all processes.
 _tprintf(_T("\t[Process name] \t[PID]\t[PPID]\t[ThreadID] \n"));

 do{
  _tprintf(_T("%25s %8d %8d %8d \n"),
   pe32.szExeFile,pe32.th32ProcessID, pe32.th32ParentProcessID,pe32.cntThreads);
 } while(Process32Next(hProcessSnap,&pe32));
//Process32Next give you an next process's information
 return;
}

//This function is for killing process.
//It's similar to ListProcessInfo() until find the process that you are killing.

void 
KillProcess(TCHAR* TargetProcess){
 //Argument TargetProcess is the process name that you want to kill.
 HANDLE hProcessSnap =
  CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0);
 if( hProcessSnap == INVALID_HANDLE_VALUE){
  _tprintf( _T("CreateToolhelp32Snapshot error! \n"));
  return;
 }
 PROCESSENTRY32 pe32;
 pe32.dwSize = sizeof(PROCESSENTRY32);
 if(!Process32First(hProcessSnap, &pe32)){
  _tprintf(_T("Process32First error ! \n"));
  CloseHandle(hProcessSnap);
  return;
 }
 HANDLE hProcess = NULL;
 BOOL isKill = FALSE;

do { //do Process32Next until the next process name is same with TargetProcess
  if(_tcscmp(pe32.szExeFile, TargetProcess) == 0)
   hProcess = OpenProcess(
   PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
  // When the function finds the process that you want to kill,
  // It gets the process's handle using OpenProcess(~),
 

  if(hProcess != NULL){
   TerminateProcess(hProcess, -1);
  //If handle status is no problem, It kills!

   isKill = TRUE;
   CloseHandle(hProcess);
   break;
  }
 } while( Process32Next(hProcessSnap, &pe32) );
 CloseHandle(hProcessSnap);
 if(isKill == FALSE)
  _tprintf( _T("Kill process fail. Try again! \n"));
}


thanks.

No comments:

Post a Comment