pico
pico

Reputation: 1910

Need Win32 API C++ code that is equivalent to "taskkill /T /F /PID XXX"

The following code works to "kill -9" my process (see below). But I prefer to not shell out to cmd.exe just to kill a process from my C++ program. Is there a pure Win32 API way of doing this, specifically, with the effects of the "/T and /F" flags. I got to believe its possible since taskkill.exe was writtend in Win32 API.

    #include <iostream>
    #include <string>

    void KillDash9(unsigned long PID) {
        std::string killcmd = std::string("") 
                            + std::string("taskkill /T /F /PID ") 
                            + std::to_string(PID);
        std::cout << killcmd << std::endl;
        system(killcmd.c_str());
    }

Here's what I tried that didn't work:

void KillDash9_v2(unsigned long PID) {
    HANDLE pHandle = OpenProcess(
    /*[in] DWORD dwDesiredAccess*/ PROCESS_TERMINATE | SYNCHRONIZE,
    /*[in] BOOL  bInheritHandle*/  TRUE,
    /*[in] DWORD dwProcessId*/     PID
    );
    TerminateProcess(pHandle, 0);
    WaitForSingleObject(pHandle, 500);
    CloseHandle(pHandle);
}
 

KillDash9_v2() won't kill the process:

"powershell -command ping -t localhost"  

enter image description here

However, KillDash9_v1() works to kill this process.

How to get KillDash9_v2() to work the same way? v2 will kill powershell (3328) but it won't kill ping.exe (7236) which keeps running after killing 7236. I need to somehow enumerate the process tree starting 7246 and kill each branch under it.

I added in error checking as suggested.

void KillDash9_v3(unsigned long PID) {

    // Clear Windows Error Variable
    SetLastError(0);

    HANDLE pHandle = OpenProcess(
    /*[in] DWORD dwDesiredAccess*/ PROCESS_TERMINATE | SYNCHRONIZE,
    /*[in] BOOL  bInheritHandle*/  FALSE,
    /*[in] DWORD dwProcessId*/     PID
    );
    if (pHandle == nullptr) {
        long int err = GetLastError();
        std::string msg = std::string("ERROR  : OpenProcess failed for PID(") + std::to_string(PID) + std::string(")\n")
                        + std::string("REASON : ") + WindowsGetErrorString(err) + std::string("\n")
                        + std::string("CODE   : ") + std::to_string(err) + std::string("\n");
        std::cout << msg;
        return;
    }

    // Clear Windows Error Variable
    SetLastError(0);

    BOOL rc = TerminateProcess(pHandle, 0);
    if (rc == 0) {
        long int err = GetLastError();
        std::string msg = std::string("ERROR:  TerminateProcess failed for PID(") + std::to_string(PID) + std::string(")\n")
                   + std::string("REASON: ") + WindowsGetErrorString(err) + std::string("\n")
                   + std::string("CODE   : ") + std::to_string(err) + std::string("\n");
        std::cout << msg;
        return;
    }

    WaitForSingleObject(pHandle, 500);
    CloseHandle(pHandle);
}

Upvotes: 1

Views: 651

Answers (1)

pico
pico

Reputation: 1910

void ProcessKillTree_v1(unsigned long PARENT_PID)
{
    std::string killcmd = std::string("") 
          + std::string("taskkill /T /F /PID ") 
          + std::to_string(PARENT_PID);
    std::cout << killcmd << std::endl;
    system(killcmd.c_str());
}
void ProcessKillTree_v2(unsigned long PARENT_PID)
{
    if(PARENT_PID > 0) {

        // Kill Tree
        HANDLE snapshot = CreateToolhelp32Snapshot(
            TH32CS_SNAPPROCESS, 0);

        if(snapshot) {
            PROCESSENTRY32 process;
            ZeroMemory(&process, sizeof(process));
            process.dwSize = sizeof(process);

            if(Process32First(snapshot, &process)) {
                do {
                    if(process.th32ParentProcessID==PARENT_PID) {
                        HANDLE process_handle 
                             = OpenProcess(
                                  PROCESS_TERMINATE, 
                                  FALSE, 
                                  process.th32ProcessID
                             );

                        if(process_handle) {
                            ProcessKillTree(process.th32ProcessID);
                            //TerminateProcess(process_handle, 2);
                            //CloseHandle(process_handle);
                        }
                    }
                }
                while (Process32Next(snapshot, &process));
            }
            CloseHandle(snapshot);
        }

        // Kill Parent
        HANDLE parent_handle = OpenProcess(
                                  PROCESS_TERMINATE, 
                                  FALSE, 
                                  PARENT_PID);
        TerminateProcess(parent_handle, 2);
        CloseHandle(parent_handle);
    }
}

Upvotes: 2

Related Questions