将输入传递给 CreateProcess() 创建的进程
就像标题中一样,我想知道是否可以在 CreateProcess() 中使用重定向运算符来传递输入。我尝试了类似以下内容:
CreateProcess(NULL, "%ComSpec% /cc:\\somebatch.bat", NULL, NULL, ...);
其中 somebatch.bat 包含
c:\app.exe < c:\input.txt
并且它没有传递输入,只是启动了app.exe
。输出显示:
c:\working_directory> c:\app.exe < c:\input.txt
c:\working_directory>没有足够的存储空间来处理此命令。
(弄乱 irpcstack 没有帮助)
你们知道有什么神奇的技巧吗?可以让我做我想做的事情,而不会弄乱 hStdInput 管道,坦白说我想避免。干杯。
like in the title I was wondering whether is it possible to pass an input by use of redirection operators in CreateProcess(). I tried something like the following:
CreateProcess(NULL, "%ComSpec% /c c:\\somebatch.bat", NULL, NULL, ...);
where somebatch.bat
contained c:\app.exe < c:\input.txt
and it didn't pass the input, just launched the app.exe
. On the output it said that:
c:\working_directory> c:\app.exe < c:\input.txt
c:\working_directory>Not enough storage is available to process this command.
(messing with irpcstack didn't help)
Are you guys aware of any magic trick that would allow me to do what I want without messing with the hStdInput
pipe, which saying frankly I wanted to avoid. Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经有一段时间没有这样做了,但是您必须在
STARTUPINFO
struct 作为 CreateProcess。我很确定没有其他好方法可以做到这一点。使用 hStdInput 非常简单,打开要用作输入的文件,将 hStdInput 设置为文件句柄,创建进程,然后关闭句柄。您也许可以打开输入文件,将句柄复制到当前进程的标准输入中,然后创建将
bInheritHandles
设置为TRUE
的进程。然后您的程序将通过stdin
简单地接收文件内容。我从未在 Windows 中尝试过此操作,但在基于 UNIX 的平台上这是常见做法。至于运行批处理文件,请阅读
CreateProcess
的 MSDN 条目。我认为你的论点搞砸了。It has been a while since I have done this, but you have to set
hStdInput
member in theSTARTUPINFO
struct passed in as the next to last argument of CreateProcess. I'm pretty sure that there is no other good way to do this. UsinghStdInput
is pretty easy, open the file that you want to use as input, sethStdInput
to the file handle, create the process, and close the handle.You might be able to open the input file, duplicate the handle into the current processes standard input, and then create the process with
bInheritHandles
set toTRUE
. Then your program will simply receive the file contents viastdin
. I've never tried this in Windows, but it is common practice on UNIX based platforms.As for running a batch file, read the comments in the MSDN entry for
CreateProcess
. I think that you have the arguments messed up.您有什么理由需要
CreateProcess
吗?system
函数使用默认 shell(因此您不需要输入%ComSpec%
),这意味着重定向可以正常工作。它还更易于使用且更便携。如果您只是要等待该过程完成,请考虑使用system
。Is there any reason you need
CreateProcess
?The
system
function uses the default shell (so you wouldn't need to put in%ComSpec%
) which means that redirection would work fine. It's also easier to use and more portable. If you're just going to wait for the process to finish, consider usingsystem
instead.