2011年6月18日 星期六

在Windows下建pipe

在Windows下建pipe

compiler是mingw32-gcc,可用popen。

compiler是VC++,console(控制台,黑黑的視窗)模式下,要用_popen。
但視窗模式下無效,也不會顯示錯誤訊息。

因為popen是Unix的東西,Windows雖有模擬,但使用上有些限制( ex. 視窗程式不能用 )。


Windows有提供它的 Pipe Functions:

Pipe Functions
http://msdn.microsoft.com/en-us/library/aa365781%28v=VS.85%29.aspx


上網一查,發現有人跟我遇到一樣的問題,而且他跟我的目的一樣,都是要呼叫gnuplot

以下只摘錄回答
===================================================================
http://www.programmer-club.com.tw/ShowSameTitleN/c/36114.html
作者 : sunny_gong(simula)
如果是使用 VC++ 的 runtime library,可以參考說明文件。C runtime library中與作業系統有關的function call,在console模式下應該都沒啥問題,但是在視窗模式中可能就有相容性的問題。其原因就是Windows並不是Unix-like系統,而C runtime library的作業系統服務呼叫就是Unix的系統呼叫,Windows是另外設計出子系統來模擬這些Unix系統呼叫的,例如console子系統與POSIX子系統。所以在Windows上面使用C runtime library的作業系統服務呼叫,都得閱讀一下說明文件,看看有沒什麼差異或限制,或者乾脆用Windows本身的系統呼叫。我只知道個大概,Inside Windows NT之類的書籍有更詳細的說明。

[MSDN Library\Run-Time Library Reference\_popen]
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.

Windows NT保護子系統:
http://lee-1.com/hlchou/WindowsNTSubSystem.htm
=========================================================================

沒有留言:

張貼留言