25
Figure 7 M-Module Driver Example Code
3.4.2
Dealing with data
The data types shown in Table III are straightforward and cover most function calls to M-
Module drivers. But there are M-Module driver function calls that, for example, require special
data types (C structures) or require the calling software to allocate memory that the driver then
modifies. In both cases, there are no data types in Table III to handle this type of data.
The Alien library provides a host of functions and utilities that can be used to deal with data
types not covered by the standard types shown in Table III. The following sections discuss the
functions and utilities that are most pertinent to M-Module Drivers. These sections are not
meant to replace Alien documentation which may include further details and a more complete
list of utilities.
3.4.2.1
Buffers
Alien buffers allow the script to allocate a block of memory that can then be passed to a function
in place of any string or pointer type. A buffer is allocated with
alien.buffer().
If nothing
is passed to
alien.buffer()
a buffer of default length is allocated. If a number is passed a
buffer of that length is allocated. If a string or userdata is passed to
alien.buffer()
, a buffer
will be allocated and initialized with a copy of the string or data. Once the driver function is
called, the function
buf:tostring()
can be called to retrieve the data from the buffer.
Individual bytes of the buffer can be accessed using
buf[i]
where
i
is 1-based not 0-based.
Further you can get or set non-byte values using the buf:get(offset, type) and buf:set(offset,
type). Note that the buf:get() and buf:set() the offset is 1-based and is in bytes not elements so if
the buffer has integers, their offsets are 1, 5, 9, etc. An example of using buffers in a M-Module
driver call is shown in Figure 8.