如何有效地杀死C++(Win32)中的进程?

Kri*_*oks 15 c++ winapi process

我目前正在编写一个非常轻量级的程序,所以我必须使用C++,因为它不受.NET框架的约束,这大大增加了程序的大小.

我需要能够终止进程并且这样做我需要获得进程句柄.不幸的是,我还没有想到如何做到这一点.

PS我知道要杀死进程你必须使用TerminateProcess.

Han*_*ant 16

OpenProcess()所需的PID通常不容易掌握.如果你得到的只是一个进程名称,那么你需要在机器上迭代正在运行的进程.使用CreateToolhelp32Snapshot执行此操作,然后执行Process32First并使用Process32Next循环.PROCESSENTRY32.szExeFile为您提供进程名称(不是路径!),th32ProcessID为您提供PID.

下一个考虑因素是该过程可能不止一次出现.并且有可能将相同的进程名称用于非常不同的程序.像"设置".如果你不想只杀死它们,你需要尝试从它们获取一些运行时信息.也许是窗口标题栏文本.GetProcessImageFileName()可以为您提供.exe的路径.它使用本机内核格式,您需要QueryDosDevice将磁盘驱动器设备名称映射到驱动器号.

下一个考虑因素是您在OpenProcess()中要求的权限.你不太可能得到PROCESS_ALL_ACCESS,你所需要的只是PROCESS_TERMINATE.虽然这也是特权.确保用于运行程序的帐户可以获得该权限.


Jam*_*ard 14

为什么不简单地调用"系统"并要求命令行杀死它,而不是通过所有的痛苦来杀死具有已知名称的进程?

例如,

int retval = ::_tsystem( _T("taskkill /F /T /IM MyProcess.exe") );
Run Code Online (Sandbox Code Playgroud)

  • 简单而优雅?这不是真的. (11认同)
  • 是的,通常有效.但它涉及太多我的口味的假设:taskkill.exe在路径中,找到的taskkill.exe实际上是你想要的那个,命令行参数没有被更新版本的taskkill更改.exe,WMI服务正在运行. (2认同)

Kri*_*oks 8

HANDLE explorer;
explorer = OpenProcess(PROCESS_ALL_ACCESS,false,2120);
TerminateProcess(explorer,1);
Run Code Online (Sandbox Code Playgroud)

那很有效

  • 不要忘记`CloseHandle(explorer)` (7认同)

Dev*_*per 7

以下是 Visual Studio 2010 C++ 项目如何通过EXE文件名终止进程的完整示例。

为了检查它,只需运行 Internet Explorer,然后执行以下代码。

#include <iostream>
#include <string>
#include<tchar.h>
#include <process.h>
#include <windows.h>
#include <tlhelp32.h>

using namespace std;

//  Forward declarations:
BOOL GetProcessList();
BOOL TerminateMyProcess(DWORD dwProcessId, UINT uExitCode);

int main( void )
{
  GetProcessList( );
  return 0;
}

BOOL GetProcessList( )
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;

  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  if( hProcessSnap == INVALID_HANDLE_VALUE )
  {   
    return( FALSE );
  }

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )
  {   
    CloseHandle( hProcessSnap );  // clean the snapshot object
    return( FALSE );
  }

  // Now walk the snapshot of processes 
  do
  {  
    string str(pe32.szExeFile);

    if(str == "iexplore.exe") // put the name of your process you want to kill
    {
        TerminateMyProcess(pe32.th32ProcessID, 1);
    } 
  } while( Process32Next( hProcessSnap, &pe32 ) );

  CloseHandle( hProcessSnap );
  return( TRUE );
}

BOOL TerminateMyProcess(DWORD dwProcessId, UINT uExitCode)
{
    DWORD dwDesiredAccess = PROCESS_TERMINATE;
    BOOL  bInheritHandle  = FALSE;
    HANDLE hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
    if (hProcess == NULL)
        return FALSE;

    BOOL result = TerminateProcess(hProcess, uExitCode);

    CloseHandle(hProcess);

    return result;
}
Run Code Online (Sandbox Code Playgroud)

想象一下在 C# 中它看起来像

using System;
using System.Collections.Generic;
using System.Text;

namespace MyProcessKiller
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses())
            {
                if (myProc.ProcessName == "iexplore")
                {
                    myProc.Kill();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Jon*_*cto 5

为了获得一个句柄传递给了TerminateProcess,使用OpenProcess结合像其他一些功能EnumProcesses.