如何使用 CreateProcess 将输出重定向到文件?

How do I redirect output to a file with CreateProcess?(如何使用 CreateProcess 将输出重定向到文件?)
本文介绍了如何使用 CreateProcess 将输出重定向到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我尝试使用 CreateProcess 来运行一个简单的命令,例如 hg >测试.txt.我尝试将字符串作为一个整体运行(而不是将其分成应用程序名称及其参数).为什么 CreateProcess(0, "notepad.exe test.txt", ...) 工作但 CreateProcess(0, "hg > test.txt", ...) 没有?

I tried using CreateProcess to run a simple command like hg > test.txt. I tried running the string as a whole (as opposed to separating it into an application name and its parameters). Why does CreateProcess(0, "notepad.exe test.txt", ...) work but CreateProcess(0, "hg > test.txt", ...) does not?

推荐答案

不能在传递给 CreateProcess.要重定向标准输出,您需要在 STARTUPINFO 结构.

You can't use stdout redirection in the command line passed to CreateProcess. To redirect stdout you need to specify a file handle for the output in the STARTUPINFO structure.

您还犯了另一个更微妙的错误.第二个参数 lpCommandLine 必须指向可写内存,因为 CreateProcess 会覆盖缓冲区.如果您碰巧使用了该函数的 ANSI 版本,那么您可以避免使用该函数,但不适用于 Unicode 版本.

You are also making another, more subtle, mistake. The second parameter, lpCommandLine must point to writeable memory because CreateProcess overwrites the buffer. If you happen to be using the ANSI version of the function then you will get away with this, but not for the Unicode version.

这个函数的 Unicode 版本,CreateProcessW,可以修改这个字符串的内容.因此,该参数不能是指向只读内存的指针(例如const 变量或文字字符串).如果此参数是一个常量字符串,该函数可能会导致访问冲突.

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

这篇关于如何使用 CreateProcess 将输出重定向到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
STL BigInt class implementation(STL BigInt 类实现)
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)