Quick question. How to run an executable file in ocaml? I believe this is possible, but I don't know how.
Please provide an example code.
If you just want to run a program and not communicate with it, use Sys.command.
let exit_code = Sys.command "c:\\path\\to\\executable.exe /argument" in
(* if exit_code=0, the command succeeded. Otherwise it failed. *)
For more complex cases, you need to use the functions in the Unix module. Despite the name, most of them work under Windows. For example, if you need to get output from a command:
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 …
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With