2010-03-16 46 views
7

当前我正在使用Boost沙盒中的Boost.Process,并且遇到问题才能正确捕获我的标准输出;想知道是否有人可以给我第二双眼球成为我可能做错的事。无法使用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++; 
    } 
} 

调用转换器:

DCRawInterface DcRaw; 
DcRaw.convertRawImage("test/CFK_2439.NEF"); 

的目标是简单的验证,我可以输入流复制到输出文件。

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

args.push_back("-c"); 

那么缩略图就被Dcraw执行写入源目录中CFK_2439.thumb.jpg的名字,这证明,我认为这个过程是用正确的参数进行调用。没有发生的事情正确地连接到输出管道。

FWIW:我在Eclipse 3.5/Latest MingW(GCC 4.4)下的Windows XP上执行此测试。

[UPDATE]

从调试,它会出现由代码到达复制视频流的时间,文件/管已经关闭 - 字节= input.readsome(...)是从未任何值而不是0.

+0

可能不是主要问题,但'output''ofstream'应该以二进制模式打开。另外:'streamcopy'可以简化为'out << input.rdbuf();' –

+0

input.rdbuf()的良好调用。在我写流媒体之前,我实际上正在使用它来查看引擎盖下发生了什么。 –

回答

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(); 

[...] 

没有重定向的标准输出将无处可去,如果我没有记错的话。如果您希望获得整个输出,那么等待过程结束是一个好习惯。

编辑:

我的Linux上也许是旧版本boost.process的。我意识到你的代码与我给你的代码片段相似。该c.wait()可能是关键......

编辑:Boost.process 0.1 :-)

1

如果迁移到“最新” boost.process是不是一个问题(如你肯定知道有此库的几个变种),您可以使用以下(http://www.highscore.de/boost/process0.5/

file_descriptor_sink sink("stdout.txt"); 
execute(
    run_exe("test.exe"), 
    bind_stdout(sink) 
); 
相关问题