In order to use functions defined in a dynamically linked library (DLL) within the LispWorks FLI, the functions need to be exported from the DLL.
You can export C functions in three ways:
__declspec(dllexport)
declaration in the C file.
In this case you should also make the functions use the cdecl
calling convention, which removes another level of name mangling.
/export
directive in the link command..def
file.
An example of method 2 follows. Let us assume you have the following C code in a file called example.c
.
int multiply (int i1, int i2) { int result; result = i1 * i2 * 500; return result; }
Then you can create a DLL by, for example, using a 32 bit C compiler such as cl.exe
.
cl /LD example.c /link /export:multiply
Finally, you should use the LispWorks FLI to define your C function in your Lisp code. This definition should look something like:
(fli:define-foreign-function (multiply "multiply") ((x :int) (y :int)) :result-type :int :module :my-dll :calling-convention :cdecl)
Note that the define-foreign-function also includes a :calling-convention
keyword to specify that the function we are interfacing to is defined as using the __cdecl
calling convention (the default for cl.exe
).
Having loaded your DLLs (with register-module) you may wish to test whether certain functions are now available.
To detect when a C function name is defined, call:
(not (fli:null-pointer-p (fli:make-pointer :symbol-name name :errorp nil)))
You can also return a list of unresolved foreign symbol names by calling module-unresolved-symbols.
You must make the exported names match the FLI definitions. To do this:
extern "C" {}
around the C++ function definitions, or:
Note: watch out for the calling convention of the exported function, which must match the :calling-convention
in the FLI definitions.
Foreign Language Interface User Guide and Reference Manual - 01 Dec 2021 19:34:57