Tcl CFFI package (v2.0.3)

::cffiTop, Main, Index

This is the reference for commands in the cffi namespace. See the Start page for an introductory usage guide. See Concepts for a mapping of C types and program elements to the script level.

The package is loaded in standard fashion with package require:

package require cffi

CommandsTop, Main, Index

alias [::cffi]Top, Main, Index

A command ensemble.

alias subcommand ...
Description

The ensemble supports the following subcommands:

bodyReturns the resolved body of an alias.
clearDeletes all aliases from the interpreter.
defineDefines one or more type aliases.
deleteDeletes aliases matching a pattern.
listReturns a list of aliases that match the specified pattern.
loadLoads predefined type aliases.

Refer to the documentation of each subcommand for details.

alias body [::cffi]Top, Main, Index

Returns the resolved body of an alias.

alias body alias_name
Parameters
alias_nameName of alias to be retrieved.
Description

The alias must be one defined in the current scope.

Return value

Returns the resolved body of an alias.

alias clear [::cffi]Top, Main, Index

Deletes all aliases from the interpreter

alias clear

alias define [::cffi]Top, Main, Index

Defines one or more type aliases.

alias define name definition
alias define aliasdefs
Parameters
aliasdefsDictionary mapping alias names to their definition.
definitionDefinition for the alias.
nameName for type alias. Must begin with an alphabetic character and may contain alphanumerics and underscore.
Description

Given two arguments, creates an alias of the given name for passed definition. If a single argument is provided, it must be a dictionary mapping alias names to their definitions.

An error will be raised if an alias name matches a built-in type or if an alias of that name already exists with a different definition in the same scope.

The definition may itself be based on an existing alias. In this case the existing alias is immediately resolved and expanded as part of the definition so further changes to it are not reflected in the alias being defined.

See Type aliases for more on type aliases.

alias delete [::cffi]Top, Main, Index

Deletes aliases matching a pattern.

alias delete pattern
Parameters
patternPattern to match against aliases.
Description

The command deletes all aliases whose name matches the specified pattern. The conditions for matching the pattern are:

alias list [::cffi]Top, Main, Index

Returns a list of aliases that match the specified pattern.

alias list ?pattern?
Parameters
patternPattern to match against aliases. Optional, default *.
Description

The command returns the alias names that match the specified pattern. The conditions for matching the pattern are:

Return value

Returns a list of aliases that match the specified pattern.

alias load [::cffi]Top, Main, Index

Loads predefined type aliases.

alias load alias_set
Parameters
alias_setThe alias set to load.
Description

The aliases are loaded into the ::cffi::c scope. Since this scope is also searched when looking up aliases, the aliases defined by this command is available in all namespace without explicit qualification.

The possible values of $alias_set are C, win32 (Windows only), or posix (platform dependent).

The C predefined alias set includes _Bool, bool, int8_t uint8_t int16_t uint16_t int32_t uint32_t int64_t uint64_t size_t ssize_t

The win32 predefined alias set includes BOOL BOOLEAN BYTE CHAR DWORD DWORDLONG DWORD_PTR HALF_PTR HANDLE INT INT_PTR LONG LONGLONG LONG_PTR LPARAM LPVOID LRESULT SHORT SIZE_T SSIZE_T UCHAR UINT UINT_PTR ULONG ULONGLONG ULONG_PTR USHORT WORD WPARAM

The posix alias set is platform dependent. In POSIX compliant environments, it includes blkcnt_t blksize_t clock_t dev_t fsblkcnt_t fsfilcnt_t gid_t id_t ino_t key_t mode_t nlink_t off_t pid_t size_t ssize_t suseconds_t time_t uid_t. On Windows, it only includes dev_t ino_t off_t time_t.

arena [::cffi]Top, Main, Index

A command ensemble.

arena subcommand ...
Description

The ensemble supports the following subcommands:

allocateAllocates memory in the current arena frame in the memory arena.
newAllocates memory in the current arena for a type and initializes it.
popframePops the current frame from the memory arena.
pushframePushes a new stack frame on the memory arena.

Refer to the documentation of each subcommand for details.

arena allocate [::cffi]Top, Main, Index

Allocates memory in the current arena frame in the memory arena.

arena allocate sizespec ?tag?
Parameters
sizespecRequested size of memory block. This may be specified either as an integer value or a type specification (optional)
tagTag for returned pointer. Optional, default "".
Description

The allocated memory will stay allocated until the frame is destroyed with arena popframe. The size $sizespec may be specified as a positive integer or a CFFI type declaration in which case the memory allocated is that of the type's size.

Return value

Returns a safe pointer to the allocation.

See also

arena new

arena new [::cffi]Top, Main, Index

Allocates memory in the current arena for a type and initializes it.

arena new typespec initializer ?tag?
Parameters
typespecA type declaration.
initializerThe type-specific value to use to initialize allocated memory.
tagThe optional tag for the returned pointer. Optional, default "".
Description

The $typespec argument may be any type specificiation. If an array is specified of a larger size than the number of elements in $initializer, the remaining elements are zeroed out.

The returned memory will be freed when the arena frame is freed with arena popframe.

Return value

Returns a safe pointer to the allocated memory.

See also

arena allocate

arena popframe [::cffi]Top, Main, Index

Pops the current frame from the memory arena.

arena popframe
Description

All allocations from the popped frame are freed and pointers to these unregistered.

arena pushframe [::cffi]Top, Main, Index

Pushes a new stack frame on the memory arena.

arena pushframe ?size?
arena pushframe size ?tag?
Parameters
sizespecRequested size of memory block. This may be specified either as an integer value or a type specification (optional)
tagTag for returned pointer if $size is specified (optional)
Description

The command allocate a new frame in the memory arena stack. All allocations from this frame will stay valid only until the corresponding call to arena popframe.

If no arguments are specified, the command initializes an empty frame. Otherwise it allocates storage of $sizespec bytes within the new frame and returns a pointer to it. The size $sizespec may be specified as a positive integer or a CFFI type declaration in which case the memory allocated is that of the type's size.

Return value

Returns an empty string if not arguments were specified or a safe pointer.

See also

arena allocate, arena popframe

call [::cffi]Top, Main, Index

Invokes a C function through a function pointer.

call fnptr ?args?
Parameters
fnptrA pointer value tagged with a prototype
argsAdditional arguments to pass to the C function.
Description

The passed pointer $fnptr tag must corresponding to a function prototype defined through the prototype function or prototype stdcall commands.

Return value

Returns the value returned by the invoked C function.

callback [::cffi]Top, Main, Index

A command ensemble.

callback subcommand ...
Description

The ensemble supports the following subcommands:

freeFrees a callback pointer.
newWraps a script level command into a C function.

Refer to the documentation of each subcommand for details.

callback free [::cffi]Top, Main, Index

Frees a callback pointer

callback free cb
Parameters
cbA function pointer allocated with callback new

callback new [::cffi]Top, Main, Index

Wraps a script level command into a C function

callback new protoname cmdprefix error_value
Parameters
protonameThe name of a prototype created through the prototype function or prototype stdcall commands.
cmdprefixThe command prefix that should be invoked when the created C function is called.
error_valueThe value that should be returned if the command prefix raises an exception. This is optional if the function prototype specifies the void return type.
Description

The returned function pointer can be invoked through the call command but the common usage is for it to be passed to a C function that takes a callback function as an argument.

Invoking the function pointer will result in $cmdprefix being called with the additional arguments passed in the invocation. These must match the parameter types of the prototype specified in the callback definition.

The type declarations in a prototypes used for callbacks have certain restrictions. These arise because the "data flow" is now from script to the C shared library.

The parameter and return type are restricted to be scalar values and strings.

When $cmdprefix is called from a C function as a callback, it is executed in the Tcl context from which the C function was called and thus has access to the script level local variables etc.

When no longer needed, the callback should be freed with the callback free command.

Return value

Returns a callback function pointer that can be called from C native code.

enum [::cffi]Top, Main, Index

A command ensemble.

enum subcommand ...
Description

The ensemble supports the following subcommands:

