我如何输出重定向到与CreateProcess的文件? [英] How do I redirect output to a file with CreateProcess?

查看:380
本文介绍了我如何输出重定向到与CreateProcess的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着使用的CreateProcess来运行一个简单的命令如汞柱> test.txt的。我试图运行串作为一个整体(相对于它分离成一个应用程序的名称和其参数)。为什么的CreateProcess(0,NOTEPAD.EXE的test.txt,...)工作,但的CreateProcess(0,汞>的考验。 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?

推荐答案

您无法使用标准输出重定向传入的命令行<一个href=\"http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx\"><$c$c>CreateProcess.要重定向你需要标准输出指定一个文件句柄在<一个输出href=\"http://msdn.microsoft.com/en-us/library/ms686331%28v=VS.85%29.aspx\"><$c$c>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版本,那么你会逃脱这一点,但不是为统一code版本。

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.

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

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的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