On this page:
The Racket Foreign Interface
8.15

The Racket Foreign Interface🔗ℹ

Eli Barzilay

 (require ffi/unsafe) package: base

The ffi/unsafe library enables the direct use of C-based APIs within Racket programs—without writing any new C code. From the Racket perspective, functions and data with a C-based API are foreign, hence the term foreign interface. Furthermore, since most APIs consist mostly of functions, the foreign interface is sometimes called a foreign function interface, abbreviated FFI.

    1 Overview

      1.1 Libraries, C Types, and Objects

      1.2 Function-Type Bells and Whistles

      1.3 By-Reference Arguments

      1.4 C Structs

      1.5 Pointers and Manual Allocation

      1.6 Pointers and GC-Managed Allocation

      1.7 Reliable Release of Resources

      1.8 Threads and Places

      1.9 More Examples

    2 Loading Foreign Libraries

    3 C Types

      3.1 Type Constructors

      3.2 Numeric Types

      3.3 Other Atomic Types

      3.4 String Types

        3.4.1 Primitive String Types

        3.4.2 Fixed Auto-Converting String Types

        3.4.3 Variable Auto-Converting String Type

        3.4.4 Other String Types

      3.5 Pointer Types

      3.6 Function Types

        3.6.1 Custom Function Types

      3.7 C Struct Types

      3.8 C Array Types

      3.9 C Union Types

      3.10 Enumerations and Masks

    4 Pointer Functions

      4.1 Pointer Dereferencing

      4.2 Memory Management

      4.3 Pointer Structure Property

    5 Derived Utilities

      5.1 Safe Homogenous Vectors

      5.2 Safe C Vectors

      5.3 Tagged C Pointer Types

      5.4 Serializable C Struct Types

      5.5 Static Callout and Callback Cores

      5.6 Defining Bindings

        5.6.1 FFI Identifier Conventions

      5.7 Allocation and Finalization

      5.8 Custodian Shutdown Registration

      5.9 Atomic Execution

      5.10 Speculatively Atomic Execution

      5.11 Thread Scheduling

      5.12 Ports

      5.13 Process-Wide and Place-Wide Registration

      5.14 Operating System Threads

        5.14.1 Operating System Asynchronous Channels

      5.15 Garbage Collection Callbacks

      5.16 Objective-C FFI

        5.16.1 FFI Types and Constants

        5.16.2 Syntactic Forms and Procedures

        5.16.3 Raw Runtime Functions

        5.16.4 Legacy Library

      5.17 Cocoa Foundation

        5.17.1 Strings

        5.17.2 Allocation Pools

      5.18 COM (Common Object Model)

        5.18.1 COM Automation

          5.18.1.1 GUIDs, CLSIDs, IIDs, and ProgIDs

          5.18.1.2 COM Objects

          5.18.1.3 COM Methods

          5.18.1.4 COM Properties

          5.18.1.5 COM Events

          5.18.1.6 COM Enumerations

          5.18.1.7 Interface Pointers

          5.18.1.8 Remote COM servers (DCOM)

          5.18.1.9 COM Types

          5.18.1.10 Class Display Names

        5.18.2 COM Classes and Interfaces

          5.18.2.1 Describing COM Interfaces

          5.18.2.2 Obtaining COM Interface References

          5.18.2.3 COM FFI Helpers

          5.18.2.4 COM Interface Example

        5.18.3 ActiveX Controls

      5.19 File Security-Guard Checks

      5.20 Windows API Helpers

      5.21 Virtual Machine Primitives

    6 Miscellaneous Support

    7 Unexported Primitive Functions

    Bibliography

    Index