win32-api
FFI Definitions of Windows win32 api for node-ffi-napi
Initialization
npm run repo:init
Packages
Package | Version | Dependencies | DevDependencies |
---|---|---|---|
win32-api |
|||
win32-def |
What can I do with this?
Calling win32 native functions come from user32.dll, kernel32.dll, comctl32.dll by Node.js via node-ffi-napi
Installing
npm install win32-api
Usage
Find window and set window title
// **Find calc's hWnd, need running a calculator program manually at first** /** * exposed modules: * C, Comctl32 for Comctl32 from lib/comctl32/api * K, Kernel32 for kernel32 from lib/kernel32/api * U, User32 for user32 from lib/user32/api */ // const user32 = U.load(['FindWindowExW']) // load only one api defined in lib/{dll}/api from user32.dll // const title = '计算器\0' // null-terminated string 字符串必须以\0即null结尾! if typeof hWnd === 'number' && hWnd > 0 || typeof hWnd === 'bigint' && hWnd > 0 || typeof hWnd === 'string' && hWnd.length > 0
Ref
// so we can all agree that a buffer with the int value written// to it could be represented as an "int *"buf.writeInt32LE12345, 0 console.logtypeof hexconsole.loghex // ← '7FA89D006FD8' buf.type = ref.types.int // @ts-ignore // now we can dereference to get the "meaningful" valueconsole.logref.derefbuf // ← 12345
// use of types and windef: knl32.GetModuleHandleExW0, lpszClass, hInstanceAddr// <Buffer@0x00000094D3968EC0 00 00 a4 60 ff 7f 00 00, type: { indirection: 2, name: 'uint64*' }>console.loghInstanceBufferconsole.loghInstanceBuffer.readInt32LE0 // -> 1621360640 (60A40000)console.loghInstanceBuffer.readBigUInt64LE // -> 140734814748672n (7FFF60A40000)
Struct
// struct usage with ref-struct // https://msdn.microsoft.com/en-us/library/windows/desktop/dd162805(v=vs.85).aspxpoint.x = 100point.y = 200console.logpoint // struct usage with ref-struct-di point.x = 100point.y = 200console.logpoint
StructExt
// struct usage with ref-struct // https://docs.microsoft.com/zh-cn/windows/win32/api/wingdi/ns-wingdi-display_devicew dd.cb = dd.ref.byteLengthconsole.logdd/**Detail in:https://github.com/waitingsong/node-win32-api/blob/master/packages/win32-api/src/data-struct-ext/wingdi.h.tshttps://github.com/waitingsong/node-win32-api/blob/master/packages/win32-api/test/user32/60_EnumDisplayDevicesW.test.ts*/
Async Find window and set window title
// **Find calc's hWnd, need running a calculator program manually at first** u32.FindWindowExW.async0, 0, lpszClass, null,
Demo
Dependencies Troubleshooting
Compile successfully with
- Node.js v12, Python v3.7 and VS2017
- Node.js v10, Python v2.7 and VS2017
If installation of node-gyp fails: Check out node-gyp and windows-build-tools