Tcl CFFI package (v1.0.7)

::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 calling scope.

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

The C predefined alias set includes 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.

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_valueNot documented.
error_resultThe 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:

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.
sequenceDefines an enumeration with consecutive member values.
unmaskReturns a list of enumeration member names corresponding to the bits set in $mask. enumeration members.
valueReturns the value of an enumeration member.

Refer to the documentation of each subcommand for details.

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.

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.

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 sequence [::cffi]Top, Main, Index

Defines an enumeration with consecutive member values

enum sequence enumname membernames start 0
Parameters
enumnameName of the enumeration.
membernamesList of names of the enumeration members.
start 0Not documented.
startValue of first enumeration member.

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

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

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. enumeration members

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:

functionReturns a string describing the syntax for a CFFI wrapped function.
functionsReturns a list of CFFI-wrapped functions matching the pattern FUNCPATTERN

Refer to the documentation of each subcommand for details.

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

Returns a string describing the syntax for a CFFI wrapped function

help function FUNCNAME
Parameters
FUNCNAMEName of a wrapped function.
Return value

Returns a string describing the syntax for a CFFI wrapped function

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

Returns a list of CFFI-wrapped functions matching the pattern FUNCPATTERN

help functions ?FUNCPATTERN?
Parameters
FUNCPATTERNGlob 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 FUNCPATTERN

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 locations to 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.
getConverts a native value in memory into a Tcl script level value.
get!Converts a value as per a type specification and stores it in memory in native form.
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.

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 locations to a specified value

memory fill pointer bytevalue count
Parameters
pointerSafe pointer to memory.
bytevalueA value in the range 0-255
countNumber of bytes to be fill.

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 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 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 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 value as per a type specification and stores it in memory in native form

memory get! pointer typespec ?index?
Parameters
pointerBase address of memory location. The pointer is not checked for validity.
typespecNot documented.
indexNot documented. 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
Parameters
pointerSafe pointer to memory.
sizeNumber of bytes to copy from the memory block.
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
Parameters
pointerPointer to memory.
sizeNumber of bytes to copy from the memory block.
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?
Parameters
pointerSafe pointer to memory.
encodingThe encoding to use for conversion. If unspecified or the empty string, the system encoding is used. Optional, default "".
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?
Parameters
pointerPointer to memory.
encodingThe encoding to use for conversion. If unspecified or the empty string, the system encoding is used. Optional, default "".
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

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.
countedRegisters a pointer as a counted pointer.
disposeUnregisters a safe or counted pointer.
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.
safeRegisters a pointer as a safe uncounted pointer.
tagReturns the pointer tag.

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.

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 subtag supertag
Parameters
subtagTag which is to be marked as 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 counted [::cffi]Top, Main, Index

Registers a pointer as a counted pointer

pointer counted pointer
Parameters
pointerPointer to be registered.
Description

A counted pointer may be registered multiple times. An error is raised if the pointer is NULL or already registered as an uncounted pointer.

See Pointers for more on counted pointers and registration.

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 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 ?args?
Parameters
argsIf specified, must be a single argument which is a tag. Only pointers matching the 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.
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 safe [::cffi]Top, Main, Index

Registers a pointer as a safe uncounted pointer

pointer safe pointer
Parameters
pointerPointer to be registered.
Description

An error is raised if the pointer is already registered or is a NULL pointer.

See Pointers for more on pointer safety and registration.

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

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

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.
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.

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 0 indicates the type is not an array.

Return value

Returns the count of elements in an array type

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

Returns a dictionary containing various information about a type declaration

type info typedecl ?parse_mode?
Parameters
typedeclA type declaration.
parse_modeOne of param, return, field. Optional, default "".
Description

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

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.
Count0 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
Parameters
typeA type definition or alias.
Description

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

Return value

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

ClassesTop, Main, Index

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.
getnativefieldsRetrieve 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.
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.

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.
argsOptions. Currently the only option is -clear
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 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.

OBJECT allocate ?count?
Parameters
countNumber of structs to allocate. Optional, default 1.
Description

The allocated memory is not initialized even if the struct definition includes the -clear option.

It must be freed by calling the free method.

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.

OBJECT describe
Description

This is primarily for debugging and troubleshooting purposes.

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

OBJECT 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. 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.

OBJECT 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.

OBJECT 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.

OBJECT 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. Optional, default 0.
Description

The decoded dictionary or dictionaries are keyed by the field names of the struct.

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.

OBJECT fromnative! pointer ?index?
Parameters
pointerUnsafe 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. Optional, default 0.
Description

The decoded dictionary or dictionaries are keyed by the field names of the struct.

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

OBJECT getnative pointer fieldname ?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.
indexIf present, $pointer is interpreted as pointing to an array of structs and this is the index into that array. 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.

OBJECT getnativefields pointer fieldnames ?index?
Parameters
pointerPointer to memory allocated for the C struct or array. Must be a safe pointer 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. 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

OBJECT 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.
Return value

Returns a dictionary containing information about the struct layout

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

Returns the name of the struct.

OBJECT 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.

OBJECT 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.

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

OBJECT setnative pointer fieldname value ?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.
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. Optional, default 0.

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

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

OBJECT 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.

OBJECT tonative pointer initializer ?index?
Parameters
pointerPointer 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.
See also

fromnative, tobinary

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.

OBJECT 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.

OBJECT 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.

OBJECT 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.

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.

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.

OBJECT functions fnlist
Parameters
fnlistList of function definitions.
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.

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

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

OBJECT 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..

OBJECT 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.

OBJECT stdcalls fnlist
Parameters
fnlistList of function definitions.
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.