Next: , Previous: defcfun, Up: Functions


foreign-funcall

Syntax

— Macro: foreign-funcall name-and-options &rest arguments ⇒ return-value

arguments ::= { arg-type arg }* [return-type] name-and-options ::= name | ( name &key library convention)

Arguments and Values

name
A Lisp string.
arg-type
A foreign type.
arg
An argument of type arg-type.
return-type
A foreign type, :void by default.
return-value
A lisp object.
library
A lisp symbol; not evaluated.
convention
One of :cdecl (default) or :stdcall.

Description

The foreign-funcall macro is the main primitive for calling foreign functions.

If a foreign structure is to be passed or returned by value (that is, the type is of the form (:struct ...)), then the cffi-libffi system must be loaded, which in turn depends on libffi, including the header files. Failure to load that system will result in an error. Variadic functions cannot at present accept or return structures by value.

Note: The return value of foreign-funcall on functions with a :void return type is still undefined.

Implementation-specific Notes

Examples

  CFFI> (foreign-funcall "strlen" :string "foo" :int)
  => 3

Given the C code:

  void print_number(int n)
  {
      printf("N: %d\n", n);
  }
  CFFI> (foreign-funcall "print_number" :int 123456)
  -| N: 123456
  => NIL

Or, equivalently:

  CFFI> (foreign-funcall "print_number" :int 123456 :void)
  -| N: 123456
  => NIL
  CFFI> (foreign-funcall "printf" :string (format nil "%s: %d.~%")
                         :string "So long and thanks for all the fish"
                         :int 42 :int)
  -| So long and thanks for all the fish: 42.
  => 41

See Also

defcfun
foreign-funcall-pointer