Skip to content

Instantly share code, notes, and snippets.

@eldesh

eldesh/dynlink.sml

Last active Aug 29, 2015
Embed
What would you like to do?
use Win32 API with MLton FFI
ann "allowFFI true" in
$(SML_LIB)/basis/basis.mlb
$(SML_LIB)/basis/mlton.mlb
$(SML_LIB)/basis/c-types.mlb
dynlink.sml
win32.sml
end
(**
* $ mlton win32.mlb
* $ ./win32
*)
local
val MessageBoxA = _import "MessageBoxA" stdcall: C_Pointer.t * string * string * Word32.word -> int;
in
val _ = MessageBoxA (C_Pointer.null, "Hello World!", "MLton Static FFI", 0w4096)
end
local
val double_to_double = _import * : DynLink.fptr -> real -> real;
val user32 = DynLink.dlopen ("user32.dll", DynLink.RTLD_LAZY)
val sigMessageBoxA =
_import * : DynLink.fptr -> C_Pointer.t * string * string * Word32.word -> int;
val msgbox_ptr = DynLink.dlsym (user32, "MessageBoxA")
val dynMessageBoxA = sigMessageBoxA msgbox_ptr
in
val _ = dynMessageBoxA (C_Pointer.null, "Hello World!", "MLton Dynamic FFI", 0w4096)
val _ = DynLink.dlclose user32
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment