How to kill a process by it’s PID on Windows

I don’t want to sound mean but, God, MSDN API sucks =P. Not because it doesn’t tell you the stuff you need to know but because it requires alot of imagination from you to understand it.

For example, I was looking for a way to kill a process using it’s process id ( PID) however it turn that when you are programming for WIN32 you need to use HANDLE’s for everything so if you’d like to have a process that creates process and then kill them you’d have to store and manage every handle.

Now I don’t lik handling Handle and pointers I like to keep it simple using integers, an array of integers to me is way easier to use than an arrat of handles since pasing pointers and casting and etc.. is not my passion on C++. Thus I found a way to kill processes by their PID using this nice simple function:

void kill_by_pid(int pid)
{
	HANDLE handy;
	handy =OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, TRUE,pid);
	TerminateProcess(handy,0);
}

Hope this works for you

Cheers

One thought on “How to kill a process by it’s PID on Windows

Leave a Reply

Your email address will not be published. Required fields are marked *