I am new to OCaml, and try to learn the Ctypes FFI.
My code below is working fine from within the utop
toplevel, but when I try to compile it to native (with ocamlfind ocamlopt -o hello.exe -linkpkg -package ctypes,ctypes.foreign -thread test.ml
), the resulting binary does not execute VirtualAlloc
.
However, if I move the line let allocaddress = virtualalloc null 276 12288 64
right after the VirtualAlloc
binding, the resulting binary works as expected. Evaluating the expressions with ;;
has the same behaviour.
open Ctypes
let dll = Dl.dlopen ~filename:"kernel32.dll" ~flags:[]
let virtualalloc =
Foreign.foreign ~from:dll "VirtualAlloc"
(ptr void @-> int @-> int @-> int @-> returning int)
let rtlmovememory =
Foreign.foreign ~from:dll "RtlMoveMemory"
(int @-> ptr void @-> int @-> returning void)
let allocaddress = virtualalloc null 276 12288 64