
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Experimental hacky tools and wrappers for easing node-ffi usage and generating bindings.
Some hacky experiments with the newly updated node-ffi that works cross-platform.
Given the following win32 header file, the ffi interface can be auto-generated, resulting in the js source below.
var ffiTools = require('ffi-tools');
var kernal32 = ffiTools.loadHeaders(new Library('kernal32'));
kernal32.h (relies on MSDN documentation formatting currently):
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx
typedef struct _FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME, *PFILETIME;
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683175(v=vs.85).aspx
HWND WINAPI GetConsoleWindow(
void
);
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364989(v=vs.85).aspx
DWORD WINAPI GetShortPathName(
__in LPCTSTR lpszLongPath,
__out LPTSTR lpszShortPath,
__in DWORD cchBuffer
);
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724320(v=vs.85).aspx
BOOL WINAPI GetFileTime(
__in HANDLE hFile,
__out_opt LPFILETIME lpCreationTime,
__out_opt LPFILETIME lpLastAccessTime,
__out_opt LPFILETIME lpLastWriteTime
);
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724390(v=vs.85).aspx
void WINAPI GetSystemTime(
__out LPSYSTEMTIME lpSystemTime
);
Produced kernal32 object:
{ GetConsoleWindow: [Function: GetConsoleWindow],
GetShortPathName: [Function: GetShortPathName],
GetFileTime: [Function: GetFileTime],
GetSystemTime: [Function: GetSystemTime] }
To optimize performance, it's also possible to generate actual JS source code that replicates the behavior of the produced interfaces. The automated interfaces are more complicated than the code produced below but the end result is the same. Using the resulting generated source is likely more efficient (and definitely more readable).
From above, kernel32.toSource()
produces the following fully standalone and functional code (indentation included):
var ffi = require('node-ffi');
var structs = {};
var kernel32 = { _lib: new ffi.DynamicLibrary('kernel32.dll', ffi.DynamicLibrary.FLAGS.RTLD_NOW) };
structs.FILETIME = ffi.Struct(
['uint32', 'dwLowDateTime'],
['uint32', 'dwHighDateTime']
);
structs.SYSTEMTIME = ffi.Struct(
['ushort', 'wYear'],
['ushort', 'wMonth'],
['ushort', 'wDayOfWeek'],
['ushort', 'wDay'],
['ushort', 'wHour'],
['ushort', 'wMinute'],
['ushort', 'wSecond'],
['ushort', 'wMilliseconds']
);
kernel32.GetConsoleWindow = function(){
var ff = new ffi.ForeignFunction(kernel32._lib.get('GetConsoleWindow'), 'ulong', [], false);
return function GetConsoleWindow(){
return ff();
}
}();
kernel32.GetShortPathName = function(){
var ff = new ffi.ForeignFunction(kernel32._lib.get('GetShortPathNameW'), 'uint32', ['string','pointer','uint32'], false);
return function GetShortPathName(lpszLongPath, cchBuffer){
var out = Object.create(null);
out.lpszShortPath = new ffi.Pointer(4);
out.ret = ff(lpszLongPath, out.lpszShortPath, cchBuffer);
out.lpszShortPath = out.lpszShortPath.getCString();
return out;
}
}();
kernel32.GetFileTime = function(){
var ff = new ffi.ForeignFunction(kernel32._lib.get('GetFileTime'), 'int8', ['ulong','pointer','pointer','pointer'], false);
return function GetFileTime(hFile){
var out = Object.create(null);
out.lpCreationTime = new structs.FILETIME;
out.lpLastAccessTime = new structs.FILETIME;
out.lpLastWriteTime = new structs.FILETIME;
out.ret = ff(hFile, out.lpCreationTime.pointer, out.lpLastAccessTime.pointer, out.lpLastWriteTime.pointer);
return out;
}
}();
kernel32.GetSystemTime = function(){
var ff = new ffi.ForeignFunction(kernel32._lib.get('GetSystemTime'), 'void', ['pointer'], false);
return function GetSystemTime(){
var out = new structs.SYSTEMTIME;
ff(out.pointer);
return out;
}
}();
kernel32.GetSystemTime();
// -->
{ wYear: 2012,
wMonth: 1,
wDayOfWeek: 0,
wDay: 22,
wHour: 2,
wMinute: 17,
wSecond: 26,
wMilliseconds: 871 }
FAQs
Experimental hacky tools and wrappers for easing node-ffi usage and generating bindings.
We found that ffi-tools demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.