2012-09-13 20 views

回答

3

也許像

#ifndef _WIN32 
inline int _pipe(int fildes[2], unsigned psize, int textmode) { 
    return pipe(fildes); 
} 
inline FILE* _popen(const char* command, const char* type) { 
    return popen(command, type); 
} 
inline void _pclose(FILE* file) { 
    pclose(file); 
} 
#endif 

MSDN說_popen_pclose_pipe在CRT中提供。因此,我們只是將強調的函數轉發到Windows以外的真正POSIX函數(您可以做相反的事情,但請注意,_pipe需要2個額外的參數,這在Windows中是有意義的)。

     ___________ 
        /   \ 
        | Warning ! | 
        \___________/ 
          | 
-------- [email protected]  [email protected]  | [email protected]  [email protected]  [email protected] 
----- _`\<,_ _`\<,_ _`\<,_  _`\<,_ _`\<,_ 
---- (*)/ (*) (*)/ (*) (*)/ (*) (*)/ (*) (*)/ (*) 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  1. 這些功能只能在控制檯程序中使用。它在Windows GUI程序中不起作用。有關詳細信息,請參閱_popen的MSDN頁面中的註釋,另請參閱What is the equivalent to Posix popen() in the Win32 API?

  2. 你知道popen在shell中執行命令嗎? UNIX shell與Windows命令提示符之間的語法大不相同,並且這些不能通過一些pope/pclose的跨平臺實現來解決。

  3. Windows函數與真正的POSIX函數之間可能存在一些細微的差別,但這些函數不是用這些簡單的包裝器處理的。

  4. 你不能在WinRT中使用它們。

(該自行車從http://www.chris.com/ascii/index.php?art=transportation%2Fbicycles複製。)

+0

啊,這是最簡單的方式Ç。我發現了一些現代的C++實現,包括重定向輸出特性,錯誤處理等。我想創建易於使用的機制來在單獨的進程中運行任何語句(因爲語句可以壓縮應用程序)並分析輸出和返回代碼。在做之前,我想環顧四周。 – Torsten

+2

@Torsten:那會是什麼實現?你能鏈接到它嗎? –

+0

@suluke對不起,我弄糊塗了:p。 – Stargateur