在ocaml中运行可执行文件(windows)文件

zfm*_*zfm 3 ocaml exe

快问.如何在ocaml中运行可执行文件?我相信这是可能的,但我不知道如何.

请提供示例代码.

Gil*_*il' 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. *)
Run Code Online (Sandbox Code Playgroud)

对于更复杂的情况,您需要使用模块中函数Unix.尽管有这个名字,但大多数都在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 …
Run Code Online (Sandbox Code Playgroud)