aliasDefines a new enumeration and alias of the same name.
clearDeletes all enumerations from the interpreter.
defineDefines a new enumeration.
deleteDeletes enumerations.
flagsDefines an enumeration as bit flags.
listLists enumerations.
maskReturns an integer mask formed by bitwise OR-ing the passed enumeration members.
membersReturns a dictionary containing the members in an enumeration.
nameReturns the symbolic name of a value in an enumeration.
namesReturns a list containing the names of members in an enumeration.
sequenceDefines an enumeration with consecutive member values.
unmaskReturns a list of enumeration member names corresponding to the bits set in $mask.
valueReturns the value of an enumeration member.

Refer to the documentation of each subcommand for details.

enum alias [::cffi]Top, Main, Index

Defines a new enumeration and alias of the same name

enum alias enumname enumdefs typedecl
Parameters
enumnameName of the enumeration.
enumdefsDictionary of enumeration member names to their values.
typedeclThe type to be used for the alias. Should have an integer base type and may contain other attributes except enum.
Description

The created alias has the same qualified name as the enum and has the corresponding enum attribute.

Return value

Returns the fully qualified enumeration name.

enum clear [::cffi]Top, Main, Index

Deletes all enumerations from the interpreter

enum clear

enum define [::cffi]Top, Main, Index

Defines a new enumeration.

enum define enumname enumdefs
Parameters
enumnameName of the enumeration.
enumdefsDictionary of enumeration member names to their values.
Description

Parameter declarations can reference the enumeration to indicate that the corresponding symbol names are acceptible as arguments and will be replaced by the corresponding value in the function call. Enumeration member values must be integers.

Return value

Returns the fully qualified enumeration name.

enum delete [::cffi]Top, Main, Index

Deletes enumerations.

enum delete pattern
Parameters
patternPattern to match.
Description

The command deletes all enumerations whose name matches the specified pattern. The conditions for matching the pattern are:

enum flags [::cffi]Top, Main, Index

Defines an enumeration as bit flags.

enum flags enumname membernames
Parameters
enumnameName of the enumeration.
membernamesList of names of the enumeration members.
Description

For example,

enum flags E {A B C}

would define an enumeration with A, B, C values being 1, 2 and 4.

Return value

Returns the fully qualified enumeration name.

enum list [::cffi]Top, Main, Index

Lists enumerations.

enum list pattern
Parameters
patternPattern to match.
Description

The command returns the names of all enumerations whose name matches the specified pattern. The conditions for matching the pattern are:

enum mask [::cffi]Top, Main, Index

Returns an integer mask formed by bitwise OR-ing the passed enumeration members

enum mask enumname membernames
Parameters
enumnameName of the enumeration in the current scope
membernamesList of member names and integer values.
Return value

Returns an integer mask formed by bitwise OR-ing the passed enumeration members

See also

enum unmask

enum members [::cffi]Top, Main, Index

Returns a dictionary containing the members in an enumeration.

enum members enumname
Parameters
enumnameName of the enumeration whose elements are to be returned.
Return value

Returns a dictionary containing the members in an enumeration.

enum name [::cffi]Top, Main, Index

Returns the symbolic name of a value in an enumeration.

enum name enumname membervalue ?default?
Parameters
enumnameName of the enumeration.
membervalueValue of an enumeration member.
defaultValue to be returned if the member value is not found.
Description

The command will raise an error if $membervalue is not present in the enumeration and no default is provided, or if $enumname is itself undefined.

Return value

Returns the symbolic name of a value in an enumeration.

enum names [::cffi]Top, Main, Index

Returns a list containing the names of members in an enumeration.

enum names enumname
Parameters
enumnameName of the enumeration whose element names are to be returned.
Return value

Returns a list containing the names of members in an enumeration.

enum sequence [::cffi]Top, Main, Index

Defines an enumeration with consecutive member values

enum sequence enumname membernames ?start?
Parameters
enumnameName of the enumeration.
membernamesList of names of the enumeration members.
startValue of first enumeration member. Optional, default 0.
Description

Each element of $membernames should have one or two elements, the first being the name of the member and the second, if present, being its value.If the second element is not present, the member is assigned a value one greater than the previous value assigned. The first member is assigned value 0 if it has no explicit assigned value.

The $start parameter is present for backward compatibility and indicates the first value to be assigned to the sequence. For future compatibility, the two-element form for the first enum member should be used instead if a starting value other than the default zero is desired.

Return value

Returns the fully qualified enumeration name.

enum unmask [::cffi]Top, Main, Index

Returns a list of enumeration member names corresponding to the bits set in $mask.

enum unmask enumname mask
Parameters
enumnameName of the enumeration.
maskInteger bit mask value.
Description

The last element of the returned list is $mask itself. Generally, the enumeration should consist of values that do not have overlapping bits set (commonly a single bit is set in each member value). This is not enforced but results might not be as expected if this condition is not met.

Return value

Returns a list of enumeration member names corresponding to the bits set in $mask.

See also

enum mask

enum value [::cffi]Top, Main, Index

Returns the value of an enumeration member

enum value enumname membername ?default?
Parameters
enumnameName of the enumeration.
membernameName of the enumeration member to retrieve.
defaultDefault value to be returned if the membername is not found.
Description

The command will raise an error if $membername is not present in the enumeration and no default is provided, or if $enumname is itself undefined.

Return value

Returns the value of an enumeration member

help [::cffi]Top, Main, Index

A command ensemble.

help subcommand ...
Description

The ensemble supports the following subcommands:

aliasReturns a string describing an alias definition.
enumReturns a string describing an enum definition.
functionReturns a string describing the syntax and parameter definitions for a CFFI wrapped function.
functionsReturns a list of CFFI-wrapped functions matching the pattern PATTERN.
structReturns a string describing field types in a Struct.
unionReturns a string describing field types in a Union.

Refer to the documentation of each subcommand for details.

help alias [::cffi]Top, Main, Index

Returns a string describing an alias definition.

help alias NAME
Parameters
NAMEName of a alias
Return value

Returns a string describing an alias definition.

help enum [::cffi]Top, Main, Index

Returns a string describing an enum definition.

help enum NAME
Parameters
NAMEName of a enum
Return value

Returns a string describing an enum definition.

help function [::cffi]Top, Main, Index

Returns a string describing the syntax and parameter definitions for a CFFI wrapped function.

help function NAME
Parameters
NAMEName of a wrapped function.
Return value

Returns a string describing the syntax and parameter definitions for a CFFI wrapped function.

help functions [::cffi]Top, Main, Index

Returns a list of CFFI-wrapped functions matching the pattern PATTERN.

help functions ?PATTERN?
Parameters
PATTERNGlob pattern to match. Optional, default *.
Description

Note that like Tcl's info commands the pattern is interpreted relative to the current namespace. So for example, to obtain the list of commands within a namespace, the namespace must also be included in the pattern. For example,

cffi::help functions ::libzip::*
Return value

Returns a list of CFFI-wrapped functions matching the pattern PATTERN.

help struct [::cffi]Top, Main, Index

Returns a string describing field types in a Struct.

help struct NAME
Parameters
NAMEName of a Struct
Return value

Returns a string describing field types in a Struct.

help union [::cffi]Top, Main, Index

Returns a string describing field types in a Union.

help union NAME
Parameters
NAMEName of a Union
Return value

Returns a string describing field types in a Union.

limits [::cffi]Top, Main, Index

Get the lower and upper limits for an integral base type

limits type
Parameters
typeThe base type.
Return value

Returns a pair containing the minimum and maximum values for the specified type.

memory [::cffi]Top, Main, Index

A command ensemble.

memory subcommand ...
Description

The ensemble supports the following subcommands:

