Search code examples
c++visual-c++spawn

Kill a process created with _spawnvp()


The Windows version of a program starts a process with

char * argv[..];
intptr_t childHandle = _spawnvp( _P_NOWAIT, "executable.exe", argv );

which works. The documentation says: "The return value from an asynchronous _spawnvp (_P_NOWAIT specified for mode) is the process handle." and thus I assume there should be also a kill command that takes this handle. How can I kill a process when I have an intptr_t?


Solution

  • _spawnvp returns a process handle, if you use it with the _P_NOWAIT argument. Using the Win32 API you can terminate the process:

    UINT exitCode = 0;
    intptr_t handle = _spawnvp(_P_NOWAIT, "executable.exe", argv );
    if(TerminateProcess((HANDLE) handle, exitCode))
    {
        // successful termination
    }