Next Prev Up Top Contents Index

7.3 Using the Foreign Parser

The interface is the function foreign-parser:process-foreign-file.

The following example uses this C source code in file test.c . It needs to be slightly different depending on your platform.

Windows version:

#include <stdio.h>
#include <string.h>
__declspec(dllexport) void __cdecl modify(char *string)
{
  char temp[256];
  sprintf(temp, "'%s' modified in a C function", string);
  strcpy(string, temp);
}

Linux/Unix version:

#include <stdio.h>
#include <string.h>
void modify(char *string)
{
  char temp[256];
  sprintf(temp, "'%s' modified in a C function", string);
  strcpy(string, temp);
}
  1. Load the Foreign Parser:
  2. (require "foreign-parser")
  3. Now generate prototype FLI definitions:
  4. (foreign-parser:process-foreign-file
     "test.c"
     :case-sensitive nil)
    =>
    ;;;    Output dff file #P"test-dff.lisp"
    ;;;    Parsing source file "test.c"
     
    ;;; Process-foreign-file : Preprocessing file
     
    ;;; Process-foreign-file : Level 1 parsing
     
    ;;; Process-foreign-file : Selecting foreign forms
    NIL
  5. You should now have a Lisp file test-dff.lisp containing a form like this:
  6. (fli:define-foreign-function
        (modify "modify" :source)
        ((string (:pointer :char)))
      :result-type
      :void
      :language
      :c
      :calling-convention
      :cdecl)
  7. This edited version passes a string using :ef-mb-string :
  8. (fli:define-foreign-function
        (modify "modify" :source)
        ((string (:reference (:ef-mb-string :limit 256))))
      :result-type
      :void
      :language
      :c
      :calling-convention
      :cdecl)
    =>
    MODIFY
  9. Load the foreign code by
  10. (fli:register-module "test.dll")

    or

    (fli:register-module "/tmp/test.so")
  11. Call the C function from LISP:
(modify "Hello, I am in LISP")
=>
NIL
"'Hello, I am in LISP' modified in a C function"

 


LispWorks Foreign Language Interface User Guide and Reference Manual - 14 Dec 2001

Next Prev Up Top Contents Index