This doc is a work in progress.
The ctypes
object is the base of the ctypes API. It is obtained by by loading the ctypes module:
Components.utils.import("resource://gre/modules/ctypes.jsm");
You can use the ctypes
object to load libraries, construct types, and perform miscellaneous tasks such as type casting. The
object also provides numerous predefined types that correspond to primitives and common typedefs in C.ctypes
To load a Library
, use ctypes.open()
.
The Library
object is used mostly to declare native functions provided by the library so that js-ctypes knows how to call them. See Library.declare()
for instructions on how to declare these functions.
Once you have finished working with a Library, call Library.close()
.
See ABI
.
To use js-ctypes effectively, it is important to understand the different kinds of objects that the module provides. There are three major categories:
These represent a type in C, and are thus represented by CType
objects. These objects have two main purposes. First, they provide a concrete representation of different data types, allowing the programmer to describe the arguments and return type of a native function (see Library.declare()
). Second, they serve as constructors to create concrete instances of the given type, i.e. CData
objects.
Out of the box, js-ctypes supports a number of predefined types.
These are functions that define new types, and thus return a CType
object. These include
ctypes.PointerType(type)
and the .ptr
property, which creates a new type describing a pointer to an existing type.ctypes.ArrayType(type, [length])
and the .array()
method, which creates a new type describing an array of objects of an existing type.ctypes.StructType()
, which creates a new type describing a C struct.ctypes.FunctionType(abi, returnType, argType1 [, argType2 ...])
, which creates a new type describing a C function.These are instances of types, represented by CData
objects and instantiated by calling CType
objects as functions.
These distinctions are crucial, and understanding them will alleviate much of the confusion surrounding js-ctypes. Calling a Type Constructor will return a CType
. Calling a CType
will return a CData
. You declare the arguments and return value of a native function with CType
objects. You call a native function with CData
objects.
js-ctypes supports both errno (on all platforms) and GetLastError (on Windows platforms).
You can pass regular JavaScript functions as callbacks to native functions. See the Callbacks page for details.
Because JavaScript Number
objects can't directly represent every possible 64-bit integer value, js-ctypes provides new types for these. See Int64
and UInt64
.