Click here to Skip to main content
15,896,269 members
Articles / Programming Languages / C#

Kill Application Using Win32 APIs

Rate me:
Please Sign up or sign in to vote.
2.39/5 (8 votes)
27 May 20072 min read 103.7K   3.5K   22  
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 .
// Kill_Process.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Kill_Process.h"
#include "Tlhelp32.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	///////////////////////////////////////////////////////////////
	// Finding Application which we have specified once that 
	// application finds it will give you message Application found
	// then first of all it opens that process and getting all rights 
	// of that application.Once we have all rights then we can kill 
	// that application with Terminate 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));
		
	///////////////////////////////////////////////////////////////	
		HANDLE hHandle;
		hHandle = ::OpenProcess(PROCESS_ALL_ACCESS,0,procEntry.th32ProcessID);
		
		::GetExitCodeProcess(hHandle,&dwExitCode);
		::TerminateProcess(hHandle,dwExitCode);

	return nRetCode;
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
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.

Comments and Discussions