标准输入的跨平台重定向和本地C衍生进程的输出/ C ++(编辑用溶液) [英] Cross-platform redirect of standard input and output of spawned process in native C/C++ (edit with solution)

查看:130
本文介绍了标准输入的跨平台重定向和本地C衍生进程的输出/ C ++(编辑用溶液)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串命令,我想异步执行,同时写入其输入和阅读它的输出。听起来很容易,对不对,魔鬼在跨平台。我针对这两个MSVC / Win32和海合会/ Linux和明明想编写特定于平台的code的最低金额。我的谷歌福失败了我,我得到了太多的噪音对我的疑问,所以我开始与我所知道的。

I have a string command I'd like to execute asynchronously while writing to its input and reading its output. Sounds easy, right, the devil is in the cross-platform. I'm targeting both MSVC/Win32 and gcc/Linux and obviously want to write the minimum amount of platform-specific code. My google-fu has failed me, I get too much noise for my queries, so I started with what I know.

的popen - 好和容易,返回FILE *,很容易消耗无处不在。但这里是 MSDN不得不说一下_popen

popen - nice and easy, returns FILE* that is easy to consume everywhere. But here's what MSDN have to say about _popen:

如果在Windows程序中所使用的,
  _popen函数返回会导致程序无效的文件指针
  停止响应无限期。
  _popen工作正常在一个控制台应用程序。要创建一个Windows
  应用重定向输入和
  输出,请参阅创建一个子进程
  与重定向的输入与输出
  平台SDK。

If used in a Windows program, the _popen function returns an invalid file pointer that causes the program to stop responding indefinitely. _popen works properly in a console application. To create a Windows application that redirects input and output, see Creating a Child Process with Redirected Input and Output in the Platform SDK.

等popen这是出了问题(编辑:因为我想我的code在GUI应用程序的工作)。 Windows的方式做到这一点在我看来是相当丑陋和详细。我能活与平台相关的产卵code,但我会想至少在I / O code是相同的。但在这里,我打了WinAPI的 HANDLE 和C. FILE * INT 文件描述符。有没有办法到 HANDLE 转换为 FILE * INT FD或反之亦然? (谷歌在这一次失败了我一次,我都试过了关键字的方式过度使用)

and so popen is out of the question (edit: because I'd like my code to work in GUI application). The Windows way to do it is in my opinion rather ugly and verbose. I could live with platform specific spawn code but I'd want at least the I/O code to be the same. Here, however, I hit a wall between the WinAPI HANDLEs and C FILE*, and int file descriptor. Is there a way to "convert" a HANDLE to FILE* or int fd or vice-versa? (Google failed me once more on this one, all the keywords I tried are way overused)

有没有更好的办法做到整个事情很少特定于平台的code?

Is there better way to do the whole thing with little platform-specific code?

外部库不是出了问题,但是维修的依赖是一种痛苦,尤其是等多个平台,所以我想,以减少依赖。我没有在升压找到这样的图书馆还。

External libraries are not out of the question, however dependency maintenance is a pain, especially so on multiple platforms so I'd like to reduce dependencies. I didn't find such library in boost also.

为了记录,到底是什么工作对我来说。在Windows / MSVC, CreatePipe() + 的CreateProcess()所概述的这里,使用 _open_osfhandle(),然后按 _fdopen()获得 FILE * 来的过程输入和输出。在Linux / GCC,什么新意,创造管道() S; 叉()然后 dup2()管道; 执行exec(); fdopen()上的相关文件描述符。这样一来,只有进程产卵code是依赖于平台(这是确定的,因为在Windows上,我想控制更多的 STARTUPINFO 参数),手写输入和阅读输出通过标准完成 FILE * 及相关功能。

Just for the record, what worked for me in the end. On Windows/MSVC, CreatePipe() + CreateProcess() as outlined here, using _open_osfhandle() followed by _fdopen() to get FILE* to the process input and output. On Linux/GCC, nothing new here, creating pipe()s; fork() then dup2() the pipes; exec(); fdopen() on the relevant file descriptors. That way, only the process spawning code is platform dependent (which is ok, as on Windows I'd like to control additional STARTUPINFO parameters), writing input and reading output is done through standard FILE* and related functions.

推荐答案

有关转换Windows HANDLE s到C文件的描述符使用 _open_osfhandle http://msdn.microsoft.com /en-us/library/bdts1c9x%28VS.71%29.aspx

for converting windows HANDLEs to C file descriptors use _open_osfhandle http://msdn.microsoft.com/en-us/library/bdts1c9x%28VS.71%29.aspx

编辑:这个例子曾经帮我用藏汉一个类似的问题:
http://www.halcyon.com/~ast/dload/guicon.htm

this example once helped me aswell with a similar problem: http://www.halcyon.com/~ast/dload/guicon.htm

这篇关于标准输入的跨平台重定向和本地C衍生进程的输出/ C ++(编辑用溶液)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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