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

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 .

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

Share

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

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) PinsussAlex 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) Pinmemberchaitanya shah21-May-08 0:33 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 28 May 2007
Article Copyright 2007 by chaitanya shah
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid