win32-api
Version:
FFI definitions of windows win32 api for node-ffi
67 lines (59 loc) • 1.84 kB
JavaScript
import * as ffi from 'ffi'
import { Config } from 'win32-def'
const dllInst = new Map() // for DLL.load() with settings.singleton === true
export function load(dllName, dllFuncs, fns, settings) {
const st = parse_settings(settings)
if (st && st.singleton) {
let inst = get_inst_by_name(dllName)
if (!inst) {
inst = ffi.Library(dllName, gen_api_opts(dllFuncs, fns))
set_inst_by_name(dllName, inst)
}
return inst
}
else {
return ffi.Library(dllName, gen_api_opts(dllFuncs, fns))
}
}
// generate function definitions via converting macro windows data type (like PVOID) to the expected value
export function gen_api_opts(dllFuncs, fns) {
const ret = {}
if (fns && Array.isArray(fns) && fns.length) {
for (const fn of fns) {
const ps = dllFuncs[fn]
if (ps) {
Object.defineProperty(ret, fn, {
value: ps,
writable: false,
enumerable: true,
configurable: false,
})
}
}
}
else {
for (const fn of Object.keys(dllFuncs)) {
const ps = dllFuncs[fn]
Object.defineProperty(ret, fn, {
value: ps,
writable: false,
enumerable: true,
configurable: false,
})
}
}
return ret
}
function get_inst_by_name(dllName) {
return dllInst.get(dllName)
}
function set_inst_by_name(dllName, inst) {
dllInst.set(dllName, inst)
}
function parse_settings(settings) {
const st = Object.assign({}, Config.settingsDefault)
if (typeof settings !== 'undefined' && settings && Object.keys(settings).length) {
Object.assign(st, settings)
}
return st
}