Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Draft
This page is not complete.

The ctypes object contains methods and predefined data types used to create and manage a library.

Method overview

CType ArrayType(type[, length]);
CData cast(data, type);
CType FunctionType(abi, returnType[, argType1, ...]);
CData Int64(n);
String libraryName(name);
Library open(libSpec);
CType PointerType(typeSpec);
CType StructType(name[, fields]);
CData UInt64(n);

Properties

Property Type Description
errno Number The value of the latest system error. Similar to errno in libc, available on all platforms. Cannot be set.
winLastError Number|undefined The value of the latest Windows error. Similar to GetLastError under Windows, available only for Windows. Cannot be set.

Constants

ABI constants

These constants are used to specify the ABI used to call a native function in the library.

Constant Description
default_abi Corresponds to cdecl; standard libraries use this ABI. You also use this for all system calls on Mac OS X and Linux.
stdcall_abi Used for calling functions declared with stdcall on Windows. These functions' names are automatically mangled for you by js-ctypes.
winapi_abi Used for calling Windows system functions. These are declared as stdcall on Windows, but do not have mangled names like those used by stdcall_abi above.

Predefined data types

Primitive types

These types behave the same on all platforms.

Type Description
int8_t Signed 8-bit integer.
uint8_t Unsigned 8-bit integer.
int16_t Signed 16-bit integer.
uint16_t Unsigned 16-bit integer.
int32_t Signed 32-bit integer.
uint32_t Unsigned 32-bit integer.
int64_t Signed 64-bit integer.
uint64_t Unsigned 64-bit integer.
float32_t 32-bit floating-point value.
float64_t 64-bit floating-point value.
Note: Some 64-bit values are outside the range of numeric values supported by JavaScript. Because of this, ctypes.int64 and ctypes.uint64 do not automatically convert to JavaScript numbers. Instead, they convert to objects of the wrapper types ctypes.Int64 and ctypes.UInt64, which are JavaScript objects rather than CData objects. See 64-bit integers for details.

Types that act like specific C types

These types are designed to work exactly like the corresponding type on the native platform.

Type Description
bool A Boolean type that behaves like the corresponding C type on the platform.
short A short integer type that behaves like the corresponding C type on the platform.
unsigned_short An unsigned short integer type that behaves like the corresponding C type on the platform.
int An integer type that behaves like the int C type on the platform.
unsigned_int An unsigned integer type that behaves like the unsigned int C type on the platform.
long

A long integer type that behaves like the corresponding C type on the platform.

NOTE This automatically converts to an Int64 JavaScript object on all platforms, since it's unknown whether this is a 32-bit or 64-bit value. This is done for compatibility's sake.

unsigned_long

An unsigned long integer type that behaves like the corresponding C type on the platform.

NOTE This automatically converts to a UInt64 JavaScript object on all platforms, since it's unknown whether this is a 32-bit or 64-bit value. This is done for compatibility's sake.

long_long A integer type at least 64 bits wide.
unsigned_long_long An unsigned integer type at least 64 bits wide.
float A floating point value that behaves like the float type on the platform.
double A double-precision floating point value that behaves like the double type on the platform.

Character types

Character types are 8-bit values that behave like their C counterparts. They're similar to the int8_t and uint8_t types, except conversion is handled differently.

For example, ctypes.char.array(30)(str) converts the string str to UTF-8 and returns a new CData object of array type.

Type Description
char

A character type that behaves like the char C type on the platform.

signed_char A signed character that behaves like the char type on the platform.
unsigned_char An unsigned character that behaves like the unsigned char type on the platform.

Types whose size varies depending on platform

Because it's unknown whether these values will be 32-bit or 64-bit, they are not automatically converted into JavaScript numbers. Instead, these convert into ctypes.Int64 or ctypes.UInt64 JavaScript objects; see 64-bit integers for details.

Type Description
size_t A platform-dependent size type.
ssize_t A platform-dependent size type.
intptr_t A platform-dependent integer representation of a pointer.
uintptr_t A platform-dependent unsigned integer representation of a pointer.

JavaScript characters

16-bit C Unicode characters are handled by js-ctypes using the jschar type.

Type Description
jschar A 16-bit unsigned character. This is different from uint16_t in that C jschar values are automatically converted into 1-character JavaScript strings. These are Unicode characters.

Special C types

These types represent C types that have special meanings.

Type Description
void_t

The special C type void. This can only be used as a return value type.

Note: You must use void_t, not void, since void is a keyword in JavaScript.
voidptr_t The C type void *. This is a pointer to an indeterminate type of data.

Large integer types

Because JavaScript doesn't support large (64-bit) integers, js-ctypes provides two data types allowing access to 64-bit integer data.

Type Description
Int64 A JavaScript object representing a 64-bit signed integer.
UInt64 A JavaScript object representing a 64-bit unsigned integer.

Methods

