2011-06-03 145 views
3

快速的問題。如何在ocaml中運行可執行文件?我相信這是可能的,但我不知道如何。在ocaml中運行可執行文件(windows)文件

請提供示例代碼。

+1

請看一下關於'Unix'模塊的手冊。 http://caml.inria.fr/pub/docs/manual-ocaml/manual035.html – nlucaroni 2011-06-03 17:30:41

+2

或'Sys.command'適用於最簡單的情況 – 2011-06-03 18:44:40

回答

6

如果你只是想運行一個程序,而不是與它溝通,請使用Sys.command

let exit_code = Sys.command "c:\\path\\to\\executable.exe /argument" in 
(* if exit_code=0, the command succeeded. Otherwise it failed. *) 

對於更復雜的情況,您需要使用functions in the Unix module。儘管名稱,most of them在Windows下工作。例如,如果您需要從命令中獲得輸出:

let ch = Unix.open_process_in "c:\\path\\to\\executable.exe /argument" in 
try 
    while true do 
    let line = input_line ch in … 
    done 
with End_of_file -> 
    let status = close_process_in ch in …