allocateAllocates memory of the specified size.
fillFills memory with a specified value.
freeFrees the memory referenced by the passed pointer.
frombinaryAllocates memory and copied the passed Tcl binary string into it.
fromstringAllocates memory and stores a Tcl string in it in the specified encoding.
fromunistringAllocates memory and stores a Tcl string as a sequence of Tcl_UniChars.
fromwinstringAllocates memory and stores a Tcl string as a sequence of winchars.
getConverts a native value in memory into a Tcl script level value.
get!Converts a native value in memory into a Tcl script level value.
newAllocates memory for a type and initializes it.
setConverts a value as per a type specification and stores it in memory in native form.
set!Converts a value as per a type specification and stores it in memory in native form.
tobinaryReturns the content of a memory block as a Tcl binary string.
tobinary!Returns the content of a memory block as a Tcl binary string.
tostringReturns the content of a memory block as a Tcl string.
tostring!Returns the content of a memory block as a Tcl string.
tounistringReturns the content of a memory block containing a nul-terminated sequence of Tcl_UniChars as a Tcl string.
tounistring!Returns the content of a memory block containing a nul-terminated sequence of Tcl_UniChars as a Tcl string.
towinstringReturns the content of a memory block containing a nul-terminated sequence of winchars as a Tcl string.
towinstring!Returns the content of a memory block containing a nul-terminated sequence of winchars as a Tcl string.

Refer to the documentation of each subcommand for details.

memory allocate [::cffi]Top, Main, Index

Allocates memory of the specified size

memory allocate sizespec ?tag?
Parameters
sizespecRequested size of memory block. This may be specified either as an integer value or a type specification.
tagTag for the returned pointer. Optional, default "".
Description

The returned memory must be eventually freed by calling memory free.

Return value

Returns a safe pointer to the allocated memory.

See also

memory new, memory free

memory fill [::cffi]Top, Main, Index

Fills memory with a specified value

memory fill pointer bytevalue count ?offset?
Parameters
pointerSafe pointer to memory.
bytevalueA value in the range 0-255
countNumber of bytes to be fill.
offsetOffset from $pointer of area to fill. May be negative for unsafe pointers. Optional, default 0.

memory free [::cffi]Top, Main, Index

Frees the memory referenced by the passed pointer

memory free pointer
Parameters
pointerSafe pointer to memory to free.
Description

The memory must have been allocated using memory allocate, memory frombinary, memory fromstring or one of the methods of a Struct object. Null pointers are silently ignored.

See also

memory allocate

memory frombinary [::cffi]Top, Main, Index

Allocates memory and copied the passed Tcl binary string into it.

memory frombinary bin_value ?tag?
Parameters
bin_valueA Tcl binary value.
tagTag for the returned pointer. Optional, default "".
Description

The returned memory must be eventually freed by calling memory free.

Return value

Returns a safe pointer to the allocated memory.

See also

memory tobinary, memory tobinary!

memory fromstring [::cffi]Top, Main, Index

Allocates memory and stores a Tcl string in it in the specified encoding.

memory fromstring value ?encoding?
Parameters
valueTcl string value.
encodingThe encoding to use for conversion. If unspecified or the empty string, the system encoding is used. Optional, default "".
Description

The returned memory must be eventually freed by calling memory free.

Return value

Returns a safe pointer to the allocated memory.

See also

memory tostring, memory tostring!

memory fromunistring [::cffi]Top, Main, Index

Allocates memory and stores a Tcl string as a sequence of Tcl_UniChars.

memory fromunistring value
Parameters
valueTcl string value.
Description

The returned memory must be eventually freed by calling memory free.

Return value

Returns a safe pointer to the allocated memory.

See also

memory tounistring, memory tounistring!

memory fromwinstring [::cffi]Top, Main, Index

Allocates memory and stores a Tcl string as a sequence of winchars.

memory fromwinstring value
Parameters
valueTcl string value.
Description

The returned memory must be eventually freed by calling memory free.

Return value

Returns a safe pointer to the allocated memory.

See also

memory towinstring, memory towinstring!

memory get [::cffi]Top, Main, Index

Converts a native value in memory into a Tcl script level value

memory get pointer typespec ?index?
Parameters
pointerBase address of memory location. The pointer must be a safe pointer but the tag is immaterial.
typespecType specification to use for conversion.
indexThe index in memory from which value is to be retrieved. Care must be taken this is within bounds of the allocated space. Optional, default 0.
Description

The command converts the data at a memory location into a Tcl script object as per the type specification $typespec. The memory address of the data is given by $pointer if $index is unspecified or 0. Otherwise $index is treated as an index into an array based at $pointer whose elements are of the type defined by $typespec and memory address used is that of the slot given by $index. For example, if $typespec is int and $index is 2, the memory address from which the value is read is at byte offset 8 assuming ints occupy 4 bytes. If $typespec is int[3], then the size of the type specification is 12, and an index of 2 will correspond to a byte offset of 24, not 8. Be aware of these semantics when reading arrays using non-0 indices.

See also

memory get!, memory set

memory get! [::cffi]Top, Main, Index

Converts a native value in memory into a Tcl script level value

memory get! pointer typespec ?index?
Parameters
pointerBase address of memory location. The pointer is not checked for validity.
typespecType specification to use for conversion.
indexThe index in memory from which value is to be retrieved. Care must be taken this is within bounds of the allocated space. Optional, default 0.
Description

This command is identical to the memory get command except that it does not require $pointer to be a safe pointer. See the documentation of that command for details.

See also

memory get, memory set

memory new [::cffi]Top, Main, Index

Allocates memory for a type and initializes it.

memory new typespec initializer ?tag?
Parameters
typespecA type declaration.
initializerThe type-specific value to use to initialize allocated memory.
tagThe optional tag for the returned pointer. Optional, default "".
Description

The $typespec argument may be any type specificiation. If an array is specified of a larger size than the number of elements in $initializer, the remaining elements are zeroed out.

The returned memory must be eventually freed by calling memory free.

Return value

Returns a safe pointer to the allocated memory.

See also

memory allocate, memory free

memory set [::cffi]Top, Main, Index

Converts a value as per a type specification and stores it in memory in native form

memory set pointer typespec value ?index?
Parameters
pointerBase address of memory location. The pointer must be a safe pointer but the tag is immaterial.
typespecType specification.
valueThe script level value to be stored.
indexThe index in memory at which the value is to be stored. Care must be taken this is within bounds of the allocated space. Optional, default 0.
Description

The command converts $value into the native form corresponding to the type specification $typespec. If $index is unspecified or 0, the native value is stored at the memory address given by $pointer. Otherwise $index is the treated as an index into an array whose elements are of the type defined by $typespec and the value is stored in the slot given by $index. For example, if $typespec is int and $index is 2, the memory address at which the value is written is at byte offset 8 assuming ints occupy 4 bytes. If $typespec is int[3], then the size of the type specification is 12, and an index of 2 will correspond to a byte offset of 24, not 8. Be aware of these semantics when writing arrays to non-0 indices to avoid memory corruption.

See also

memory get, memory set!

memory set! [::cffi]Top, Main, Index

Converts a value as per a type specification and stores it in memory in native form

memory set! pointer typespec value ?index?
Parameters
pointerBase address of memory location. The pointer is not checked for validity.
typespecType specification.
valueThe script level value to be stored.
indexThe index in memory at which the value is to be stored. Care must be taken this is within bounds of the allocated space. Optional, default 0.
Description

This command is identical to the memory set command except that it does not require $pointer to be a safe pointer. See the documentation of that command for details.

See also

memory get, memory set

memory tobinary [::cffi]Top, Main, Index

Returns the content of a memory block as a Tcl binary string.

memory tobinary pointer size ?offset?
Parameters
pointerSafe pointer to memory.
sizeNumber of bytes to copy from the memory block.
offsetOffset from $pointer of area to value. May be negative for unsafe pointers. Optional, default 0.
Return value

Returns the content of a memory block as a Tcl binary string.

See also

memory tobinary!, memory frombinary

memory tobinary! [::cffi]Top, Main, Index

Returns the content of a memory block as a Tcl binary string.

memory tobinary! pointer size ?offset?
Parameters
pointerPointer to memory.
sizeNumber of bytes to copy from the memory block.
offsetOffset from $pointer of area to value. May be negative for unsafe pointers. Optional, default 0.
Description

