ffi.dlopen(path, definitions?): Object
Attributes
Loads a dynamic library and resolves the requested function definitions.
On Windows passing null is not supported.
When definitions is omitted, functions is returned as an empty object until
symbols are resolved explicitly.
The returned object contains:
Attributes
lib:
DynamicLibraryThe loaded library handle.
functions:
ObjectCallable wrappers for the requested symbols.
The returned object also implements the explicit resource management protocol,
so it can be used with the using declaration. Disposing the returned
object closes the library handle.
import { dlopen, suffix } from 'node:ffi'; { using handle = dlopen(`./mylib.${suffix}`, { add_i32: { arguments: ['int32', 'int32'], return: 'int32' }, }); console.log(handle.functions.add_i32(20, 22)); } // handle.lib.close() is invoked automatically here.
import { dlopen, suffix } from 'node:ffi'; const { lib, functions } = dlopen(`./mylib.${suffix}`, { add_i32: { arguments: ['int32', 'int32'], return: 'int32' }, string_length: { arguments: ['pointer'], return: 'uint64' }, }); console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi'); const { lib, functions } = dlopen(`./mylib.${suffix}`, { add_i32: { arguments: ['int32', 'int32'], return: 'int32' }, string_length: { arguments: ['pointer'], return: 'uint64' }, }); console.log(functions.add_i32(20, 22));