无法使用Boost.Process捕获进程的标准输出

Chr*_*s K 7 c++ windows boost io-redirection

目前我正在使用Boost沙盒中的Boost.Process,并且在使其正确捕获标准输出时遇到问题; 想知道是否有人可以给我第二副眼球进入我可能做错的事情.

我正在尝试使用DCRAW(最新版本)从RAW相机图像中取出缩略图,并捕获它们以转换为QT QImage.

流程启动功能:

namespace bf = ::boost::filesystem; 
namespace bp = ::boost::process;

QImage DCRawInterface::convertRawImage(string path) {
    // commandline:  dcraw -e -c <srcfile>  -> piped to stdout.
    if ( bf::exists( path ) ) {
        std::string exec = "bin\\dcraw.exe";

        std::vector<std::string> args;
        args.push_back("-v");
        args.push_back("-c");
        args.push_back("-e");
        args.push_back(path);

        bp::context ctx;
        ctx.stdout_behavior = bp::capture_stream();

        bp::child c = bp::launch(exec, args, ctx);

        bp::pistream &is = c.get_stdout();
        ofstream output("C:\\temp\\testcfk.jpg");
        streamcopy(is, output);
    }
    return (NULL);
}


inline void streamcopy(std::istream& input, std::ostream& out) {
    char buffer[4096];
    int i = 0;
    while (!input.eof() ) {
        memset(buffer, 0, sizeof(buffer));
        int bytes = input.readsome(buffer, sizeof buffer);
        out.write(buffer, bytes);
        i++;
    }
}
Run Code Online (Sandbox Code Playgroud)

调用转换器:

DCRawInterface DcRaw;
DcRaw.convertRawImage("test/CFK_2439.NEF"); 
Run Code Online (Sandbox Code Playgroud)

目标是简单地验证我可以将输入流复制到输出文件.

目前,如果我注释掉以下行:

    args.push_back("-c");
Run Code Online (Sandbox Code Playgroud)

然后缩略图由DCRAW写入源目录,名称为CFK_2439.thumb.jpg,这证明了使用正确的参数调用进程.没有发生的是正确连接到输出管道.

FWIW:我在Eclipse 3.5 /最新MingW(GCC 4.4)下在Windows XP上执行此测试.

[UPDATE]

从调试开始,当代码到达streamcopy时,文件/管道已经关闭 - bytes = input.readsome(...)永远不会是0以外的任何值.

neu*_*uro 3

好吧,我认为您需要正确重定向输出流。在我的应用程序中,类似这样的工作:

[...]

bp::command_line cl(_commandLine);
bp::launcher l;

l.set_stdout_behavior(bp::redirect_stream);
l.set_stdin_behavior(bp::redirect_stream);
l.set_merge_out_err(true);

bp::child c = l.start(cl);
bp::pistream& is = c.get_stdout();

string result;
string line;
while (std::getline(is, line) && !_isStopped)
{
    result += line;
}

c.wait();

[...]
Run Code Online (Sandbox Code Playgroud)

如果我没记错的话,如果没有重定向,标准输出将无处可去。如果您想获得整个输出,最好等待进程结束。

编辑:

我使用的 Linux 可能是旧版本的 boost.process。我意识到您的代码与我给您的代码片段类似。c.wait() 可能是关键......

编辑:Boost.process 0.1 :-)