Unlike the memory tobinary method, this does not check the validity of $pointer and should be used with care.

Return value

Returns the content of a memory block as a Tcl binary string.

See also

memory tobinary, memory frombinary

memory tostring [::cffi]Top, Main, Index

Returns the content of a memory block as a Tcl string.

memory tostring pointer ?encoding? ?offset?
Parameters
pointerSafe pointer to memory.
encodingThe encoding to use for conversion. If unspecified or the empty string, the system encoding is used. Optional, default "".
offsetOffset from $pointer of area to value. May be negative for unsafe pointers. Optional, default 0.
Return value

Returns the content of a memory block as a Tcl string.

See also

memory tostring!, memory fromstring

memory tostring! [::cffi]Top, Main, Index

Returns the content of a memory block as a Tcl string.

memory tostring! pointer ?encoding? ?offset?
Parameters
pointerPointer to memory. Need not be a safe pointer.
encodingThe encoding to use for conversion. If unspecified or the empty string, the system encoding is used. Optional, default "".
offsetOffset from $pointer of area to value. May be negative. Optional, default 0.
Description

Unlike the memory tostring method, this does not check the validity of $pointer and should be used with care.

Return value

Returns the content of a memory block as a Tcl string.

See also

memory tostring!, memory fromstring

memory tounistring [::cffi]Top, Main, Index

Returns the content of a memory block containing a nul-terminated sequence of Tcl_UniChars as a Tcl string.

memory tounistring pointer ?offset?
Parameters
pointerSafe pointer to memory.
offsetOffset in Tcl_UniChar units from $pointer of area to value. May be negative for unsafe pointers. Optional, default 0.
Return value

Returns the content of a memory block containing a nul-terminated sequence of Tcl_UniChars as a Tcl string.

See also

memory tounistring!, memory fromunistring

memory tounistring! [::cffi]Top, Main, Index

Returns the content of a memory block containing a nul-terminated sequence of Tcl_UniChars as a Tcl string.

memory tounistring! pointer ?offset?
Parameters
pointerPointer to memory. Need not be a safe pointer.
offsetOffset in Tcl_UniChar units from $pointer of area to value. May be negative. Optional, default 0.
Return value

Returns the content of a memory block containing a nul-terminated sequence of Tcl_UniChars as a Tcl string.

See also

memory tounistring, memory fromunistring

memory towinstring [::cffi]Top, Main, Index

Returns the content of a memory block containing a nul-terminated sequence of winchars as a Tcl string.

memory towinstring pointer ?offset?
Parameters
pointerSafe pointer to memory.
offsetOffset in WCHAR units from $pointer of area to value. May be negative for unsafe pointers. Optional, default 0.
Return value

Returns the content of a memory block containing a nul-terminated sequence of winchars as a Tcl string.

See also

memory towinstring!, memory fromwinstring

memory towinstring! [::cffi]Top, Main, Index

Returns the content of a memory block containing a nul-terminated sequence of winchars as a Tcl string.

memory towinstring! pointer ?offset?
Parameters
pointerPointer to memory. Need not be a safe pointer.
offsetOffset in WCHAR units from $pointer of area to value. May be negative. Optional, default 0.
Return value

Returns the content of a memory block containing a nul-terminated sequence of winchars as a Tcl string.

See also

memory towinstring!, memory fromwinstring

pkgconfig [::cffi]Top, Main, Index

A command ensemble.

pkgconfig subcommand ...
Description

The ensemble supports the following subcommands:

getGets the value of a configuration key.
listReturns the list of keys containing information about the package configuration.

Refer to the documentation of each subcommand for details.

pkgconfig get [::cffi]Top, Main, Index

Gets the value of a configuration key.

pkgconfig get key
Parameters
keyOne of the keys returned by pkgconfig list command.
Description

The following keys are supported.

backendReturns libffi or dyncall indicating the backend FFI library in use.
versionThe package version.
compilerIdentifies the compiler used to build the extension.

pkgconfig list [::cffi]Top, Main, Index

Returns the list of keys containing information about the package configuration.

pkgconfig list
Return value

Returns the list of keys containing information about the package configuration.

pointer [::cffi]Top, Main, Index

A command ensemble.

pointer subcommand ...
Description

The ensemble supports the following subcommands:

addressReturns the address component of the pointer.
castCasts a pointer to a new tag.
castableSpecifies that pointers with tag $subtag are acceptable in declarations that require $supertag.
castablesReturns a list of tags that have been marked as castable with the pointer castable command..
checkValidates a pointer and raise an exception if invalid.
compareCompares if two pointers are the same.
countedRegisters a pointer as a counted pointer.
disposeUnregisters a safe or counted pointer.
infoReturns a dictionary containing registration information about a pointer.
invalidateUnregisters a safe, counted or pinned pointer irrespective its reference count.
isnullReturns true if the pointer is a NULL pointer, false otherwise.
isvalidValidates a pointer.
listReturns a list of registered pointers optionally filtered by a tag.
makeReturn a pointer for a memory address.
pinPins a pointer as permanently safe.
safeRegisters a pointer as a safe uncounted pointer.
tagReturns the pointer tag.
uncastableMakes pointers with the specified tag uncastable to another type.

Refer to the documentation of each subcommand for details.

pointer address [::cffi]Top, Main, Index

Returns the address component of the pointer.

pointer address pointer
Parameters
pointerPointer whose address component is to be returned.
Return value

Returns the address component of the pointer.

pointer cast [::cffi]Top, Main, Index

Casts a pointer to a new tag.

pointer cast pointer ?TAG?
Parameters
pointerPointer to be cast.
tagNew tag to apply. This will be qualified with the current scope if not fully qualified.
Description

The cast will fail if the TAG is not castable to or from the pointer's tag. See Casting pointers. In addition, the cast will fail if the pointer is registered as a safe pointer and the pointer's tag is not castable to the tag in the registration.

If TAG is not specified, any existing tag is removed from the pointer (effectively marking it as a void* pointer).

Return value

Returns a pointer with the same address and new tag

pointer castable [::cffi]Top, Main, Index

Specifies that pointers with tag $subtag are acceptable in declarations that require $supertag.

pointer castable subtags supertag
Parameters
subtagsList of tags which are acceptable wherever $supertag is expected. Will be qualified with current scope if necessary.
supertagTag which should accept $subtag tagged pointers. Will be qualified with current scope if necessary.
Description

See Casting pointers for more information.

pointer castables [::cffi]Top, Main, Index

Returns a list of tags that have been marked as castable with the pointer castable command..

pointer castables
Return value

Returns a list of tags that have been marked as castable with the pointer castable command..

pointer check [::cffi]Top, Main, Index

Validates a pointer and raise an exception if invalid.

pointer check pointer
Parameters
pointerPointer to be validated.
Description

For a pointer to be treated as valid,

If any of the above conditions is not met, an error exception is raised. Note that if $pointer is untagged, the tag of the registration is not checked.

See Pointers for more on pointer safety and registration.

pointer compare [::cffi]Top, Main, Index

Compares if two pointers are the same

pointer compare ptr1 ptr2
Parameters
ptr1The first pointer to compare.
ptr2The second pointer to compare.
Return value

Returns 1 if both address and tag components match, -1 if address matches but tags are different and 0 otherwise.

pointer counted [::cffi]Top, Main, Index

Registers a pointer as a counted pointer

pointer counted pointer
Parameters
pointerPointer to be registered.
Description

In the case that the pointer is already registered, behavior depends on the existing registration:

In the case that the pointer is registered with a different tag, if the pointer tag cannot be implicitly cast to the registered tag, the registered tag is replaced by the pointer tag.

See Pointers for more on counted pointers and registration.

Return value

Returns the registered pointer.

pointer dispose [::cffi]Top, Main, Index

Unregisters a safe or counted pointer

pointer dispose pointer
Parameters
pointerPointer to be unregistered.
Description

Note that the command only unregisters the pointer. It does not do anything in terms of releases any resources associated with the pointer.

An error is raised if the pointer is not registered unless it is a null pointer in which case it is ignored without error.

