NextPrevUpTopContentsIndex

4.2.3 Passing a constant string

Use of the :reference-pass type in this example converts the Lisp string to a foreign string on calling, but does not convert the string back again on return.

Here is the C code for the example. It uses the argument string but returns an integer.

Windows version:

#include <string.h>
#include <ctype.h>
 
__declspec(dllexport) int __cdecl count_upper(const char *string)
{
  int count;
  int len;
  int ii;
  count = 0;
  len = strlen(string);
  for (ii = 0; ii < len ; ii++)
      if (isupper(string[ii]))
         count++;
  return count;
}

Linux/Unix/Macintosh version:

#include <string.h>
#include <ctype.h>
 
int count_upper(const char *string)
{
  int count;
  int len;
  int ii;
  count = 0;
  len = strlen(string);
  for (ii = 0; ii < len ; ii++)
      if (isupper(string[ii]))
         count++;
  return count;
}

Here is the foreign function definition using :reference-pass :

(fli:define-foreign-function (count-upper "count_upper" :source)
                             ((string (:reference-pass :ef-mb-string)))
                             :result-type :int
                             :language :c
                             :calling-convention :cdecl)
 
(count-upper "ABCdef")
=>
3

LispWorks Foreign Language Interface User Guide and Reference Manual - 14 Mar 2008

NextPrevUpTopContentsIndex