On this page:
ffi-lib?
2.1 Unsafe Library Functions
ffi-lib
get-ffi-obj
set-ffi-obj!
make-c-parameter
define-c
ffi-obj-ref

2 Loading Foreign Libraries

The FFI is normally used by extracting functions and other objects from shared objects (a.k.a. shared libraries or dynamically loaded libraries). The ffi-lib function loads a shared object.

(ffi-lib? v)  boolean>
  v : any/c
Returns #t if v is the result of ffi-lib, #f otherwise.

2.1 Unsafe Library Functions

(ffi-lib path [version])  any
  path : (or/c path-string? #f)
  version : (or/c string? (listof (or/c string? #f)) #f) = #f
Returns an foreign-library value. If path is a path, the result represents the foreign library, which is opened in an OS-specific way (using LoadLibrary under Windows, and dlopen under Unix and Mac OS X).

The path is not expected to contain the library suffix, which is added according to the current platform. If adding the suffix fails, several other filename variations are tried: retrying without an automatically added suffix, and using a full path of a file if it exists relative to the current directory (since the OS-level library function usually searches, unless the library name is an absolute path). An optional version string can be supplied, which is appended to the name before or after the suffix, depending on platform conventions, unless it is #f or "". If version is a list, ffi-lib will try each of them in order.

If path is #f, then the resulting foreign-library value represents all libraries loaded in the current process, including libraries previously opened with ffi-lib. In particular, use #f to access C-level functionality exported by the run-time system (as described in Inside: PLT Scheme C API).

Note: ffi-lib tries to look for the library file in a few places, inluding the PLT libraries (see get-lib-search-dirs), a relative path, or a system search. When version is a list, different versions are tried through each route before continuing the search with other routes. However, if dlopen cannot open a library, there is no reliable way to know why it failed, so if all path combinations fail, it will raise an error with the result of dlopen on the unmodified argument name. For example, if you have a local "foo.so" library that cannot be loaded because of a missing symbol, using (ffi-lib "foo.so") will fail with all its search options, most because the library is not found, and once because of the missing symbol, and eventually produce an error message that comes from dlopen("foo.so") which will look like the file is not found. In such cases try to specify a full or relative path (containing slashes, e.g., "./foo.so").

(get-ffi-obj objname lib type [failure-thunk])  any
  objname : (or/c string? bytes? symbol?)
  lib : (or/c ffi-lib? path-string? #f)
  type : ctype?
  failure-thunk : (or/c (-> any) #f) = #f
Looks for the given object name objname in the given lib library. If lib is not a foreign-library value produced by ffi-lib, it is converted to one by calling ffi-lib. If objname is found in lib, it is converted to Scheme using the given type. Types are described in C Types; in particular the get-ffi-obj procedure is most often used with function types created with _fun.

Keep in mind that get-ffi-obj is an unsafe procedure; see Overview for details.

If the object is not found, and failure-thunk is provided, it is used to produce a return value. For example, a failure thunk can be provided to report a specific error if an object is not found:

  (define foo
    (get-ffi-obj "foo" foolib (_fun _int -> _int)
      (lambda ()
        (error 'foolib
               "installed foolib does not provide \"foo\""))))

The default (also when failure-thunk is provided as #f) is to raise an exception.

(set-ffi-obj! objname lib type new)  void?
  objname : (or/c string? bytes? symbol?)
  lib : (or/c ffi-lib? path-string? #f)
  type : ctype?
  new : any/c
Looks for objname in lib similarly to get-ffi-obj, but then it stores the given new value into the library, converting it to a C value. This can be used for setting library customization variables that are part of its interface, including Scheme callbacks.

(make-c-parameter objname lib type)  
(and/c (-> any)
       (any/c -> void?))
  objname : (or/c string? bytes? symbol?)
  lib : (or/c ffi-lib? path-string? #f)
  type : ctype?
Returns a parameter-like procedure that can either references the specified foreign value, or set it. The arguments are handled as in get-ffi-obj.

A parameter-like function is useful in case Scheme code and library code interact through a library value. Although make-c-parameter can be used with any time, it is not recommended to use this for foreign functions, since each reference through the parameter will construct the low-level interface before the actual call.

(define-c id lib-expr type-expr)
Defines id behave like a Scheme binding, but id is actually redirected through a parameter-like procedure created by make-c-parameter. The id is used both for the Scheme binding and for the foreign object’s name.

(ffi-obj-ref objname lib [failure-thunk])  any
  objname : (or/c string? bytes? symbol?)
  lib : (or/c ffi-lib? path-string? #f)
  failure-thunk : (or/c (-> any) #f) = #f
Returns a pointer object for the specified foreign object. This procedure is for rare cases where make-c-parameter is insufficient, because there is no type to cast the foreign object to (e.g., a vector of numbers).