win32-api
Version:
FFI definitions of windows win32 api for node-ffi
324 lines (299 loc) • 12.1 kB
JavaScript
/**
* win32-api
* FFI definitions of windows win32 api for node-ffi
*
* @version 6.2.0
* @author waiting
* @license MIT
* @link https://github.com/waitingsong/node-win32-api#readme
*/
import { Config, DTypes } from 'win32-def';
export { Config, DModel, DStruct, DTypes, FModel } from 'win32-def';
import { Library } from 'ffi';
const dllInst = new Map(); // for DLL.load() with settings.singleton === true
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 = Library(dllName, gen_api_opts(dllFuncs, fns));
set_inst_by_name(dllName, inst);
}
return inst
}
else {
return Library(dllName, gen_api_opts(dllFuncs, fns))
}
}
// generate function definitions via converting macro windows data type (like PVOID) to the expected value
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
}
const apiDef = {
InitCommonControlsEx: [DTypes.BOOL, [DTypes.LPINITCOMMONCONTROLSEX]],
};
const dllName = 'comctl32'; /* comctl32 */
const load$1 = (fns, settings) => load(dllName, apiDef, fns, settings);
var index = /*#__PURE__*/Object.freeze({
__proto__: null,
apiDef: apiDef,
dllName: dllName,
load: load$1
});
const apiDef$1 = {
FormatMessageW: [
DTypes.DWORD,
[DTypes.DWORD, DTypes.LPCVOID, DTypes.DWORD, DTypes.DWORD, DTypes.LPTSTR, DTypes.DWORD, DTypes.va_list],
],
FreeConsole: [DTypes.BOOL, []],
/** err code: https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms681381(v=vs.85).aspx */
GetLastError: [DTypes.DWORD, []],
/** retrive value from buf by ret.ref().readUInt32() */
GetModuleHandleW: [DTypes.HMODULE, [DTypes.LPCTSTR]],
/** flags, optional LPCTSTR name, ref hModule */
GetModuleHandleExW: [DTypes.BOOL, [DTypes.DWORD, DTypes.LPCTSTR, DTypes.HMODULE]],
GetProcessHeaps: [DTypes.DWORD, [DTypes.DWORD, DTypes.PHANDLE]],
GetSystemTimes: [DTypes.BOOL, [DTypes.PFILETIME, DTypes.PFILETIME, DTypes.PFILETIME]],
HeapFree: [DTypes.BOOL, [DTypes.HANDLE, DTypes.DWORD, DTypes.LPVOID]],
OpenProcess: [DTypes.HANDLE, [DTypes.DWORD, DTypes.BOOL, DTypes.DWORD]],
OutputDebugStringW: [DTypes.VOID, [DTypes.LPCTSTR]],
SetLastError: [DTypes.VOID, [DTypes.DWORD]],
SetThreadExecutionState: [DTypes.INT, [DTypes.INT]],
};
const dllName$1 = 'kernel32'; /* kernel32 */
const load$2 = (fns, settings) => load(dllName$1, apiDef$1, fns, settings);
var index$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
apiDef: apiDef$1,
dllName: dllName$1,
load: load$2
});
const apiDef$2 = {
NtQueryInformationProcess: [DTypes.NTSTATUS, [DTypes.HANDLE, DTypes.DWORD32, DTypes.PVOID, DTypes.ULONG, DTypes.PULONG]],
};
const dllName$2 = 'ntdll'; /* ntdll */
const load$3 = (fns, settings) => load(dllName$2, apiDef$2, fns, settings);
var index$2 = /*#__PURE__*/Object.freeze({
__proto__: null,
apiDef: apiDef$2,
dllName: dllName$2,
load: load$3
});
const apiDef$3 = {
BringWindowToTop: [DTypes.BOOL, [DTypes.HWND]],
/** url: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-clienttoscreen */
ClientToScreen: [DTypes.BOOL, [DTypes.HWND, DTypes.LPPOINT]],
CloseWindow: [DTypes.BOOL, [DTypes.HWND]],
CreateWindowExW: [DTypes.HWND, [
DTypes.DWORD, DTypes.LPCTSTR, DTypes.LPCTSTR, DTypes.DWORD,
DTypes.INT, DTypes.INT, DTypes.INT, DTypes.INT,
DTypes.HWND, DTypes.HMENU, DTypes.HINSTANCE, DTypes.LPVOID,
]],
DefWindowProcW: [DTypes.LRESULT, [DTypes.HWND, DTypes.UINT, DTypes.WPARAM, DTypes.LPARAM]],
DestroyWindow: [DTypes.BOOL, [DTypes.HWND]],
DispatchMessageW: [DTypes.LRESULT, [DTypes.LPMSG]],
EnumThreadWindows: [DTypes.BOOL, [DTypes.DWORD, DTypes.WNDENUMPROC, DTypes.LPARAM]],
EnumWindows: [DTypes.BOOL, [DTypes.WNDENUMPROC, DTypes.LPARAM]],
FindWindowExW: [DTypes.HWND, [DTypes.HWND, DTypes.HWND, DTypes.LPCTSTR, DTypes.LPCTSTR]],
GetAncestor: [DTypes.HWND, [DTypes.HWND, DTypes.UINT]],
GetClassInfoExW: [DTypes.BOOL, [DTypes.HINSTANCE, DTypes.LPCTSTR, DTypes.LPWNDCLASSEX]],
GetForegroundWindow: [DTypes.HWND, []],
GetMessageW: [DTypes.BOOL, [DTypes.LPMSG, DTypes.HWND, DTypes.UINT, DTypes.UINT]],
GetParent: [DTypes.HWND, [DTypes.HWND]],
GetTopWindow: [DTypes.HWND, [DTypes.HWND]],
GetWindow: [DTypes.HWND, [DTypes.HWND, DTypes.UINT]],
GetWindowInfo: [DTypes.BOOL, [DTypes.HWND, DTypes.PWINDOWINFO]],
GetWindowLongW: [DTypes.LONG, [DTypes.HWND, DTypes.INT]],
GetWindowTextW: [DTypes.INT, [DTypes.HWND, DTypes.LPTSTR, DTypes.INT]],
GetWindowThreadProcessId: [DTypes.DWORD, [DTypes.HWND, DTypes.LPDWORD]],
IsWindowVisible: [DTypes.BOOL, [DTypes.HWND]],
PeekMessageW: [DTypes.BOOL, [DTypes.LPMSG, DTypes.HWND, DTypes.UINT, DTypes.UINT, DTypes.UINT]],
PostMessageW: [DTypes.BOOL, [DTypes.HWND, DTypes.UINT, DTypes.WPARAM, DTypes.LPARAM]],
RegisterClassExW: [DTypes.ATOM, [DTypes.WNDCLASSEX]],
SendMessageW: [DTypes.LRESULT, [DTypes.HWND, DTypes.UINT, DTypes.WPARAM, DTypes.LPARAM]],
SetWindowTextW: [DTypes.BOOL, [DTypes.HWND, DTypes.LPCTSTR]],
SetWinEventHook: [DTypes.HWINEVENTHOOK, [DTypes.UINT, DTypes.UINT, DTypes.HMODULE, DTypes.WINEVENTPROC, DTypes.DWORD, DTypes.DWORD, DTypes.UINT]],
ShowWindow: [DTypes.BOOL, [DTypes.HWND, DTypes.INT]],
TranslateMessage: [DTypes.BOOL, [DTypes.LPMSG]],
TranslateMessageEx: [DTypes.BOOL, [DTypes.LPMSG]],
UnhookWinEvent: [DTypes.BOOL, [DTypes.HWINEVENTHOOK]],
UpdateWindow: [DTypes.BOOL, [DTypes.HWND]],
};
/* istanbul ignore next */
if (process.arch === 'x64') {
apiDef$3.GetWindowLongPtrW = [DTypes.LONG_PTR, [DTypes.HWND, DTypes.INT]];
}
/* --------- Window Styles ---------------- */
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx
const WS_BORDER = 0x00800000;
const WS_CAPTION = 0x00C00000;
const WS_CHILD = 0x40000000;
const WS_CLIPCHILDREN = 0x02000000;
const WS_CLIPSIBLINGS = 0x04000000;
const WS_DISABLED = 0x08000000;
const WS_DLGFRAME = 0x00400000;
const WS_GROUP = 0x00020000;
const WS_HSCROLL = 0x00100000;
const WS_ICONIC = 0x20000000;
const WS_MAXIMIZE = 0x01000000;
const WS_MAXIMIZEBOX = 0x00010000;
const WS_MINIMIZE = 0x20000000;
const WS_MINIMIZEBOX = 0x00020000;
const WS_OVERLAPPED = 0x00000000;
const WS_POPUP = 0x80000000; // The windows is a pop-up window
const WS_SIZEBOX = 0x00040000;
const WS_SYSMENU = 0x00080000; // The window has a window menu on its title bar.
const WS_TABSTOP = 0x00010000;
const WS_THICKFRAME = 0x00040000;
const WS_TILED = 0x00000000;
const WS_VISIBLE = 0x10000000;
const WS_VSCROLL = 0x00200000;
// tslint:disable-next-line
const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
// tslint:disable-next-line
const WS_POPUPWINDOW = (WS_POPUP | WS_BORDER | WS_SYSMENU);
// tslint:disable-next-line
const WS_TILEDWINDOW = (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
/* --------- Extended Window Styles ---------------- */
// https://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
const WS_EX_ACCEPTFILES = 0x00000010;
const WS_EX_APPWINDOW = 0x00040000;
const WS_EX_CLIENTEDGE = 0x00000200;
const WS_EX_COMPOSITED = 0x02000000;
const WS_EX_CONTEXTHELP = 0x00000400;
const WS_EX_CONTROLPARENT = 0x00010000;
const WS_EX_DLGMODALFRAME = 0x00000001;
const WS_EX_LAYERED = 0x00080000;
const WS_EX_LAYOUTRTL = 0x00400000;
const WS_EX_LEFT = 0x00000000;
const WS_EX_LEFTSCROLLBAR = 0x00004000;
const WS_EX_LTRREADING = 0x00000000;
const WS_EX_MDICHILD = 0x00000040;
const WS_EX_NOACTIVATE = 0x08000000;
const WS_EX_NOINHERITLAYOUT = 0x00100000;
const WS_EX_NOPARENTNOTIFY = 0x00000004;
const WS_EX_NOREDIRECTIONBITMAP = 0x00200000;
const WS_EX_RIGHT = 0x00001000;
const WS_EX_RIGHTSCROLLBAR = 0x00000000;
const WS_EX_RTLREADING = 0x00002000;
const WS_EX_STATICEDGE = 0x00020000;
const WS_EX_TOOLWINDOW = 0x00000080;
const WS_EX_TOPMOST = 0x00000008;
const WS_EX_TRANSPARENT = 0x00000020;
const WS_EX_WINDOWEDGE = 0x00000100;
// tslint:disable-next-line
const WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
// tslint:disable-next-line
const WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
const PM_NOREMOVE = 0x0000;
const PM_REMOVE = 0x0001;
const PM_NOYIELD = 0x0002;
var constants = /*#__PURE__*/Object.freeze({
__proto__: null,
WS_BORDER: WS_BORDER,
WS_CAPTION: WS_CAPTION,
WS_CHILD: WS_CHILD,
WS_CLIPCHILDREN: WS_CLIPCHILDREN,
WS_CLIPSIBLINGS: WS_CLIPSIBLINGS,
WS_DISABLED: WS_DISABLED,
WS_DLGFRAME: WS_DLGFRAME,
WS_GROUP: WS_GROUP,
WS_HSCROLL: WS_HSCROLL,
WS_ICONIC: WS_ICONIC,
WS_MAXIMIZE: WS_MAXIMIZE,
WS_MAXIMIZEBOX: WS_MAXIMIZEBOX,
WS_MINIMIZE: WS_MINIMIZE,
WS_MINIMIZEBOX: WS_MINIMIZEBOX,
WS_OVERLAPPED: WS_OVERLAPPED,
WS_POPUP: WS_POPUP,
WS_SIZEBOX: WS_SIZEBOX,
WS_SYSMENU: WS_SYSMENU,
WS_TABSTOP: WS_TABSTOP,
WS_THICKFRAME: WS_THICKFRAME,
WS_TILED: WS_TILED,
WS_VISIBLE: WS_VISIBLE,
WS_VSCROLL: WS_VSCROLL,
WS_OVERLAPPEDWINDOW: WS_OVERLAPPEDWINDOW,
WS_POPUPWINDOW: WS_POPUPWINDOW,
WS_TILEDWINDOW: WS_TILEDWINDOW,
WS_EX_ACCEPTFILES: WS_EX_ACCEPTFILES,
WS_EX_APPWINDOW: WS_EX_APPWINDOW,
WS_EX_CLIENTEDGE: WS_EX_CLIENTEDGE,
WS_EX_COMPOSITED: WS_EX_COMPOSITED,
WS_EX_CONTEXTHELP: WS_EX_CONTEXTHELP,
WS_EX_CONTROLPARENT: WS_EX_CONTROLPARENT,
WS_EX_DLGMODALFRAME: WS_EX_DLGMODALFRAME,
WS_EX_LAYERED: WS_EX_LAYERED,
WS_EX_LAYOUTRTL: WS_EX_LAYOUTRTL,
WS_EX_LEFT: WS_EX_LEFT,
WS_EX_LEFTSCROLLBAR: WS_EX_LEFTSCROLLBAR,
WS_EX_LTRREADING: WS_EX_LTRREADING,
WS_EX_MDICHILD: WS_EX_MDICHILD,
WS_EX_NOACTIVATE: WS_EX_NOACTIVATE,
WS_EX_NOINHERITLAYOUT: WS_EX_NOINHERITLAYOUT,
WS_EX_NOPARENTNOTIFY: WS_EX_NOPARENTNOTIFY,
WS_EX_NOREDIRECTIONBITMAP: WS_EX_NOREDIRECTIONBITMAP,
WS_EX_RIGHT: WS_EX_RIGHT,
WS_EX_RIGHTSCROLLBAR: WS_EX_RIGHTSCROLLBAR,
WS_EX_RTLREADING: WS_EX_RTLREADING,
WS_EX_STATICEDGE: WS_EX_STATICEDGE,
WS_EX_TOOLWINDOW: WS_EX_TOOLWINDOW,
WS_EX_TOPMOST: WS_EX_TOPMOST,
WS_EX_TRANSPARENT: WS_EX_TRANSPARENT,
WS_EX_WINDOWEDGE: WS_EX_WINDOWEDGE,
WS_EX_OVERLAPPEDWINDOW: WS_EX_OVERLAPPEDWINDOW,
WS_EX_PALETTEWINDOW: WS_EX_PALETTEWINDOW,
PM_NOREMOVE: PM_NOREMOVE,
PM_REMOVE: PM_REMOVE,
PM_NOYIELD: PM_NOYIELD
});
const dllName$3 = 'user32'; /* user32 */
const load$4 = (fns, settings) => load(dllName$3, apiDef$3, fns, settings);
var index$3 = /*#__PURE__*/Object.freeze({
__proto__: null,
apiDef: apiDef$3,
constants: constants,
dllName: dllName$3,
load: load$4
});
export { index as C, index as Comctl32, index$1 as K, index$1 as Kernel32, index$2 as Ntdll, index$3 as U, index$3 as User32 };