dikidera
dikidera

Reputation: 2043

How to kill processes by name? (Win32 API)

Basically, I have a program which will be launched more than once. So, there will be two or more processes launched of the program.

I want to use the Win32 API and kill/terminate all the processes with a specific name.

I have seen examples of killing A process, but not multiple processes with the exact same name(but different parameters).

Upvotes: 35

Views: 66152

Answers (3)

Trey
Trey

Reputation: 1

void kill(std::string filename, int delay)
{
    filename += ".exe";
    HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
    PROCESSENTRY32 pEntry;
    pEntry.dwSize = sizeof(pEntry);
    BOOL hRes = Process32First(hSnapShot, &pEntry);
    while (hRes) {
        if (filename.c_str() == pEntry.szExeFile) {
            HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, (DWORD)pEntry.th32ProcessID);
            if (hProcess != NULL) {
                TerminateProcess(hProcess, 9);
                CloseHandle(hProcess);
            }
        }
        hRes = Process32Next(hSnapShot, &pEntry);
    }
    CloseHandle(hSnapShot);
}

// usage
int main()
{
    kill("notepad");
}

I know this is old but i feel as if i should explain some of the issues and bad practice with the 2011 anwer. There is absolutely no reason for you to be writing c in c++ unless you need to. The use of const char array is unnecessary as std::string::c_str() already returns a pointer to the string. As you can see in my snippet...

    - filename is no longer a const char, instead its a string because its native c++ and good practice
    - strcmp check is removed as there is no reason to compare string differences. Instead we check if they're equivalent
    - We append ".exe" to filename so you can type the process name without the .exe
There is simply no reason to write c in c++ unless its mandatory.

Upvotes: -2

Jeremy Whitcher
Jeremy Whitcher

Reputation: 721

I just ran into a similar problem. Here's what I came up with...

void myClass::killProcess()
{
   const int maxProcIds = 1024;
   DWORD procList[maxProcIds];
   DWORD procCount;
   char* exeName = "ExeName.exe";
   char processName[MAX_PATH];

   // get the process by name
   if (!EnumProcesses(procList, sizeof(procList), &procCount))
      return;

   // convert from bytes to processes
   procCount = procCount / sizeof(DWORD);

   // loop through all processes
   for (DWORD procIdx=0; procIdx<procCount; procIdx++)
   {
      // get a handle to the process
      HANDLE procHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procList[procIdx]);
      // get the process name
      GetProcessImageFileName(procHandle, processName, sizeof(processName));
      // terminate all pocesses that contain the name
      if (strstr(processName, exeName))
         TerminateProcess(procHandle, 0);
      CloseHandle(procHandle);    
   }
}

Upvotes: 2

masoud
masoud

Reputation: 56509

Try below code, killProcessByName() will kill any process with name filename :

#include <windows.h>
#include <process.h>
#include <Tlhelp32.h>
#include <winbase.h>
#include <string.h>
void killProcessByName(const char *filename)
{
    HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
    PROCESSENTRY32 pEntry;
    pEntry.dwSize = sizeof (pEntry);
    BOOL hRes = Process32First(hSnapShot, &pEntry);
    while (hRes)
    {
        if (strcmp(pEntry.szExeFile, filename) == 0)
        {
            HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
                                          (DWORD) pEntry.th32ProcessID);
            if (hProcess != NULL)
            {
                TerminateProcess(hProcess, 9);
                CloseHandle(hProcess);
            }
        }
        hRes = Process32Next(hSnapShot, &pEntry);
    }
    CloseHandle(hSnapShot);
}
int main()
{
    killProcessByName("notepad++.exe");
    return 0;
}

Note: The code is case sensitive to filename, you can edit it for case insensitive.

Upvotes: 63

Related Questions