Hi there,
Foreign.LowLevel.call has the following type:
Foreign.LowLevel.call;
val it = fn: LowLevel.cType list -> LowLevel.cType -> LowLevel.symbol -> LowLevel.Memory.voidStar * LowLevel.Memory.voidStar -> unit
I understood what the arguments are: the cType list is the type of arguments, the cType is the return type, the symbol is the C function symbol in the loaded library. But the two general pointers are not clear. Where should they point to? For a void argument function I gave
val monitorVoidStar = Foreign.System.loadLibrary "monitor.dll";
for both and it worked, but I guess one of them should be a pointer to the argument list. What is the meaning of these two pointers?
val monitorLib = Foreign.loadLibrary "monitor.dll";
val monitorVoidStar = Foreign.System.loadLibrary "monitor.dll";
fun main () = Foreign.LowLevel.call [] Foreign.LowLevel.cTypeVoid (Foreign.getSymbol monitorLib "say_hello") (monitorVoidStar, monitorVoidStar)
$ cat monitor.c #include <stdio.h>
void say_hello() {
printf("Hello from C!\n");
}
- Gergely
On 29/07/2025 15:58, Gergely Buday wrote:
Foreign.LowLevel.call has the following type:
Foreign.LowLevel.call;
val it = fn: LowLevel.cType list -> LowLevel.cType -> LowLevel.symbol -> LowLevel.Memory.voidStar * LowLevel.Memory.voidStar -> unit
I understood what the arguments are: the cType list is the type of arguments, the cType is the return type, the symbol is the C function symbol in the loaded library. But the two general pointers are not clear. Where should they point to? For a void argument function I gave
The first is the pointer to the arguments, the second is a pointer to the memory to receive the result. In this case because both the arguments and the result are void the values you pass are ignored.
However, I would strongly advise you to use the higher level functions Foreign.buildCallXXX . These will take care of allocating and freeing the argument and result vectors. Have a look at the sample code in the samplecode/Foreign directory. That directory contains both the C code and the ML code that calls it.
David