How to kill processes by name? (Win32 API)

后端 未结 3 921
梦毁少年i
梦毁少年i 2020-11-30 06:36

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 k

相关标签:
3条回答
  • 2020-11-30 07:07

    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);    
       }
    }
    
    0 讨论(0)
  • 2020-11-30 07:14

    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.

    0 讨论(0)
  • 2020-11-30 07:15
    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.

    0 讨论(0)
提交回复
热议问题