Previous: , Up: Alien Variables   [Contents][Index]


8.4.2 External Alien Variables

External Alien names are strings, and Lisp names are symbols. When an external Alien is represented using a Lisp variable, there must be a way to convert from one name syntax into the other. The macros extern-alien, def-alien-variable and def-alien-routine use this conversion heuristic:

Macro: alien:def-alien-variable name type

This macro defines name as an external Alien variable of the specified Alien type. name and type are not evaluated. The Lisp name of the variable (see above) becomes a global Alien variable in the Lisp namespace. Global Alien variables are effectively “global symbol macros”; a reference to the variable fetches the contents of the external variable. Similarly, setting the variable stores new contents—the new contents must be of the declared type.

For example, it is often necessary to read the global C variable errno to determine why a particular function call failed. It is possible to define errno and make it accessible from Lisp by the following:

(def-alien-variable "errno" int)

;; Now it is possible to get the value of the C variable errno simply by
;; referencing that Lisp variable:
;;
(print errno)
Macro: alien:extern-alien name type

This macro returns an Alien with the specified type which points to an externally defined value. name is not evaluated, and may be specified either as a string or a symbol. type is an unevaluated Alien type specifier.


Previous: Local Alien Variables, Up: Alien Variables   [Contents][Index]