See Pointers for more on pointer safety and registration.

pointer info [::cffi]Top, Main, Index

Returns a dictionary containing registration information about a pointer.

pointer info pointer
Parameters
pointerPointer for which registration information is required.
Description

The returned dictionary always contains the keys Tag containing the pointer tag and Registration containing one of the values none, safe, pinned or counted. If the Registration value is not unregistered, the dictionary contains the additional keys RegisteredTag, holding the tag of the pointer in the registry, and Match which may have one of the values exact, derived or mismatch.

Return value

Returns a dictionary containing registration information about a pointer.

pointer invalidate [::cffi]Top, Main, Index

Unregisters a safe, counted or pinned pointer irrespective its reference count.

pointer invalidate pointer
Parameters
pointerPointer to be unregistered.
Description

Note that the command only unregisters the pointer. It does not do anything in terms of releases any resources associated with the pointer.

The command differs from pointer dispose in that

See Pointers for more on pointer safety and registration.

pointer isnull [::cffi]Top, Main, Index

Returns true if the pointer is a NULL pointer, false otherwise.

pointer isnull pointer
Parameters
pointerPointer to be checked.
Return value

Returns true if the pointer is a NULL pointer, false otherwise.

pointer isvalid [::cffi]Top, Main, Index

Validates a pointer.

pointer isvalid pointer
Parameters
pointerPointer to be validated.
Description

For a pointer to be treated as valid,

Return a boolean true value if all the above conditions are met, otherwise false.

See Pointers for more on pointer safety and registration.

pointer list [::cffi]Top, Main, Index

Returns a list of registered pointers optionally filtered by a tag.

pointer list ?tag?
Parameters
tagIf specified, only pointers matching the tag are returned. If tag is the empty string, only pointers without a tag are returned.
Return value

Returns a list of registered pointers optionally filtered by a tag.

pointer make [::cffi]Top, Main, Index

Return a pointer for a memory address.

pointer make address ?tag?
Parameters
addressMemory address as a positive integer.
tagIf not fully qualified, this will be qualified with the current namespace name. An empty tag is treated as if it was unspecified.
Description

The returned pointer is not registered as safe. The caller can use the pointer safe command to mark it as such if so desired.

pointer pin [::cffi]Top, Main, Index

Pins a pointer as permanently safe.

pointer pin pointer
Parameters
pointerPointer to be pinned. This may or may not be already registered.
Description

Once a pointer is pinned, it is unaffected by any pointer safe, pointer counted or pointer dispose calls. It can however be invalidated by pointer invalidate.

A pinned pointer has no associated tag even if the passed pointer has one. It will thus match any pointer with the same address component irrespective of the tag. The primary use of pinned pointers is to deal with "pseudo handles" in the Windows API.

Return value

Returns $pointer itself.

pointer safe [::cffi]Top, Main, Index

Registers a pointer as a safe uncounted pointer.

pointer safe pointer
Parameters
pointerPointer to be registered.
Description

In the case that the pointer is already registered, behavior depends on the existing registration:

See Pointers for more on pointer safety and registration.

Return value

Returns the registered pointer.

pointer tag [::cffi]Top, Main, Index

Returns the pointer tag

pointer tag pointer
Parameters
pointerPointer whose tag is to be returned.
Description

An empty string is returned if the pointer is not tagged.

See Pointers for more on pointer tags.

Return value

Returns the pointer tag

pointer uncastable [::cffi]Top, Main, Index

Makes pointers with the specified tag uncastable to another type

pointer uncastable tag
Parameters
tagA pointer tag Pointers tagged with $tag will no longer be castable to other tags that were configured with the pointer castables command.

prototype [::cffi]Top, Main, Index

A command ensemble.

prototype subcommand ...
Description

The ensemble supports the following subcommands:

deleteDeletes prototypes matching a pattern.
functionDefines a function prototype for calling a function using the default C calling convention.
listReturns a list of prototypes matching the specified pattern.
stdcallDefines a function prototype for calling a function using the stdcall calling convention.

Refer to the documentation of each subcommand for details.

prototype delete [::cffi]Top, Main, Index

Deletes prototypes matching a pattern.

prototype delete pattern
Parameters
patternPattern to match.
Description

The pattern matching algorithm is the same as that of Tcl's string match command. It is not an error if the pattern does not match any prototypes.

prototype function [::cffi]Top, Main, Index

Defines a function prototype for calling a function using the default C calling convention.

prototype function name fntype parameters
Parameters
nameName for the prototype. Must begin with an alphabetic character and may contain alphanumerics and underscore.
fntypeThe type definition for the return value from the function.
parametersList of alternating parameter name and type definitions.
Description

The $fntype and $parameters take the same form and have the same semantics as described for the Wrapper.function method for defining functions. However, unlike that method, this command does not create a command for invoking a C function. It only defines a prototype that can then be used in conjunction with a C function address to invoke that function.

See Prototypes and function pointers for more details.

See also

Wrapper.function

prototype list [::cffi]Top, Main, Index

Returns a list of prototypes matching the specified pattern.

prototype list ?pattern?
Parameters
patternPattern to match. Optional, default *.
Description

The pattern matching algorithm is the same as that of Tcl's string match command.

Return value

Returns a list of prototypes matching the specified pattern.

prototype stdcall [::cffi]Top, Main, Index

Defines a function prototype for calling a function using the stdcall calling convention.

prototype stdcall name fntype parameters
Parameters
nameName for the prototype. Must begin with an alphabetic character and may contain alphanumerics and underscore.
fntypeThe type definition for the return value from the function.
parametersList of alternating parameter name and type definitions.
Description

The $fntype and $parameters take the same form and have the same semantics as described for the Wrapper.stdcall method for defining functions. However, unlike that method, this command does not create a command for invoking a C function. It only defines a prototype that can then be used in conjunction with a C function address to invoke that function.

See Prototypes and function pointers for more details.

See also

Wrapper.stdcall

savederrors [::cffi]Top, Main, Index

Return errno and GetLastError() values from the last CFFI call annotated with saveerrors.

savederrors
Description

For functions annotated with saveerrors, CFFI internally saves the errno and (on Windows) GetLastError() values. The savederrors command retrieves these as a pair with the first element being the saved errno value and the second the GetLastError() result. The latter is always 0 on non-Windows platforms.

type [::cffi]Top, Main, Index

A command ensemble.

type subcommand ...
Description

The ensemble supports the following subcommands:

countReturns the count of elements in an array type.
frombinaryConverts the native binary value to its script level representation.
infoReturns a dictionary containing various information about a type declaration.
sizeReturns the size of a type in terms of the number of bytes of memory occupied by that type.
tobinaryConverts a script level value to a native representation.

Refer to the documentation of each subcommand for details.

type count [::cffi]Top, Main, Index

Returns the count of elements in an array type

type count type
Parameters
typeA type definition or alias.
Description

A return value of -1 indicates the type is not an array.

Return value

Returns the count of elements in an array type

type frombinary [::cffi]Top, Main, Index

Converts the native binary value to its script level representation.

type frombinary typespec value
Parameters
typespecA type declaration.
valueThe native binary value.
Description

The $typespec argument may be any type specification.

Return value

Returns the script level representation of the value.

See also

type tobinary

type info [::cffi]Top, Main, Index

Returns a dictionary containing various information about a type declaration

type info typedecl ?args?
Parameters
typedeclA type declaration.
-parsemode MODEOne of param, return, field.
-vlacount VLACOUNTNumber of elements in variable size array.
Description

The type is parsed as a parameter type declaration, a function return type declaration or a structure field type declaration depending on the -parsemode option. If this is unspecified or an empty string, it is parsed in generic fashion and no context-specific validation is done.

The option -vlacount must be specified if the type is variable length array or a struct that contains a variable length array

The returned dictionary contains the following keys:

AlignmentThe memory alignment needed for a value of that type.
BaseSizeThe number of bytes to store a single value of the type when the type is an array. For scalars, same as Size.
Count-1 for scalar values, else indicates the type is an array with that number of elements.
SizeThe number of bytes of memory needed to store a value of that type.
DefinitionThe type declaration after applying any defaults. For type aliases, this is the resolved type definition.
Return value

