Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Kill Application Using Win32 APIs

2.39/5 (8 votes)
27 May 20072 min read 1   3.5K  
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