2010-03-16 47 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) 
); 
相關問題