Returns a dictionary containing various information about a type declaration

type size [::cffi]Top, Main, Index

Returns the size of a type in terms of the number of bytes of memory occupied by that type.

type size type ?args?
Parameters
typeA type definition or alias.
-vlacount VLACOUNTNumber of elements in variable size array.
Description

In the case of array types, the size includes the size of the entire array and not the size of a single element.

The option -vlacount must be specified if the type is variable length array or a struct that contains a variable length array

Return value

Returns the size of a type in terms of the number of bytes of memory occupied by that type.

type tobinary [::cffi]Top, Main, Index

Converts a script level value to a native representation.

type tobinary typespec value
Parameters
typespecA type declaration.
valueThe script level value.
Description

The $typespec argument must not have a string or variable size component. If an array is specified of a larger size than the number of elements in $value, the remaining elements are zeroed out.

Return value

Returns a Tcl binary containing the native representation.

See also

type frombinary

ClassesTop, Main, Index

Interface [::cffi]Top, Main, Index

Method summary
constructorConstructor for the class.
idReturns the id for the interface as passed via the -id option in the interface definition.
methodsCreates Tcl commands wrapping the C functions comprising an interface.
stdmethodsCreates Tcl commands corresponding to the C functions comprising an interface.

constructor [::cffi::Interface]Interface, Top, Main, Index

Defines an interface consisting of method definitions.

Interface create OBJNAME ?args?
Interface new ?args?
Parameters
-id IDAn id to be associated with the interface. This is for the caller's use and CFFI does not make any use of it. For COM, this can be used to associate the GUID or IID for the interface. Defaults to the empty string.
-inherit BASEINTERFACEIf specified, the interface inherits methods from BASEINTERFACE. An empty value for BASEINTERFACE is treated as though the option was not specified.
Description

See Interfaces for more information.

id [::cffi::Interface]Interface, Top, Main, Index

Returns the id for the interface as passed via the -id option in the interface definition.

INTERFACEOBJ id
Return value

Returns the id for the interface as passed via the -id option in the interface definition.

methods [::cffi::Interface]Interface, Top, Main, Index

Creates Tcl commands wrapping the C functions comprising an interface.

INTERFACEOBJ methods methodlist ?args?
Parameters
methodlistList of function definitions.
-disposemethod METHODNAMDEThe name of the method, if any, whose implicit pointer parameter should be annotated with dispose.
Description

The C functions in the wrapper are presumed to be using the _cdecl calling convention. The created commands are of the form INTERFACENAME.METHODNAME. The first argument to each is typed as pointer.INTERFACENAME.

The order of the methods in $methodlist must be the same as that in the corresponding C interface. All functions in the interface are expected to have a pointer to the interface as the first parameter.

The $methodlist argument is a flat (not nested) list of function name, return type and parameter list in the same form as described for the ::cffi::Wrapper::function method except that the first parameter in the C function, the pointer to the object, should not be included.

See also

stdmethods

stdmethods [::cffi::Interface]Interface, Top, Main, Index

Creates Tcl commands corresponding to the C functions comprising an interface.

INTERFACEOBJ stdmethods methodlist ?args?
Parameters
methodlistList of function definitions.
-disposemethod METHODNAMDEThe name of the method, if any, whose implicit pointer parameter should be annotated with dispose.
Description

The C functions are presumed to be using the _stdcall calling convention. The created commands are of the form INTERFACENAME.METHODNAME. The first argument to each is typed as pointer.INTERFACENAME.

The order of the methods in $methodlist must be the same as that in the corresponding C interface. All functions in the interface are expected to have a pointer to the interface as the first parameter.

The $methodlist argument is a flat (not nested) list of function name, return type and parameter list in the same form as described for the ::cffi::Wrapper::function method except that the first parameter in the C function, the pointer to the object, should not be included.

See also

methods

Struct [::cffi]Top, Main, Index

Method summary
constructorConstructor for the class.
allocateAllocates memory for one or more C structs.
describeReturns a human-readable description of the C struct definition.
fieldpointerReturns a pointer corresponding to the address of a field within a native structure.
freeFrees memory that was allocated for a native C struct.
frombinaryDecodes a Tcl binary string containing a native C struct into a Tcl dictionary.
fromnativeDecodes native C struct(s) in memory into a Tcl dictionary.
fromnative!Decodes native C struct(s) in memory into a Tcl dictionary.
getnativeReturns the value of a field in a native structure in memory.
getnative!Returns the value of a field in a native structure in memory.
getnativefieldsRetrieve values of multiple fields from a native struct in memory.
getnativefields!Retrieve values of multiple fields from a native struct in memory.
infoReturns a dictionary containing information about the struct layout.
nameReturns the name of the struct.
newAllocates and initializes a native struct in memory.
setnativeSets the value of a field in a native structure in memory.
setnative!Sets the value of a field in a native structure in memory.
sizeReturns the size of the struct.
tobinaryEncodes the Tcl representation of a C struct value into a Tcl binary string.
tonativeWrites the Tcl dictionary representation of a C struct value to memory in native form.
tonative!Writes the Tcl dictionary representation of a C struct value to memory in native form.

constructor [::cffi::Struct]Struct, Top, Main, Index

Constructs a script level object that maps to a C struct definition.

Struct create OBJNAME definition ?args?
Struct new definition ?args?
Parameters
definitionDefines the layout and type of the C struct fields.
-clearClears memory at initialization. See below.
-pack PACKVALUEChanges alignment and padding used for fields. See Structs.
Description

The created object can be used to pass arguments to and from functions, allocate memory, encode and decode native C structs in memory etc.

The $definition argument is a dictionary mapping field names to the corresponding types. See Packed structs for more information.

The name of the created struct is the same as the name of the returned object without the initial :: global namespace qualifier. This name can be retrieved with the name method and is used to identify the struct when tagging pointers and for typing function parameters and nested struct fields.

If the -clear option is present, the memory of structures of this type is cleared before the field values are initialized. In this case, missing field values in a struct value will not result in an error being raised even if no default annotation is included for the field. This also effectively provides a default zero value for all fields.

allocate [::cffi::Struct]Struct, Top, Main, Index

Allocates memory for one or more C structs.

STRUCTOBJ allocate ?args?
Parameters
-count COUNTNumber of structs to allocate.
-vlacount VLACOUNTNumber of elements in variable size array.
Description

Unlike in the new method, the allocated memory is not initialized. This is the case even if the struct definition includes the -clear option.

The option -vlacount must be specified if the struct contains a field that is a variable length array (VLA) or a nested struct that contains a VLA. VLACOUNT is then length of that array.

The allocate memory must be freed by calling the free method of the struct.

Return value

Returns a safe pointer to the allocated memory tagged with the struct name.

describe [::cffi::Struct]Struct, Top, Main, Index

Returns a human-readable description of the C struct definition.

STRUCTOBJ describe
Description

This is primarily for debugging and troubleshooting purposes. Note the returned information will not take into account the variable components of the struct if any.

Return value

Returns a human-readable description of the C struct definition.

fieldpointer [::cffi::Struct]Struct, Top, Main, Index

Returns a pointer corresponding to the address of a field within a native structure

STRUCTOBJ fieldpointer pointer fieldname ?tag? ?index?
Parameters
pointerPointer to memory allocated for the C struct or array. Must be a safe pointer tagged with the struct name.
fieldnameName of the field.
tagTag for the returned pointer. Optional, default "".
indexIf present, $pointer is interpreted as pointing to an array of structs and this is the index into that array. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Description

Note the returned pointer is not registered as a safe pointer.

Return value

Returns a pointer corresponding to the address of a field within a native structure

free [::cffi::Struct]Struct, Top, Main, Index

Frees memory that was allocated for a native C struct.

STRUCTOBJ free pointer
Parameters
pointerA pointer returned by allocate

frombinary [::cffi::Struct]Struct, Top, Main, Index

