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.
HANDLE hndl = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
DWORD dwsma = GetLastError();
DWORD dwExitCode = 0;
procEntry.dwSize = sizeof( PROCESSENTRY32 );
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.
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.
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 email@example.com.