win32-api
FFI Definitions of Windows win32 api for node-ffi
What can I do with this?
Calling win32 native functions come from user32.dll, kernel32.dll, comctl32.dll by Node.js
Installing
npm install --save win32-api
Usage
// find calc's hWnd, need run a calculator program manually at first/** * expose module: * 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 K U = ; // or {Kernel32, User32}const ref = ; const knl32 = K;const user32 = U; // load all apis defined in lib/{dll}/api from user32.dll// const user32 = U.load(['FindWindowExW']); // load only one api defined in lib/{dll}/api from user32.dll const title = 'Calculator\0'; // null-terminated string// const title = '计算器\0'; // string in chinese const lpszWindow = Buffer;const hWnd = user32; if hWnd && ! hWnd // Caution: output hWnd will cuase exception in the following process, even next script! // So do NOT do this in the production code! // console.log('buf: ', hWnd); // avoid this console; // this is ok // Change title of the Calculator const res = user32; if ! res // See: [System Error Codes] below const errcode = knl32; const len = 255; const buf = Buffer; const p = 0x00001000 | 0x00000200; // FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS const langid = 0x0409; // 0x0409: US, 0x0000: Neutral locale language const msglen = knl32; if msglen console; else console;
// use the types exposed by the module for TypeScript dev;; // 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 hex;console.loghex; // ← '7FA89D006FD8' buf.type = ref.types.int; // @ts-ignore // now we can dereference to get the "meaningful" valueconsole.logref.derefbuf; // ← 12345
// struct usage by ref-struct;; // https://msdn.microsoft.com/en-us/library/windows/desktop/dd162805(v=vs.85).aspx;point.x = 100;point.y = 200;console.logpoint;
// usage of types and windef:;; ; ; // ← here the typesbuf.writeInt32LE12345, 0; // const hInstance =<FM.FFIBuffer> Buffer.alloc(process.arch === 'x64' ? 8 : 4);; // W.HINSTANCE is 'int64*' under x64, 'int32*' under ia32knl32.GetModuleHandleExW0, null, hInstance;
Demo
Dependencies Troubleshooting
- If installation of node-gyp fails: Check out node-gyp and windows-build-tools