Knowledge Base Nr: 00095 proclist.cpp - http://www.swe-kaiser.de

Downloads:

Win32: laufende Prozesse auflisten und beenden

  
#include <tlhelp32.h>

void KillProcess(const char* lpszProcName)
{
HANDLE ss = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

PROCESSENTRY32 pe;
pe.dwSize = sizeof(pe);

BOOL bSucc = ::Process32First(ss, &pe);
while (bSucc)
{
bSucc = ::Process32Next(ss, &pe);
if (bSucc)
{
printf("%04d\t%s", pe.th32ProcessID, pe.szExeFile);
if (stricmp(lpszProcName, pe.szExeFile) == 0)
{
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, pe.th32ProcessID);
bSucc = ::TerminateProcess(hProcess, 0);
}
}
}

::CloseHandle(ss);
}