问题描述
我尝试使用 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 将输出重定向到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)