Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* eslint-disable new-cap */
'use strict';
const path = require('path');
const ffi = require('ffi-napi');
const wchar = require('ref-wchar-napi');
const ref = require('ref-napi');
const struct = require('ref-struct-napi');
// Create the struct required to save the window bounds
const Rect = struct({
left: 'long',
top: 'long',
right: 'long',
bottom: 'long'
});
const RectPointer = ref.refType(Rect);
// Required by QueryFullProcessImageName
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880(v=vs.85).aspx
const PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
// Create FFI declarations for the C++ library and functions needed (User32.dll), using their "Unicode" (UTF-16) version
const user32 = new ffi.Library('User32.dll', {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx
GetForegroundWindow: ['pointer', []],
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=vs.85).aspx
GetWindowTextW: ['int', ['pointer', 'pointer', 'int']],
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633521(v=vs.85).aspx
GetWindowTextLengthW: ['int', ['pointer']],
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx
GetWindowThreadProcessId: ['uint32', ['pointer', 'uint32 *']],
// Get window bounds function
'use strict';
/**
* Module dependencies.
*/
const ForeignFunction = require('./foreign_function');
const assert = require('assert');
const debug = require('debug')('ffi:DynamicLibrary');
const bindings = require('./bindings');
const funcs = bindings.StaticFunctions;
const ref = require('ref-napi');
const read = require('fs').readFileSync;
// typedefs
const int = ref.types.int;
const voidPtr = ref.refType(ref.types.void);
const dlopen = ForeignFunction(funcs.dlopen, voidPtr, [ 'string', int ]);
const dlclose = ForeignFunction(funcs.dlclose, int, [ voidPtr ]);
const dlsym = ForeignFunction(funcs.dlsym, voidPtr, [ voidPtr, 'string' ]);
const dlerror = ForeignFunction(funcs.dlerror, 'string', [ ]);
/**
* `DynamicLibrary` loads and fetches function pointers for dynamic libraries
* (.so, .dylib, etc). After the libray's function pointer is acquired, then you
* call `get(symbol)` to retreive a pointer to an exported symbol. You need to
* call `get___()` on the pointer to dereference it into its actual value, or
* turn the pointer into a callable function with `ForeignFunction`.
*/
function DynamicLibrary (path, mode) {
if (!(this instanceof DynamicLibrary)) {
it('should call the static "add_boxes" bindings', function () {
const count = 3;
const boxes = new Buffer(box.size * count);
box.set(boxes, box.size * 0, { width: 1, height: 10 });
box.set(boxes, box.size * 1, { width: 2, height: 20 });
box.set(boxes, box.size * 2, { width: 3, height: 30 });
const boxPtr = ref.refType(box);
const add_boxes = ffi.ForeignFunction(bindings.add_boxes, box, [ boxPtr, 'int' ]);
const rtn = add_boxes(boxes, count);
assert(rtn instanceof box);
assert.strictEqual(6, rtn.width);
assert.strictEqual(60, rtn.height);
});
it('should work with "gettimeofday" and a "timeval" Struct pointer', () => {
const timeval = new Struct({
tv_sec: ref.types.long,
tv_usec: ref.types.long
});
const timevalPtr = ref.refType(timeval);
const timezonePtr = ref.refType(ref.types.void);
const l = new Library(null, {
gettimeofday: [ref.types.int, [timevalPtr, timezonePtr]]
});
const tv = new timeval();
l.gettimeofday(tv.ref(), null);
assert.strictEqual(Math.floor(Date.now() / 1000), tv.tv_sec);
});
}
it('should not call "set()" of a pointer type', function () {
const voidType = Object.create(ref.types.void);
voidType.get = function () {
throw new Error('"get()" should not be called');
};
voidType.set = function () {
throw new Error('"set()" should not be called');
};
const voidPtr = ref.refType(voidType);
let called = false;
const cb = ffi.Callback(voidPtr, [ voidPtr ], function (ptr) {
called = true;
assert.strictEqual(0, ptr.address());
return ptr;
})
const fn = ffi.ForeignFunction(cb, voidPtr, [ voidPtr ]);
assert(!called);
const nul = fn(ref.NULL);
assert(called);
assert(Buffer.isBuffer(nul));
assert.strictEqual(0, nul.address());
});
exports.CBC = MQCBC._MQCBCffi_t;
exports.CMHO = ref.types.void;
exports.DMHO = ref.types.void;
exports.IMPO = ref.types.void;
exports.SMPO = ref.types.void;
exports.DMPO = ref.types.void;
exports.PD = ref.types.void;
exports.PHCONN = ref.refType(exports.HCONN);
exports.PHOBJ = ref.refType(exports.HOBJ);
exports.PLONG = ref.refType(exports.LONG);
exports.PHMSG = ref.refType(exports.HMSG);
exports.POD = ref.refType(exports.OD);
exports.PSD = ref.refType(exports.SD);
exports.PMD = ref.refType(exports.MD);
exports.PPMO = ref.refType(exports.PMO);
exports.PGMO = ref.refType(exports.GMO);
exports.PCNO = ref.refType(exports.CNO);
exports.PCSP = ref.refType(exports.CSP);
exports.PSCO = ref.refType(exports.SCO);
exports.PSRO = ref.refType(exports.SRO);
exports.PSTS = ref.refType(exports.STS);
exports.PCTLO = ref.refType(exports.CTLO);
exports.PCBD = ref.refType(exports.CBD);
exports.PCBC = ref.refType(exports.CBC);
exports.PCMHO = ref.refType(exports.CMHO);
exports.PDMHO = ref.refType(exports.DMHO);
exports.PIMPO = ref.refType(exports.IMPO);
exports.PSMPO = ref.refType(exports.SMPO);
exports.PDMPO = ref.refType(exports.DMPO);
exports.PPD = ref.refType(exports.PD);
exports.SMPO = ref.types.void;
exports.DMPO = ref.types.void;
exports.PD = ref.types.void;
exports.PHCONN = ref.refType(exports.HCONN);
exports.PHOBJ = ref.refType(exports.HOBJ);
exports.PLONG = ref.refType(exports.LONG);
exports.PHMSG = ref.refType(exports.HMSG);
exports.POD = ref.refType(exports.OD);
exports.PSD = ref.refType(exports.SD);
exports.PMD = ref.refType(exports.MD);
exports.PPMO = ref.refType(exports.PMO);
exports.PGMO = ref.refType(exports.GMO);
exports.PCNO = ref.refType(exports.CNO);
exports.PCSP = ref.refType(exports.CSP);
exports.PSCO = ref.refType(exports.SCO);
exports.PSRO = ref.refType(exports.SRO);
exports.PSTS = ref.refType(exports.STS);
exports.PCTLO = ref.refType(exports.CTLO);
exports.PCBD = ref.refType(exports.CBD);
exports.PCBC = ref.refType(exports.CBC);
exports.PCMHO = ref.refType(exports.CMHO);
exports.PDMHO = ref.refType(exports.DMHO);
exports.PIMPO = ref.refType(exports.IMPO);
exports.PSMPO = ref.refType(exports.SMPO);
exports.PDMPO = ref.refType(exports.DMPO);
exports.PPD = ref.refType(exports.PD);
exports.CHARV = StructType({
VSPtr : ref.refType(ref.types.void),
VSOffset : ref.types.int32,
exports.PPMO = ref.refType(exports.PMO);
exports.PGMO = ref.refType(exports.GMO);
exports.PCNO = ref.refType(exports.CNO);
exports.PCSP = ref.refType(exports.CSP);
exports.PSCO = ref.refType(exports.SCO);
exports.PSRO = ref.refType(exports.SRO);
exports.PSTS = ref.refType(exports.STS);
exports.PCTLO = ref.refType(exports.CTLO);
exports.PCBD = ref.refType(exports.CBD);
exports.PCBC = ref.refType(exports.CBC);
exports.PCMHO = ref.refType(exports.CMHO);
exports.PDMHO = ref.refType(exports.DMHO);
exports.PIMPO = ref.refType(exports.IMPO);
exports.PSMPO = ref.refType(exports.SMPO);
exports.PDMPO = ref.refType(exports.DMPO);
exports.PPD = ref.refType(exports.PD);
exports.CHARV = StructType({
VSPtr : ref.refType(ref.types.void),
VSOffset : ref.types.int32,
VSBufSize: ref.types.int32,
VSLength : ref.types.int32,
VSCCSID : ref.types.int32
});
exports.PCHARV = ref.refType(exports.CHARV);
exports.SCO = ref.types.void;
exports.SRO = ref.types.void;
exports.STS = ref.types.void;
exports.SD = ref.types.void;
exports.CTLO = ref.types.void;
exports.CBD = ref.types.void;
exports.CBC = MQCBC._MQCBCffi_t;
exports.CMHO = ref.types.void;
exports.DMHO = ref.types.void;
exports.IMPO = ref.types.void;
exports.SMPO = ref.types.void;
exports.DMPO = ref.types.void;
exports.PD = ref.types.void;
exports.PHCONN = ref.refType(exports.HCONN);
exports.PHOBJ = ref.refType(exports.HOBJ);
exports.PLONG = ref.refType(exports.LONG);
exports.PHMSG = ref.refType(exports.HMSG);
exports.POD = ref.refType(exports.OD);
exports.PSD = ref.refType(exports.SD);
exports.PMD = ref.refType(exports.MD);
exports.PPMO = ref.refType(exports.PMO);
exports.PGMO = ref.refType(exports.GMO);
exports.PCNO = ref.refType(exports.CNO);
exports.PCSP = ref.refType(exports.CSP);
exports.PSCO = ref.refType(exports.SCO);
exports.PSRO = ref.refType(exports.SRO);
exports.PSTS = ref.refType(exports.STS);
exports.PCTLO = ref.refType(exports.CTLO);
exports.PCBD = ref.refType(exports.CBD);
exports.PCBC = ref.refType(exports.CBC);