cast()

Casts the specified CData object to a new type, returning a new CData object representing the value in the new type. See Type casting for details.

CData cast(
  data,
  type
);
Parameters
data
The CData object to type cast.
type
The type to which to cast the value. This can be one of the Predefined data types, or any user-defined types.
Return value

A new CData object representing the same data, but with the specified type.

libraryName()

Returns the correct platform-specific filename for a given library name (e.g. libnss3.dylib for nss3 on OS X.)

String libraryName(
  name
);
Parameters
name
The name of the library.
Return value

String containing the platform-specific filename of the library.

open()

Opens a library, specified by a pathname. The library is loaded from the specified full path, or, if a partial path is specified, from the appropriate library directory based on the platform on which the application is running. See Library search paths for more details.

Library open(
  libSpec
);
Parameters
libSpec
The native library to open, specified as a pathname string.
Return value

A Library object representing the opened library.

Examples

Example of StructType() on Windows

Let's say we need TB_BUTTON, this type needs to be created. So we look up on MSDN what the TB_BUTTON structures is (MSDN :: TB_BUTTON Structure) and it says it is:

typedef struct {
  int       iBitmap;
  int       idCommand;
  BYTE      fsState;
  BYTE      fsStyle;
#ifdef _WIN64
  BYTE      bReserved[6];
#else
#if defined(_WIN32)
  BYTE      bReserved[2];
#endif
#endif
  DWORD_PTR dwData;
  INT_PTR   iString;
} TBBUTTON, *PTBBUTTON, *LPTBBUTTON;

So now notice that if it's 64-bit process it's different than if it's 32-bit process. So we will figure that out and create the structure appropriately. This is what the final looks like:

var struct_TBButton;
if (ctypes.voidptr_t.size == 4 /* 32-bit */) {
  struct_TBButton = ctypes.StructType('TBButton', [
    {'iBitmap': ctypes.int},
    {'idCommand': ctypes.int},
    {'fbState': ctypes.unsigned_char},
    {'fsStyle': ctypes.unsigned_char},
    {'bReserved': ctypes.unsigned_char},
    {'bReserved2': ctypes.unsigned_char},
    {'dwData': ctypes.uintptr_t},
    {'iString': ctypes.intptr_t}
  ]);
}
else if (ctypes.voidptr_t.size == 8 /* 64-bit */) {
  struct_TBButton = ctypes.StructType('TBButton', [
    {'iBitmap': ctypes.int},
    {'idCommand': ctypes.int},
    {'fbState': ctypes.unsigned_char},
    {'fsStyle': ctypes.unsigned_char},
    {'bReserved': ctypes.unsigned_char},
    {'bReserved2': ctypes.unsigned_char},
    {'bReserved3': ctypes.unsigned_char},
    {'bReserved4': ctypes.unsigned_char},
    {'bReserved5': ctypes.unsigned_char},
    {'bReserved6': ctypes.unsigned_char},
    {'dwData': ctypes.uintptr_t},
    {'iString': ctypes.intptr_t}
  ]);
}
else {
  throw new Error("wut?!");
}

console.log(struct_TBButton.size);
// 20 on 32-bit, 32 on 64-bit if I'm not mistaken

There is another way to do this, we can use ArrayType, but example for this I don't know at this time.

Credit for this example is to nmaier (StackOverflow :: Getting TB_BUTTON is crashing and not working)

Example of cast and FunctionType on Windows

Components.utils.import("resource://gre/modules/ctypes.jsm");

var kernel = ctypes.open("kernel32.dll");
var HMODULE = ctypes.uint32_t;
var HWND = ctypes.uint32_t;
var LPCTSTR = ctypes.jschar.ptr;
var LPCSTR = ctypes.char.ptr;
var LoadLibrary = kernel.declare("LoadLibraryW", ctypes.winapi_abi, HMODULE, LPCTSTR);
var GetProcAddress = kernel.declare("GetProcAddress", ctypes.winapi_abi, ctypes.void_t.ptr, HMODULE, LPCSTR);

var hUser = LoadLibrary("user32");
var funcPtr = GetProcAddress(hUser, "MessageBoxW");

// Now we have a pointer to a function, let's cast it to the right type
var MessageBoxType = ctypes.FunctionType(ctypes.winapi_abi, ctypes.int32_t, [HWND, LPCTSTR, LPCTSTR, ctypes.uint32_t]);
funcPtr = ctypes.cast(funcPtr, MessageBoxType.ptr);
funcPtr(0, "Test1", "Test2", 0);

Credit for this example is to Wladimir Palant (StackOverflow :: How to call a function using pointer in js-ctypes)

See also

Document Tags and Contributors

 Contributors to this page: adgallman2, arai, Noitidart, kscarfone, Yoric, Shaver, vasi, Sheppy, philikon, MattBrubeck
 Last updated by: adgallman2,