有没有更好的方法在 C++/CLI 中重定向 stdout 然后使用 system() ?

发布于 2024-12-01 17:41:05 字数 959 浏览 1 评论 0原文

在我正在处理的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

緦唸λ蓇 2024-12-08 17:41:05

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.

淡淡绿茶香 2024-12-08 17:41:05

查看这篇文章。值得注意的是:

/* Setup Redirect */
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;

/* Capture Output */
StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();

"1>\\root\\logs\\process_STDOUT.txt" 作为参数传递与重定向不同

Check out this article. Noteworthy:

/* Setup Redirect */
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;

/* Capture Output */
StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();

Passing "1>\\root\\logs\\process_STDOUT.txt" as an argument is not the same as redirecting

救赎№ 2024-12-08 17:41:05

“管道命令”不作为参数传递给程序; shell 解释它,并且程序本身不知道它的输出被重定向到文件。所以你不想将这样的东西作为参数传递。

但是,Program 类具有 StandardOutputStandardError 流属性来帮助您捕获输出。您只需指定(在您的 ProcessStartInfo 对象中)您想要重定向输出。

s->RedirectStandardOutput = true;
s->RedirectStandardError = true;

过了这一点,您将能够使用 p->StandardOutputp->; 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 has StandardOutput and StandardError stream properties to help you capture output. You simply need to specify (in your ProcessStartInfo object) that you want to redirect output.

s->RedirectStandardOutput = true;
s->RedirectStandardError = true;

Past that point, you'll be able to use p->StandardOutput and p->StandardError to get the output of the stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文