English 中文(简体)
如何在C++(Win32)有效地杀害一个进程?
原标题:How to effectively kill a process in C++ (Win32)?

我目前正在撰写一个非常轻重的方案,因此我不得不使用C++,因为它不受约束。 NET框架大大提高了该方案的规模。

www.un.org/Depts/DGACM/index_spanish.htm 我需要能够结束进程,并完成这项工作。 令人不舒服的是,我没有说明如何做到这一点。

P.S.,我知道,要杀你必须使用TerminateProcess的过程。

最佳回答

你们需要的开放程序通常不容易获得搁置。 如果你们都说是个过程名称,那么你就需要振兴机器上的运行程序。 采用制造Toolhelp32Snapshot,然后是第332I号程序,然后是第332Next。 附录 ExeFile给你带来了工艺名称(不是途径!), th32Process ID给您。

下一个考虑是,这一过程可能不止一次。 还有一种机会是,同样的程序名称被用于非常不同的方案。 和“Setup”一样。 如果你不想杀死他们,那么你就不得不试图从他们那里获得一些时间的胎儿。 可能的话,温得力限制案文。 让我们能够走到顶端。 它使用本地的舱面格式,你需要QueryDosDevice,把磁盘驱动器的名称用在驾驶纸上。

下一次审议是您在公开程序中要求的权利。 页: 1 PROCESS_TERMINATE。 虽然这同样是特权。 确保你用来管理你的节目的账户能够取得这一权利。

问题回答

下述法典:

const auto explorer = OpenProcess(PROCESS_TERMINATE, false, process_id);
TerminateProcess(explorer, 1);
CloseHandle(explorer);

不是通过所有这些痛苦来杀害一个有名人名的进程,为什么不简单地叫“系统”并要求指挥线杀死它?

例如,

int retval = ::_tsystem( _T("taskkill /F /T /IM MyProcess.exe") );

http://msdn.microsoft.com/en-us/library/ms686714(VS.85).aspx“rel=“noreferer”>TerminateProcess,使用,加上诸如

这里是“2010年C++”演播室“视觉”项目“如何在<<>EXE文档名称”前杀死这一过程的全例。

为了检查它刚刚运行的因特网探索者,并在执行后遵循密码。

#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;
}

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();
                }
            }
        }
    }
}

窗户只有

system("taskkill /f /im servicetokill.exe")

这里有一些工作样本代码,用以杀一个叫做“ShouldBeDead.exe”的过程:

// you will need these headers, and you also need to link to Psapi.lib
#include <tchar.h>
#include <psapi.h>

...
// first get all the process so that we can get the process id 
DWORD processes[1024], count;
if( !EnumProcesses( processes, sizeof(processes), &count ) )
{
    return false;
}

count /= sizeof(DWORD);
for(unsigned int i = 0; i < count; i++)
{
    TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
    if(processes[i] != 0)
    {
        // remember to open with PROCESS_ALL_ACCESS, otherwise you will not be able to kill it
        HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, processes[i] );
        if(NULL != hProcess)
        {
            HMODULE hMod;
            DWORD cbNeeded;
            if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
            {
                GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR));

                // find the process and kill it
                if(strcmp(szProcessName, "ShouldBeDead.exe") == 0)
                {
                    DWORD result = WAIT_OBJECT_0;
                    while(result == WAIT_OBJECT_0)
                    {
                        // use WaitForSingleObject to make sure it s dead
                        result = WaitForSingleObject(hProcess, 100);
                        TerminateProcess(hProcess, 0);
                    }

                    CloseHandle(hProcess);
                }
            }
        }
    }
}

http://msdn.microsoft.com/en-us/library/ms68242528VS.85%29.aspx” rel=“nofollow noreferer”>CreateProcess<>>>>>。

这里有sample代码,要求系统列出所有流程,然后寻找你想要的加工清单,从而找到一个过程。

Task Killer using Modern C++

下面是为我的“任期方案”创建的编号一。

//_____________________________________________
//                                             |
// TheNexGen of Terminator (inclusion version) |
// ------------------------------------------- |
//                                             |
// Add your Programs in the  if  check as I ve |
// listed below, and compile using c++17 flag  |
// or higher                                   |
//_____________________________________________|



#include <process.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string_view>
using namespace std;

int main()
{
    HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE) return 0;

    PROCESSENTRY32W pe32{ .dwSize = sizeof(PROCESSENTRY32) };
    if (!Process32First(hProcessSnap, &pe32)) return CloseHandle(hProcessSnap), 0;

    do
    {
        wstring_view str = pe32.szExeFile;

        if
            (
                str == L"chrome.exe"
                || str == L"AAM Update Notifier.exe"
                || str == L"About.exe"
                || str == L"ActionCenterDownloader.exe"
                || str == L"adb.exe"
                || str == L"AdobeARM.exe"
            )
        {
            if (HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, pe32.th32ProcessID))
            {
                TerminateProcess(hProcess, 1);
                CloseHandle(hProcess);
            }
        }
    }
    while (Process32Next(hProcessSnap, &pe32));

    CloseHandle(hProcessSnap);
}

Description

增加《刑法》100x条的执行速度,@DmitryBoyko





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签