0

以下代码可以很好地显示在不同用户下运行的所有进程(例如: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);
}

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

4

1 回答 1

0

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

于 2013-09-04T04:25:17.730 回答