node ffi-tools
Some hacky experiments with the newly updated node-ffi that works cross-platform.
Generate Interfaces from header files
Given the following win32 header file, the ffi interface can be auto-generated, resulting in the js source below.
var ffiTools = ;var kernal32 = ffiTools;
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 SYSTEMTIME, *PSYSTEMTIME; // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx typedef struct _FILETIME FILETIME, *PFILETIME; // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683175(v=vs.85).aspx HWND WINAPI // http://msdn.microsoft.com/en-us/library/windows/desktop/aa364989(v=vs.85).aspx DWORD WINAPI // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724320(v=vs.85).aspx BOOL WINAPI // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724390(v=vs.85).aspx void WINAPI
Produced kernal32 object:
GetConsoleWindow: Function: GetConsoleWindow GetShortPathName: Function: GetShortPathName GetFileTime: Function: GetFileTime GetSystemTime: Function: GetSystemTime
Interface toSource to produce fixed source code bindings
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 = ;var structs = {};var kernel32 = _lib: 'kernel32.dll' ffiDynamicLibraryFLAGSRTLD_NOW ; structsFILETIME = ffi; structsSYSTEMTIME = ffi; kernel32{ var ff = kernel32_lib 'ulong' false; return { return ; }}; kernel32{ var ff = kernel32_lib 'uint32' 'string''pointer''uint32' false; return { var out = Object; outlpszShortPath = 4; outret = ; outlpszShortPath = outlpszShortPath; return out; }}; kernel32{ var ff = kernel32_lib 'int8' 'ulong''pointer''pointer''pointer' false; return { var out = Object; outlpCreationTime = ; outlpLastAccessTime = ; outlpLastWriteTime = ; outret = ; return out; }}; kernel32{ var ff = kernel32_lib 'void' 'pointer' false; return { var out = ; ; return out; }};
End Usage
kernel32;// --> wYear: 2012 wMonth: 1 wDayOfWeek: 0 wDay: 22 wHour: 2 wMinute: 17 wSecond: 26 wMilliseconds: 871