Windows – Windows CMD equivalent of Unix shell’s exec

shellwindowswindows-command-prompt

Is there any equivalent in Windows to Unix Shell's "exec"? Basically, I need to avoid forking a new process, so that input/output pipes are preserved, as well as process id.

Edit:

So, here's my problem. I have a process A that starts a script, and this script ends by executing a process B. I need for A to get all of B's output, as well as be able to kill B by killing the process it has spawned (the script).

On Unix, executing B with exec does that job.

Best Answer

AFAIK there isn't. Windows lacks execv() which is how bash exec does it. call works for calling batch files (easy, just interpret the file in the current interpreter, similar to bash's . command) but not for exes.

This makes it impossible to write one-liner wrapper scripts for scripts in any language on Windows. You'll always get that "Terminate batch job?" crap on Ctrl+C and killing the batch process (not with Ctrl+C, from task manager say) won't kill the child process. I'm now looking for a C template file to do this wrapping.

UPDATE: Windows does have _execv() in its POSIX compatibility layer in MSVCRT, but AFAIK (haven't tested it) it's just a wrapper around CreateProcess so it will always create a new process, it cannot replace the current process.