Click here to Skip to main content
Click here to Skip to main content

Kill Application Using Win32 APIs

By , 27 May 2007
 

Introduction

We can use it in consol or dialog base application. For that you need to add one header file in your application "Tlhelp32.h". It kills process which is specified in program it may be self or any other application running in system .This application can be helpful incase application is giving problem at time of closing or any memory leak is there then this application can solve that problem.

Using the code

******************** Method One for finding process and killing *****************

There are two stages in this code.

1. We are enumerating system processes. We are getting all process name and comparing it.If We finds that same It meance we found application running and we can retreive Handle and ThreadID of that application.

 
 /////////////////////////////////////////////////////////////////////////
 // This part of code is enumerating process.
 /////////////////////////////////////////////////////////////////////////
  HANDLE hndl = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  DWORD dwsma = GetLastError();
  DWORD dwExitCode = 0;
  PROCESSENTRY32  procEntry={0};
  procEntry.dwSize = sizeof( PROCESSENTRY32 );
  Process32First(hndl,&procEntry);
  do
  {
    if(!strcmpi(procEntry.szExeFile,"wweb32.exe"))
    {
        AfxMessageBox("Application found");
    break;
    }
    //cout<<"Running Process "<<"          "<<procEntry.szExeFile<<endl;
  }while(Process32Next(hndl,&procEntry));
 

2. Once we found process, Then we will retreive processID and ThreadID of that process.If we have that details of process then we can kill that process using TerminateProcess API. But this API may fail if there is no access so we need to take first access using OpenProcess API and first parameter of that API must be "PROCESS_ALL_ACCESS". Now we have full access of process then we can execute TerminateProcess API. This will terminate or kill process from our system.

 
  HANDLE hHandle;
  hHandle = ::OpenProcess(PROCESS_ALL_ACCESS,0,procEntry.th32ProcessID);
  
  ::GetExitCodeProcess(hHandle,&dwExitCode);
  ::TerminateProcess(hHandle,dwExitCode);
 

* * * * * * * * * * * * * * Method two for killing self Application * * * * * * * * * * * *

For killing self application we don't need to enumerate process which I have given above. That we can do using API GetCurrentProcessId. We need to use if we want to kill and other process running in system. This is one method using which we can retrieve ProcessId. Once we have ProcessId then we have control of that process. Here is code how you can kill your application. You just need to past this to 5 line code to your application.

  HANDLE hHandle;
  DWORD dwExitCode = 0;
  hHandle = ::OpenProcess(PROCESS_ALL_ACCESS,0,GetCurrentProcessId());
  ::GetExitCodeProcess(hHandle,&dwExitCode);
  ::TerminateProcess(hHandle,dwExitCode);

Points of Interest

Application can kill itself if problem comes in closing. Interesting for developers who are tring lot for closing application error fill but still unable to close application without error. This is one of the best solution for them.

This works same as killing process forcefully from Task Manager. For ex. If some application is giving problem then we need to open task manager and terminating it forcefully.

If you have any problem or you want my help then you can e-mail me on cha1202001@yahoo.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

chaitanya shah
Technical Lead Tata Consultancy Servcies
India India
I have experience in iOs, Objective C,Java,C++/Vc++ 6.0,vc.Net,MFC,ATL,COM,WTL.
 
You can contact me on chaitanya.ce@gmail.com if you have any query.
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNeed to convert the WideChar (WCHAR) to a char * using the WideCharToMultiByte functionmemberMember 849000514-Dec-11 17:34 
For those curious I had to use the following:
char appName[255];
WideCharToMultiByte(CP_ACP, 0,  procEntry.szExeFile, -1, appName, 255, NULL, NULL);

GeneralThanksmemberdennislx27-Feb-11 7:47 
I was looking for that, great job! Smile | :)
GeneralThat class is only defined in .NET not win32. This code is to be is in the old MFC libraries. (VB 6.0 not VS2005)sussAlex Valdes7-Dec-07 8:17 
Right????
GeneralRe: That class is only defined in .NET not win32. This code is to be is in the old MFC libraries. (VB 6.0 not VS2005)memberchaitanya shah21-May-08 0:33 
In this article I have given all different way of killing Application. This article also includes basic windows APIs.It also includes MFC one.
 
Revert back for any further quires.
GeneralThat class is only defined in .NET not win32. This code is to be is in the old MFC libraries. (VB 6.0 not VS2005)sussAlex Valdes7-Dec-07 8:17 
Right????
GeneralRe: That class is only defined in .NET not win32. This code is to be is in the old MFC libraries. (VB 6.0 not VS2005)memberchaitanya shah21-May-08 0:31 
In this artical I have given all diferent way of killing Application. This artical also includes basic windows APIs.It also includes MFC one.
QuestionWhy not use Process class?memberRobert Rohde27-May-07 23:19 
With the static methods GetProcessesByName, GetProcessesById and GetCurrentProcess together with the Kill method (and much more) it has a complete managed wrapper for everything one should need.
 

AnswerRe: Why not use Process class?memberWarren Keyes27-May-07 23:45 
I agree.
I've used the .NET Process class many times and they're great,you can also use them for basic profiling of memory usage etc
Also the CreateToolSnapshot32 and related functions are Win32 calls and are not part of the MFC libs
GeneralRe: Why not use Process class?memberchaitanya shah28-May-07 20:41 
Thank you for pointing out my mistake.I had written it by mistake I have changed It in artical form MFC to Win32.
AnswerRe: Why not use Process class?memberchaitanya shah28-May-07 20:38 
Yes, You are correct.I have added code using GetCurrentProcessID API for self killing application. But I have given code of enumerating process is for searching any process running in system which we want to kill it.It can be self or any other application.
GeneralRe: Why not use Process class?memberRobert Rohde29-May-07 7:08 
You are not getting me. The Process class has everything to enumerate all processes and kill them if desired. There is no need to do api calls.
 

GeneralRe: Why not use Process class?memberchaitanya shah30-May-07 21:11 
I think you are telling me why I have not used ::ExitProcess(0) API. This does not work with DLL. If we want to kill exe then It is also one of the good option.I think I had given your question answer.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 28 May 2007
Article Copyright 2007 by chaitanya shah
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid