node-ctypes - Python ctypes-like FFI for Node.js
A foreign function interface library that mirrors the Python ctypes API, built on libffi and N-API.
import { CDLL, c_int } from 'node-ctypes';const libc = new CDLL('libc.so.6'); // Linux// const libc = new CDLL('msvcrt.dll'); // Windows// const libc = new CDLL('libc.dylib'); // macOSconst abs_func = libc.abs;abs_func.argtypes = [c_int];abs_func.restype = c_int;console.log(abs_func(-42)); // 42 Copy
import { CDLL, c_int } from 'node-ctypes';const libc = new CDLL('libc.so.6'); // Linux// const libc = new CDLL('msvcrt.dll'); // Windows// const libc = new CDLL('libc.dylib'); // macOSconst abs_func = libc.abs;abs_func.argtypes = [c_int];abs_func.restype = c_int;console.log(abs_func(-42)); // 42
import { Structure, c_int } from 'node-ctypes';class Point extends Structure { static _fields_ = [["x", c_int], ["y", c_int]];}const p = new Point(10, 20);console.log(p.x, p.y); // 10 20 Copy
import { Structure, c_int } from 'node-ctypes';class Point extends Structure { static _fields_ = [["x", c_int], ["y", c_int]];}const p = new Point(10, 20);console.log(p.x, p.y); // 10 20
import { callback, c_int32, c_void_p, readValue } from 'node-ctypes';const compare = callback( (a, b) => readValue(a, c_int32) - readValue(b, c_int32), c_int32, [c_void_p, c_void_p]);// Pass compare.pointer to native functionscompare.release(); // Release when done Copy
import { callback, c_int32, c_void_p, readValue } from 'node-ctypes';const compare = callback( (a, b) => readValue(a, c_int32) - readValue(b, c_int32), c_int32, [c_void_p, c_void_p]);// Pass compare.pointer to native functionscompare.release(); // Release when done
import { POINTER, pointer, c_int32 } from 'node-ctypes';const x = new c_int32(42);const p = pointer(x);console.log(p.contents.value); // 42 Copy
import { POINTER, pointer, c_int32 } from 'node-ctypes';const x = new c_int32(42);const p = pointer(x);console.log(p.contents.value); // 42
node-ctypes - Python ctypes-like FFI for Node.js
A foreign function interface library that mirrors the Python ctypes API, built on libffi and N-API.
Example: Loading a library and calling a function
Example: Defining a struct
Example: Callbacks
Example: Pointers