Click here to Skip to main content
Licence 
First Posted 27 May 2007
Views 50,637
Downloads 1,351
Bookmarked 19 times

Kill Application Using Win32 APIs

By | 27 May 2007 | Article
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 .

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

Member

Follow on Twitter Follow on Twitter
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.

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNeed to convert the WideChar (WCHAR) to a char * using the WideCharToMultiByte function PinmemberMember 849000517:34 14 Dec '11  
GeneralThanks Pinmemberdennislx7:47 27 Feb '11  
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) PinsussAlex Valdes8:17 7 Dec '07  
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) Pinmemberchaitanya shah0:33 21 May '08  
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) PinsussAlex Valdes8:17 7 Dec '07  
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) Pinmemberchaitanya shah0:31 21 May '08  
QuestionWhy not use Process class? PinmemberRobert Rohde23:19 27 May '07  
AnswerRe: Why not use Process class? PinmemberWarren Keyes23:45 27 May '07  
GeneralRe: Why not use Process class? Pinmemberchaitanya shah20:41 28 May '07  
AnswerRe: Why not use Process class? Pinmemberchaitanya shah20:38 28 May '07  
GeneralRe: Why not use Process class? PinmemberRobert Rohde7:08 29 May '07  
GeneralRe: Why not use Process class? Pinmemberchaitanya shah21:11 30 May '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 28 May 2007
Article Copyright 2007 by chaitanya shah
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid