【问题标题】:How to kill Processes running under different users using c++ code如何使用 C++ 代码杀死在不同用户下运行的进程
【发布时间】:2013-09-07 11:02:39
【问题描述】:

以下代码可以很好地显示在不同用户下运行的所有进程(例如:notepad.exe)的进程 ID。但是当前用户下的进程独自被杀死。我需要杀死在不同用户下运行的所有进程。

#define SAMPLEAPP "notepad.exe"
void main()
{
    KillProcessByName(SAMPLEAPP);
    system("pause");
}
void KillProcessByName(const char *filename)
{
    // Taking snapshot of all processes
    HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
    //structure to capture each entry in snapshot
    PROCESSENTRY32 pEntry;
    pEntry.dwSize = sizeof (pEntry);
    //capture the first process in the list
    BOOL hRes = Process32First(hSnapShot, &pEntry);
    while (hRes)
    {
        char tempProcess[PROCESS_SIZE];// = pEntry.szExeFile;
        wcstombs(tempProcess, pEntry.szExeFile, PROCESS_SIZE);
        //if process name is equal to the process passed as argument to be killed
        if (strcmp(tempProcess, filename) == 0)
        {
            HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
                (DWORD) pEntry.th32ProcessID);
            std::cout << "Process ID of the Process " << tempProcess << " is : " << pEntry.th32ProcessID;
            if (hProcess != NULL)
            {
                // Kill the process 
                TerminateProcess(hProcess, 9);
                CloseHandle(hProcess);
            }
        }
        //Capture the next process in process snapshot
        hRes = Process32Next(hSnapShot, &pEntry);
    }
    CloseHandle(hSnapShot);
}

如何杀死一个进程,即使它属于另一个用户?

【问题讨论】:

    标签: c++ kill-process


    【解决方案1】:

    右键单击您的程序,然后选择“以管理员身份运行”。

    【讨论】:

    • 我也试过“以管理员身份运行”。即使这样,其他用户下的进程也不会被杀死。
    • 您使用的用户帐户可以使用任务管理器终止这些进程吗?
    • 是的。在任务管理器中,我可以使用“结束进程”选项结束其他用户的进程。
    • TerminateProcess() 失败时GetLastError() 会返回什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多