Skip to content

Instantly share code, notes, and snippets.

@baiyanhuang
Created April 5, 2011 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baiyanhuang/902868 to your computer and use it in GitHub Desktop.
Save baiyanhuang/902868 to your computer and use it in GitHub Desktop.
A program to terminate all processes of same name in Windows
#include <windows.h>
#include <Tlhelp32.h>
int _tmain(int argc, const TCHAR* argv[])
{
if(argc != 2) return -1;
TCHAR* processName = argv[1];
PROCESSENTRY32 ppe = {0};
ppe.dwSize = sizeof (PROCESSENTRY32);
HANDLE hSnapShot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
if (Process32First (hSnapShot, &ppe))
{
do
{
// Process name match (and it is not current process)
if (!_tcsicmp (processName, ppe.szExeFile) && (ppe.th32ProcessID != GetCurrentProcessId ()))
{
HANDLE hMySecondProcess = OpenProcess (PROCESS_TERMINATE, FALSE, ppe.th32ProcessID);
if (hMySecondProcess)
{
TerminateProcess (hMySecondProcess, 0);
}
CloseHandle (hMySecondProcess);
}
}while (Process32Next (hSnapShot, &ppe)); }
CloseHandle (hSnapShot);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment