Next: , Previous: , Up: Top   [Contents][Index]


7 Foreign Function Calling

Macro: %foreign-funcall name {arg-type arg}* &optional result-type ⇒ object
Macro: %foreign-funcall-pointer ptr {arg-type arg}* &optional result-type ⇒ object

Invoke a foreign function called name in the foreign source code.

Each arg-type is a foreign type specifier, followed by arg, Lisp data to be converted to foreign data of type arg-type. result-type is the foreign type of the function’s return value, and is assumed to be :void if not supplied.

%foreign-funcall-pointer takes a pointer ptr to the function, as returned by foreign-symbol-pointer, rather than a string name.

Examples

  ;; Calling a standard C library function:
  (%foreign-funcall "sqrtf" :float 16.0 :float) ⇒ 4.0
  ;; Dynamic allocation of a buffer and passing to a function:
  (with-foreign-ptr (buf 255 buf-size)
    (%foreign-funcall "gethostname" :pointer buf :size buf-size :int)
    ;; Convert buf to a Lisp string using MAKE-STRING and %MEM-REF or
    ;; a portable CFFI function such as CFFI:FOREIGN-STRING-TO-LISP.
    )