Decodes a Tcl binary string containing a native C struct into a Tcl dictionary.

STRUCTOBJ frombinary bin_value
Parameters
bin_valueA Tcl binary value containing the C struct.
Return value

Returns the dictionary representation.

fromnative [::cffi::Struct]Struct, Top, Main, Index

Decodes native C struct(s) in memory into a Tcl dictionary.

STRUCTOBJ fromnative pointer ?index?
Parameters
pointerSafe pointer to the C struct or array of structs in memory.
indexThe index position at which the struct is to be written. Note this interpreted as an index into an array of structs, not as a byte offset into memory. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Description

The decoded dictionary or dictionaries are keyed by the field names of the struct. For fields containing variable length arrays, the field holding the length of the array must be explicitly specified and cannot be defaulted.

Return value

Returns a dictionary of the decoded struct.

See also

fromnative!, tonative, frombinary

fromnative! [::cffi::Struct]Struct, Top, Main, Index

Decodes native C struct(s) in memory into a Tcl dictionary.

STRUCTOBJ fromnative! pointer ?index?
Parameters
pointerSafe or unsafe pointer to the C struct or array of structs in memory.
indexThe index position at which the struct is to be written. Note this interpreted as an index into an array of structs, not as a byte offset into memory. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Description

The decoded dictionary or dictionaries are keyed by the field names of the struct. For fields containing variable length arrays, the field holding the length of the array must be explicitly specified and cannot be defaulted.

Return value

Returns a dictionary of the decoded struct.

See also

fromnative, tonative, frombinary

getnative [::cffi::Struct]Struct, Top, Main, Index

Returns the value of a field in a native structure in memory

STRUCTOBJ getnative pointer fieldname ?index?
Parameters
pointerSafe pointer to memory allocated for the C struct or array. Must be tagged with the struct name.
fieldnameName of the field.
indexIf present, $pointer is interpreted as pointing to an array of structs and this is the index into that array. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Description

In the case of fields of type pointer, the returned pointer is registered as a safe pointer unless the field was marked with the unsafe annotation.

Return value

Returns the value of a field in a native structure in memory

getnative! [::cffi::Struct]Struct, Top, Main, Index

Returns the value of a field in a native structure in memory

STRUCTOBJ getnative! pointer fieldname ?index?
Parameters
pointerSafe or unsafe pointer to memory allocated for the C struct or array. Must be tagged with the struct name.
fieldnameName of the field.
indexIf present, $pointer is interpreted as pointing to an array of structs and this is the index into that array. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Description

In the case of fields of type pointer, the returned pointer is registered as a safe pointer unless the field was marked with the unsafe annotation.

Return value

Returns the value of a field in a native structure in memory

getnativefields [::cffi::Struct]Struct, Top, Main, Index

Retrieve values of multiple fields from a native struct in memory.

STRUCTOBJ getnativefields pointer fieldnames ?index?
Parameters
pointerPointer to memory allocated for the C struct or array. Must be tagged with the struct name.
fieldnamesList of field names.
indexIf present, $pointer is interpreted as pointing to an array of structs and this is the index into that array. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Return value

Returns a list of field values in the same order as $fieldnames

getnativefields! [::cffi::Struct]Struct, Top, Main, Index

Retrieve values of multiple fields from a native struct in memory.

STRUCTOBJ getnativefields! pointer fieldnames ?index?
Parameters
pointerSafe or unsafe pointer to memory allocated for the C struct. Must be tagged with the struct name.
fieldnamesList of field names.
indexIf present, $pointer is interpreted as pointing to an array of structs and this is the index into that array. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Return value

Returns a list of field values in the same order as $fieldnames

info [::cffi::Struct]Struct, Top, Main, Index

Returns a dictionary containing information about the struct layout

STRUCTOBJ info ?args?
Parameters
-vlacount VLACOUNTNumber of elements in variable size array.
Description

The returned dictionary has the following fields:

SizeSize of the struct.
AlignmentStruct alignment.
FieldsDictionary of fields keyed by field name. The value is another nested dictionary with keys Size, Offset, and Definition containing the size, offset in struct, and type definition of the field.

The option -vlacount must be specified if the struct contains a field that is a variable length array (VLA) or a nested struct that contains a VLA. VLACOUNT is then length of that array.

Return value

Returns a dictionary containing information about the struct layout

name [::cffi::Struct]Struct, Top, Main, Index

Returns the name of the struct.

STRUCTOBJ name
Description

Note the name of the struct is different from the name of the object name.

Return value

Returns the name of the struct.

new [::cffi::Struct]Struct, Top, Main, Index

Allocates and initializes a native struct in memory.

STRUCTOBJ new ?initval?
Parameters
initvalInitial value for the struct as a dictionary mapping field names to values. Optional, default "".
Description

If no argument is supplied or fields are missing, defaults from the struct definition are used for initialization. An error is raised if any fields are not defaulted unless the -clear option was specified for the Struct. In that case missing fields without defaults are filled with zeroes.

For fields containing variable length arrays, the field holding the length of the array must be explicitly specified and cannot be defaulted.

The allocated memory must be freed by calling the free method for the struct.

Return value

Returns a safe pointer to the allocated memory tagged with the struct name.

setnative [::cffi::Struct]Struct, Top, Main, Index

Sets the value of a field in a native structure in memory

STRUCTOBJ setnative pointer fieldname value ?index?
Parameters
pointerSafe pointer to memory allocated for the C struct or array. Must be tagged with the struct name.
fieldnameName of the field.
valueValue to store in the field.
indexIf present, $pointer is interpreted as pointing to an array of structs and this is the index into that array. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Description

The field being set must not be a variable sized field, or field holding a count for a variable sized field; otherwise, an error is raised.

setnative! [::cffi::Struct]Struct, Top, Main, Index

Sets the value of a field in a native structure in memory

STRUCTOBJ setnative! pointer fieldname value ?index?
Parameters
pointerSafe or unsafe pointer to memory allocated for the C struct or array. Must be tagged with the struct name.
fieldnameName of the field.
valueValue to store in the field.
indexIf present, $pointer is interpreted as pointing to an array of structs and this is the index into that array. For variable sized structs index must be 0 or unspecified. Optional, default 0.
Description

The field being set must not be a variable sized field, or field holding a count for a variable sized field; otherwise, an error is raised.

size [::cffi::Struct]Struct, Top, Main, Index

Returns the size of the struct.

STRUCTOBJ size ?args?
Parameters
-vlacount VLACOUNTNumber of elements in variable size array.
Description

The option -vlacount must be specified if the struct contains a field that is a variable length array (VLA) or a nested struct that contains a VLA. VLACOUNT is then length of that array.

Return value

Returns the size of the struct.

tobinary [::cffi::Struct]Struct, Top, Main, Index

Encodes the Tcl representation of a C struct value into a Tcl binary string.

STRUCTOBJ tobinary dict_value
Parameters
dict_valueA Tcl dictionary representation of a C struct value.
Return value

Returns the binary string containing the native C struct.

tonative [::cffi::Struct]Struct, Top, Main, Index

Writes the Tcl dictionary representation of a C struct value to memory in native form.

STRUCTOBJ tonative pointer initializer ?index?
Parameters
pointerSafe pointer to memory allocated for the C struct or array. Must be tagged with the struct name.
initializerThe Tcl dictionary value.
indexThe index position at which the struct is to be written. Note this interpreted as an index into an array of structs, not as a byte offset into memory. Optional, default 0.
Description

The method raises an error if the struct is variable size.

See also

tonative!, fromnative, tobinary

tonative! [::cffi::Struct]Struct, Top, Main, Index

Writes the Tcl dictionary representation of a C struct value to memory in native form.

STRUCTOBJ tonative! pointer initializer ?index?
Parameters
pointerSafe or unsafe pointer to memory allocated for the C struct or array. Must be tagged with the struct name.
initializerThe Tcl dictionary value.
indexThe index position at which the struct is to be written. Note this interpreted as an index into an array of structs, not as a byte offset into memory. Optional, default 0.
Description

