Fork of waitingsong/node-win32-api. Changes:
- Replace
ref-napi
and @types/ref-napi
with @tigerconnect/ref-napi
for node v16 support, due to stacktraces in node v14 - v16 with ref-napi - Use
@tigerconnect/ffi-napi
fork instead of ffi-napi
for the same reason - Use
@tigerconnect/ref-union-di
fork instead of ref-union-di
to fix some CommonJS problems - Pin
@types/ref-struct-di
to 1.1.0
since 1.1.6
doesn't seem to work well - Remove OS requirement from win32-api package.json so we can still get access to types and APIs for test mocks
- Require node 14.16.0 or higher
- Upgrade eslint config and fix lint errors
win32-api
FFI Definitions of Windows win32 api for node-ffi-napi
Initialization
npm run repo:init
Packages
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 @tigerconnect/win32-api
Usage
Find window and set window title
import { K, U } from '@tigerconnect/win32-api'
import * as ref from '@tigerconnect/ref-napi'
const knl32 = K.load()
const user32 = U.load()
const title = 'Calculator\0'
const lpszWindow = Buffer.from(title, 'ucs2')
const hWnd = user32.FindWindowExW(0, 0, null, lpszWindow)
if (typeof hWnd === 'number' && hWnd > 0
|| typeof hWnd === 'bigint' && hWnd > 0
|| typeof hWnd === 'string' && hWnd.length > 0
) {
console.log('buf: ', hWnd)
const res = user32.SetWindowTextW(hWnd, Buffer.from('Node-Calculator\0', 'ucs2'))
if ( ! res) {
console.log('SetWindowTextW failed')
}
else {
console.log('window title changed')
}
}
import { U } from '@tigerconnect/win32-api'
import * as ref from '@tigerconnect/ref-napi'
const buf = Buffer.alloc(4)
buf.writeInt32LE(12345, 0)
const hex = ref.hexAddress(buf)
console.log(typeof hex)
console.log(hex)
buf.type = ref.types.int
console.log(ref.deref(buf))
import * as ref from '@tigerconnect/ref-napi'
import { K, DTypes as W } from '@tigerconnect/win32-api'
const knl32 = K.load()
const lpszClass = Buffer.from('guard64\0', 'ucs2')
const hInstanceBuffer = ref.alloc(W.HANDLE_PVOID)
const hInstanceAddr = ref.address(hInstanceBuffer)
knl32.GetModuleHandleExW(0, lpszClass, hInstanceAddr)
console.log(hInstanceBuffer)
console.log(hInstanceBuffer.readInt32LE(0))
console.log(hInstanceBuffer.readBigUInt64LE())
import * as Struct from 'ref-struct'
import { DModel as M, DStruct as DS } from '@tigerconnect/win32-api'
const point: M.POINT_Struct = new Struct(DS.POINT)()
point.x = 100
point.y = 200
console.log(point)
import * as ref from '@tigerconnect/ref-napi'
import * as StructDi from 'ref-struct-di'
import { DModel as M, DStruct as DS } from '@tigerconnect/win32-api'
const Struct = StructDi(ref)
const point: M.POINT_Struct = new Struct(DS.POINT)()
point.x = 100
point.y = 200
console.log(point)
import * as Struct from 'ref-struct-napi'
import {
DModel as M,
DStructExt,
} from '@tigerconnect/win32-api'
const dd: M.DISPLAY_DEVICEW_Struct = new Struct(DStructExt.DISPLAY_DEVICEW)()
dd.cb = dd.ref().byteLength
console.log(dd)
Async Find window and set window title
import { U } from '@tigerconnect/win32-api'
import * as ref from '@tigerconnect/ref-napi'
const u32 = U.load(['FindWindowExW', 'SetWindowTextW'])
const lpszClass = Buffer.from('CalcFrame\0', 'ucs2')
u32.FindWindowExW.async(0, 0, lpszClass, null, (err, hWnd) => {
if (err) {
throw err
}
if (typeof hWnd === 'number' && hWnd > 0
|| typeof hWnd === 'bigint' && hWnd > 0
|| typeof hWnd === 'string' && hWnd.length > 0
) {
const title = 'Node-Calculator'
u32.SetWindowTextW.async(hWnd, Buffer.from(title + '\0', 'ucs2'), err2 => {
if (err2) {
throw err2
}
const buf = Buffer.alloc(title.length * 2)
u32.GetWindowTextW.async(hWnd, buf, buf.byteLength, err3 => {
if (err3) {
throw err3
}
const str = buf.toString('ucs2').replace(/\0+$/, '')
if (str !== title) {
throw new Error(`title should be changed to ${title}, bug got ${str}`)
}
})
})
}
else {
throw new Error('FindWindowExW() failed')
}
})
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
Relevant
License
MIT
Languages