运行一个简单的 shell 命令 [英] Run a simple shell command
问题描述
当您只想运行像 hg > 这样的简单 shell 命令时,最好使用的 WinAPI 函数是什么?test.txt
?
推荐答案
要简单地运行一个文件,然后 ShellExecute()
和 CreateProcess()
是最好的选择.
To simply run a file, then ShellExecute()
and CreateProcess()
are the best options.
当你想将输出重定向到一个文件/运行一个 shell 命令时,它会使事情复杂化......
As you want to redirect output to a file/run a shell command, it complicates things...
输出重定向是命令提示符的一个功能,因此,您要运行的命令需要传递给 cmd.exe
(在 NT/XP+ 上)通过 /c
和您的命令作为参数(ShellExecute
或 CreateProcess
都可以).
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"
然而,最好的方法是使用 CreateProcess()
并创建自己的管道来与程序的 stdin 和 stdout 对话(这是 cmd
在内部所做的全部)
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)
这篇关于运行一个简单的 shell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!