NextPrevUpTopContentsIndex

define-foreign-variable

Macro
Summary

Defines a Lisp function to access a variable in foreign code.

Package

fli

Signature
define-foreign-variable the-name
 &key type
 accessor
 language
 
                                                                   
   no-check
 module
 => lisp-name
the-name
 ::= lisp-name
 | (lisp-name
 foreign-name
 [encoding
]) |
            (lisp-name
 foreign-string
 [encoding
])
encoding ::= :source | :object | :lisp | :dbcs accessor ::= :value | :address-of | read-only
Arguments

the-name

Names the Lisp function which is used to access the foreign variable.

lisp-name

A symbol naming the Lisp accessor.

foreign-name

A symbol which is the name of the foreign variable.

foreign-string

A string containing the name of the foreign variable.

encoding

An option controlling how the Lisp variable name is translated to match the foreign variable name in the foreign DLL. The encoding option can be one of the following:

:source tells LispWorks that the string foreign-string or the symbol foreign-name is the name of the variable in the foreign source code. This is the default value of encoding .

:object tells LispWorks that the string foreign-string or the symbol foreign-name is the literal name of the variable in the foreign object code.

:lisp tells LispWorks that the symbol foreign-name is a Lisp symbol and must be translated and encoded.

:dbcs adds a suffix onto the variable name which will automatically change depending on whether Lisp image is started up in the Windows NT or Windows 95 operating system. The suffix is " A " for Windows 95 and " W " for Windows NT.

type

The FLI type corresponding to the type of the foreign variable to which Lisp is interfacing.

accessor

An option specifying what kind of accessor is generated for the variable. It can be one of the following:

:value gets the value of the foreign variable directly. This is the default value.

:address-of returns a FLI pointer to the foreign variable.

:read-only ensures that no setf method is defined for the variable, which means that its value can be read, but it cannot be set.

language

The language in which the foreign source code for the variable is written. The default is :c .

no-check

If nil , the types of the arguments provided when the Lisp function is called are compared with the expected types and an error is raised if they do not match. Setting no-check to t overrides this check.

module

Specifies the module in which the foreign variable is defined. If it is the name of a module registered using register-module then that module is used to look up the variable. If the name of the module is a string then that module is automatically registered and used to look up the symbol.

Values

lisp-name

A symbol naming the Lisp accessor.

Description

The macro define-foreign-variable defines a Lisp accessor which can be used to get and set the value of a variable defined in foreign code.

If the foreign variable has a type corresponding to an FLI aggregate type, then a copy of the object is allocated using allocate-foreign-object, and the copy is then accessed.

Example

The following example illustrates how to use the FLI to define a foreign variable, given the following C variable in a DLL:

int num;

The first example defines a Lisp variable, num1 , to interface with the C variable num .

(fli:define-foreign-variable (num1 "num") :type :int)

The following commands return the value of num , and increase its value by 1:

(num1) (incf (num1))

In the next example, the Lisp variable num2 interfaces with num in a read-only manner.

(fli:define-foreign-variable (num2 "num")
  :type :int :accessor :READ-ONLY)

In this case, the next command still returns the value of num , but the second command raises an error, because num2 is read-only.

(num2) (incf (num2))

The final example defines a Lisp variable, num3 , which accesses num through pointers.

(fli:define-foreign-variable (num3 "num")
  :type :int :accessor :address-of)

As a result, the next command returns a pointer to num , and to obtain the actual value stored by num , num3 needs to be dereferenced.

(num3) (fli:dereference (num3))
See also

define-foreign-callable
define-foreign-function


LispWorks Foreign Language Interface User Guide and Reference Manual - 27 Mar 2005

NextPrevUpTopContentsIndex