The method raises an error if the struct is variable size.

See also

tonative, fromnative, tobinary

Union [::cffi]Top, Main, Index

Method summary
constructorConstructor for the class.
decodeReturns a field from a native union.
describeReturns a human-readable description of the C union definition.
encodeReturns a union in native form with the specified field value.
infoReturns a dictionary containing information about the info layout.
nameReturns the name of the struct.
sizeReturns the size of the union.

constructor [::cffi::Union]Union, Top, Main, Index

Constructs a script level object that maps to a C union definition.

Union create OBJNAME definition ?args?
Union new definition ?args?
Parameters
definitionDefines the layout and type of the C struct fields.
argsOptions. Currently the only option is -clear
Description

The created object can be used to manipulate native C unions.

The $definition argument is a dictionary mapping field names to the corresponding types. This has the same syntax as Structs.

The name of the created struct is the same as the name of the returned object without the initial :: global namespace qualifier. This name can be retrieved with the name method and is used to identify the struct when tagging pointers and for typing function parameters and nested struct fields.

If the -clear option is present, the memory of unions of this type is cleared before the field values are initialized.

decode [::cffi::Union]Union, Top, Main, Index

Returns a field from a native union.

UNIONOBJ decode field uvalue
Parameters
fieldThe field within the union that should be retrieved.
uvalueThe opaque union in native form.
Return value

Returns a field from a native union.

See also

encode

describe [::cffi::Union]Union, Top, Main, Index

Returns a human-readable description of the C union definition.

UNIONOBJ describe
Description

This is primarily for debugging and troubleshooting purposes.

Return value

Returns a human-readable description of the C union definition.

encode [::cffi::Union]Union, Top, Main, Index

Returns a union in native form with the specified field value.

UNIONOBJ encode field value
Parameters
fieldThe field within the union that should be set.
valueThe value of the field.
Return value

Returns a union in native form with the specified field value.

See also

decode

info [::cffi::Union]Union, Top, Main, Index

Returns a dictionary containing information about the info layout

UNIONOBJ info
Description

The returned dictionary has the following fields:

SizeSize of the struct.
AlignmentStruct alignment.
FieldsDictionary of fields keyed by field name. The value is another nested dictionary with keys Size, Offset, and Definition containing the size, offset in struct, and type definition of the field.

Other fields may be present and should be ignored.

Return value

Returns a dictionary containing information about the info layout

name [::cffi::Union]Union, Top, Main, Index

Returns the name of the struct.

UNIONOBJ name
Description

Note the name of the struct may be different from the name of the object command.

Return value

Returns the name of the struct.

size [::cffi::Union]Union, Top, Main, Index

Returns the size of the union.

UNIONOBJ size
Return value

Returns the size of the union.

Wrapper [::cffi]Top, Main, Index

Method summary
constructorConstructor for the class.
destructorDestructor for the class.
addressofReturns the address of a symbol from the loaded library.
functionCreates a Tcl command to invoke a C function in the loaded library.
functionsCreates Tcl commands for multiple C functions within the loaded library.
pathReturns the file system path for the wrapped library or image wrapped by the object.
stdcallCreates a Tcl command to invoke a C function that uses the __stdcall calling convention from the loaded library..
stdcallsCreates Tcl commands for multiple C functions within the loaded library that all use the __stdcall calling convention.

constructor [::cffi::Wrapper]Wrapper, Top, Main, Index

Wraps a shared library / DLL, loading it in the process.

Wrapper create OBJNAME path
Wrapper new path
Parameters
pathPath to the shared library.
Description

It is strongly recommended that the path to the shared library be specified as an absolute path. Otherwise, it is located in a system-specific manner involving operating system version, environment variables, registry settings (on Windows), phase of the moon etc.. This is not advisable for both reliability and security reasons.

When no longer needed, the object can be deleted in the usual manner (via rename to an empty string) or by invoking its destroy method.

Return value

Returns the name of the created object.

destructor [::cffi::Wrapper]Wrapper, Top, Main, Index

Destroys the object and releases internal resources.

WRAPPEROBJ destroy
Description

The managed library may be unloaded if there is nothing else holding a reference to it.

addressof [::cffi::Wrapper]Wrapper, Top, Main, Index

Returns the address of a symbol from the loaded library.

WRAPPEROBJ addressof symbol
Parameters
symbolName of the symbol.
Description

The command will raise an error if the symbol is not found.

Return value

Returns the address of a symbol from the loaded library.

function [::cffi::Wrapper]Wrapper, Top, Main, Index

Creates a Tcl command to invoke a C function in the loaded library.

WRAPPEROBJ function fnname fntype parameters
Parameters
fnnameName of the function and optionally, Tcl command.
fntypeThe type definition for the return value from the function.
parametersList of alternating parameter name and type definitions.
Description

The command creates a Tcl command corresponding to a C function contained in the library. The function must be one that follows the default C calling convention (See Calling conventions).

The $fnname argument is a one or two element list, the first being the name of the C function and the second, if present, being the name of the corresponding Tcl command. The latter defaults to the former if unspecified. Unless fully qualified, the command is created in the namespace from which it is called.

The return type of the C function is specified through the $fntype argument which should be a type declaration. See syntax.

The $parameters argument is a list of alternating parameter names and type declarations that describe the parameters of the C function. The parameter name is only used to construct error messages while the latter determines how arguments are converted and passed to the C function. See syntax.

The return type as well as parameter type declarations may have annotations that control semantics and behavior with respect to how values are passed and converted between Tcl and C. See Functions for details of these.

The command will raise an error if the function identified by $cname is not found in the loaded DLL.

Return value

Returns the name of the created Tcl command.

functions [::cffi::Wrapper]Wrapper, Top, Main, Index

Creates Tcl commands for multiple C functions within the loaded library.

WRAPPEROBJ functions fnlist ?args?
Parameters
fnlistList of function definitions.
-ignoremissingDo not raise an error if a function is not found.
Description

This is a wrapper around the function method that provides some syntactic sugar for defining multiple functions. The $fnlist argument is a flat (not nested) list of function name, return type and parameter list in the same form as described for the function method.

If a function is not found, the command will raise an error but those functions that are found will be defined. The error can be completely suppressed with the -ignoremissing option.

If the function name element is #, the triple is ignored, effectively being considered a comment.

path [::cffi::Wrapper]Wrapper, Top, Main, Index

Returns the file system path for the wrapped library or image wrapped by the object.

WRAPPEROBJ path
Description

The returned path may be or may not be normalized and may be in native form or Tcl's canonical form. If no path argument was provided to the constructor, an empty string may be returned on some platforms.

Return value

Returns the file system path for the wrapped library or image wrapped by the object.

stdcall [::cffi::Wrapper]Wrapper, Top, Main, Index

Creates a Tcl command to invoke a C function that uses the __stdcall calling convention from the loaded library..

WRAPPEROBJ stdcall fnname fntype parameters
Parameters
fnnameName of the function and optionally, Tcl command.
fntypeThe type definition for the return value from the function.
parametersList of alternating parameter name and type definitions.
Description

The command creates a Tcl command corresponding to a C function contained in the library. The function must be one that follows the __stdcall calling convention (See Calling conventions). The only platform where this differs from the C calling convention is 32-bit Windows. On other platforms, this method is a synonym for function.

See function for other details.

Return value

Returns the name of the created Tcl command.

stdcalls [::cffi::Wrapper]Wrapper, Top, Main, Index

Creates Tcl commands for multiple C functions within the loaded library that all use the __stdcall calling convention.

WRAPPEROBJ stdcalls fnlist ?args?
Parameters
fnlistList of function definitions.
-ignoremissingDo not raise an error if a function is not found.
Description

This is a wrapper around the stdcall method that provides some syntactic sugar for defining multiple functions. The $fnlist argument is a flat (not nested) list of function name, return type and parameter list in the same form as described for the stdcall method.

If a function is not found, the command will raise an error but those functions that are found will be defined. The error can be completely suppressed with the -ignoremissing option.

If the function name element is #, the triple is ignored, effectively being considered a comment.