Lisp HUG Maillist Archive

fli:define-foreign-callable

Hi,
I am new to LispWorks. I am trying to define a Lisp function that will be 
called from a C layer. Here is what my Lisp function will appear to be for 
the C function:

const char *myFunction(const char *arg1, int arg2);

Can you show me how to make this a foreign callable function? I tried to 
read the fli reference manual, but it appears quite complex!

Thanks for the help.
Regards,
Rangarajan 



Re: fli:define-foreign-callable

On Sun, 20 Jan 2008 09:37:09 +0530, "Rangarajan Krishnamoorthy" <ranga@mmsindia.com> wrote:

> I am new to LispWorks. I am trying to define a Lisp function that
> will be called from a C layer. Here is what my Lisp function will
> appear to be for the C function:
>
> const char *myFunction(const char *arg1, int arg2);
>
> Can you show me how to make this a foreign callable function? I
> tried to read the fli reference manual, but it appears quite
> complex!

It appears complex because the topic at hand is complex...

You /probably/ want something like this:

  CL-USER 1 > (fli:define-foreign-callable ("myFunction"
                                            :result-type (:pointer (:unsigned :char)))
                  ((name (:reference-return :ef-mb-string))
                   (n :int))
                (fli:convert-to-foreign-string (format nil "The ~:R ~A" n name)))
  "myFunction"

  CL-USER 2 > (fli:define-foreign-function (my-function "myFunction")
                  ((name (:reference-pass :ef-mb-string))	
                   (n :int))	
                :result-type (:reference-return :ef-mb-string))
  MY-FUNCTION

  CL-USER 3 > (my-function "Henry" 8)
  "The eighth Henry"

Of course, you'll also have to take care of external formats and
allocation.  (Who will de-allocate the string your function returns?)

HTH,
Edi.


Re: fli:define-foreign-callable

On Sun, 20 Jan 2008 18:26:29 +0530, "Rangarajan Krishnamoorthy" <ranga@mmsindia.com> wrote:

> After a bit of trial and error, I found that the following works:
> ;;; The Lisp function takes a string and returns an integer
> ;;; DLL representation => int interfaceFunction(const char *arg)
> (fli:define-foreign-callable ("interfaceFunction" :language :ansi-c 
> :result-type :int :calling-convention :cdecl)
>     ((phnum (:pointer (:unsigned :char))))
>   (actualFunction (fli:convert-from-foreign-string phnum)))

Please indent your Lisp code correctly when sending it to the mailing
list.

Note that the (:REFERENCE-RETURN :EF-MB-STRING) type in my example
does the FLI:CONVERT-FROM-FOREIGN-STRING for you automatically.

Edi.


Updated at: 2020-12-10 08:44 UTC