I am having trouble displaying native OSX calls from OCaml, where the c-call is expecting the buffer and size to be transferred. I worked with examples in Real World OCaml using CTypes and Foreign, but they don’t cover this case or at least I don’t understand how I feel about it.
Here is my fragment of OCaml:
open Core.Std
open Unix
open Ctypes
open Foreign
(* from /usr/include/libproc.h
int proc_pidpath(int pid, void * buffer, uint32_t buffersize);
*)
let proc_pidpath = foreign "proc_pidpath" (int @-> ptr void @-> int @-> returning int)
let () =
let pid = Pid.to_int (Unix.getpid ()) in
let buf = allocate string 255 in
let path = proc_pidpath(pid, buf, 255) in
printf "Pid: %i Path: %s\n" pid buf
How to allocate a buffer to pass to proc_pidpath()and is there a better way to wrap this call so that it returns an Option type (String or Nil) or just a string?
source
share