有没有更好的方法在 C++/CLI 中重定向 stdout 然后使用 system() ?
在我正在处理的 C++/CLI 代码的 2 部分中,程序需要运行不同的可执行文件并将其 STDOUT 输出重定向到文件。它以两种不同的方式进行尝试,目前这两种方式中只有一种有效。该程序正在Windows XP 上运行。
第一部分组装一个长 char*,最终看起来像这样:
char* exepath = "start/B /D\\root\\bin \\root\\bin\\process.exe 1>\\root\\logs\\process_STDOUT.txt"
然后代码简单地调用
Status = system(exepath);
这工作正常: 它既运行 process.exe 又按预期创建 process_STDOUT.txt 文件。
第二部分尝试使用 ProcessStartInfo 对象来执行相同的操作。它成功启动 process.exe,但尚未创建重定向输出 .txt 文件。这是该代码的简化版本:
Process p = gcnew Process();
ProcessStartInfo^ s = gcnew ProcessStartInfo();
s->FileName = "\\root\\bin\\process.exe";
s->WindowStyle = ProcessWindowStyle::Hidden;
s->CreateNoWindow = true;
s->UseShellExecute = false;
s->Arguments = "1>\\root\\logs\\process_STDOUT.txt";
p->StartInfo = s;
p->Start();
我用这段代码做错了什么吗?如果没有,是回到调用 system(exepath) 我唯一的选择还是还有其他选择?
In 2 parts of the C++/CLI code I'm working on, the program needs to run a different executable and have its STDOUT output redirected to a file. Its being attempted in 2 different ways, and only one of the 2 currently works. The program is being run on Windows XP.
The first section assembles a long char* that ends up looking something like this:
char* exepath = "start/B /D\\root\\bin \\root\\bin\\process.exe 1>\\root\\logs\\process_STDOUT.txt"
Then the code simply calls
Status = system(exepath);
This works fine: It both runs process.exe and creates the process_STDOUT.txt file as expected.
The second section tries to do the same using a ProcessStartInfo object instead. It successfully starts process.exe, but has not been creating the redirected output .txt file. Here is a simplified version of that code:
Process p = gcnew Process();
ProcessStartInfo^ s = gcnew ProcessStartInfo();
s->FileName = "\\root\\bin\\process.exe";
s->WindowStyle = ProcessWindowStyle::Hidden;
s->CreateNoWindow = true;
s->UseShellExecute = false;
s->Arguments = "1>\\root\\logs\\process_STDOUT.txt";
p->StartInfo = s;
p->Start();
Am I doing something wrong with this code? And if not, is going back to just calling system(exepath) my only option or are there any others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
system
函数调用系统的命令行解释器,在 Windows 上通常是 cmd.exe。 这是解析 I/O 重定向语法的程序。您正在启动的程序不知道>
的含义;它将其解释为实际命令行参数的第一个字符。 (想一想 - 您曾经在您的命令行程序中编写过命令行重定向代码吗?不,当然没有。的作者也没有process.exe。)这就是您的
ProcessStartInfo
方法不重定向 I/O 的原因。为此,请参阅此处有关在 .Net 中重定向 stdin 和 stdout 的问题。由于无论如何您都将输出重定向到文件,因此您当前的
系统
代码应该没问题。当您想要收集程序中的输出而不是文件时,需要采用更具编程性的重定向方式。The
system
function invokes the system's command-line interpretter, which on Windows is usually cmd.exe. That's the program that parses the I/O-redirection syntax. The program you're starting has no idea what>
means; it interprets that as the first character of an actual command-line argument. (Think about it — have you ever written command-line-redirection code in your command-line programs? No, of course not. Neither did the author of process.exe.)So that's why your
ProcessStartInfo
method doesn't redirect I/O. For that, see the question here about redirecting stdin and stdout in .Net.Since you're redirecting output to a file anyway, your current
system
code should be fine. The more programmatic ways of redirecting are necessary when you want to collect the output within your program instead of a file.查看这篇文章。值得注意的是:
将
"1>\\root\\logs\\process_STDOUT.txt"
作为参数传递与重定向不同Check out this article. Noteworthy:
Passing
"1>\\root\\logs\\process_STDOUT.txt"
as an argument is not the same as redirecting“管道命令”不作为参数传递给程序; shell 解释它,并且程序本身不知道它的输出被重定向到文件。所以你不想将这样的东西作为参数传递。
但是,
Program
类具有StandardOutput
和StandardError
流属性来帮助您捕获输出。您只需指定(在您的 ProcessStartInfo 对象中)您想要重定向输出。过了这一点,您将能够使用
p->StandardOutput
和p->; StandardError
来获取流的输出。The "pipe command" is not passed as a parameter to the program; the shell interprets it and the program itself has no idea that its output is being redirected to a file. So you don't want to pass stuff like that as an argument.
However, the
Program
class hasStandardOutput
andStandardError
stream properties to help you capture output. You simply need to specify (in yourProcessStartInfo
object) that you want to redirect output.Past that point, you'll be able to use
p->StandardOutput
andp->StandardError
to get the output of the stream.