Run a simple shell command

前端 未结 3 541
星月不相逢
星月不相逢 2020-12-18 05:00

What is the best WinAPI function to use when you only want to run a simple shell command like hg > test.txt?

3条回答
  •  暖寄归人
    2020-12-18 05:29

    To simply run a file, then ShellExecute() and CreateProcess() are the best options.

    As you want to redirect output to a file/run a shell command, it complicates things...

    Output redirection is a feature of the command prompt, and as such, the command you want to run needs to be passed to cmd.exe (on NT/XP+) passing /c and your command as the parameters (either ShellExecute or CreateProcess will do).

    cmd /c "ipconfig >c:\debug\blah.txt"
    

    The best way however is to use CreateProcess() and create your own pipes to talk to the stdin and stdout of the program (This is all cmd does internally)

提交回复
热议问题