Click here to Skip to main content
13,453,951 members (44,912 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 27 May 2007

Kill Application Using Win32 APIs

, 27 May 2007
Rate this:
Please Sign up or sign in to vote.
It kills application. Programmer need to specify program EXE name, then this application will enumerate that application from current running process and retrieving ThreadID and Process Handle using this information application will terminate that application .


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 );
       AfxMessageBox("Application found");
   //cout<<"Running Process "<<"          "<<procEntry.szExeFile<<endl;

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);


* * * * * * * * * * * * * * 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());

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


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 if you have any query.

You may also be interested in...


Comments and Discussions

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) Pin
Alex Valdes7-Dec-07 8:17
sussAlex Valdes7-Dec-07 8:17 
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) Pin
chaitanya shah21-May-08 0:31
memberchaitanya shah21-May-08 0:31 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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