Skip to content
win32 api
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.config
.githooks chore: npm installation with log info Feb 27, 2019
.vscode chore: update launch.json.example Aug 16, 2018
demo chore(deps): update win32-def Feb 24, 2019
src chore: update comments and format order Mar 17, 2019
test test: normalize log output Mar 17, 2019
.editorconfig initial Mar 22, 2018
.eslintignore initial Mar 22, 2018
.eslintrc.json chore: update configs Apr 29, 2018
.gitignore chore: detect files exists is-skip-commitlint.js Jan 22, 2019
.npmignore build: npm ignore tsconfig.*.json Feb 24, 2019
.travis.yml ci: run test once Mar 14, 2019
CHANGELOG.md chore(release): 6.1.0 Mar 17, 2019
LICENSE initial Mar 22, 2018
README.md feat(types): generate types of async() automatically Mar 3, 2019
README.zh-CN.md feat(types): generate types of async() automatically Mar 3, 2019
appveyor.yml Merge remote-tracking branch 'bp/master' Mar 17, 2019
commitlint.config.js chore: format Feb 22, 2019
package.json chore(release): 6.1.0 Mar 17, 2019
rollup.config.js build: distinct external Jan 24, 2019
tsconfig.cjs.json refactor: with win32-def Jun 17, 2018
tsconfig.dev.json chore(script): add devbuild for develop build Jan 26, 2019
tsconfig.json style: format Dec 18, 2018
tslint.json chore(tslint): set rule interface-name false Feb 16, 2019

README.md

win32-api

FFI Definitions of Windows win32 api for node-ffi

Version License Available platform Build status Coverage Status Conventional Commits

What can I do with this?

Calling win32 native functions come from user32.dll, kernel32.dll, comctl32.dll by Node.js via node-ffi or 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**

/**
 * 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
 */
import { K, U } from 'win32-api'
import * as ref from 'ref-napi'

const knl32 = K.load()
const user32 = U.load()  // 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.from(title, 'ucs2')
const hWnd = user32.FindWindowExW(null, null, null, lpszWindow)

if (hWnd && ! hWnd.isNull()) {
  // Caution: outputing hWnd will cuase exception in the following process, even next script!
  // NOT do below in the production code!
  // console.log('buf: ', hWnd); // avoid this
  console.log('buf: ', ref.address(hWnd)) // this is ok

  // Change title of the Calculator
  const res = user32.SetWindowTextW(hWnd, Buffer.from('Node-Calculator\0', 'ucs2'))

  if ( ! res) {
    console.log('SetWindowTextW failed')
  }
  else {
    console.log('window title changed')
  }
}

Ref

import { U } from 'win32-api'
import * as ref from 'ref-napi'

// so we can all agree that a buffer with the int value written
// to it could be represented as an "int *"
const buf  = Buffer.alloc(4)
buf.writeInt32LE(12345, 0)

const hex = ref.hexAddress(buf)
console.log(typeof hex)
console.log(hex)  // ← '7FA89D006FD8'

buf.type = ref.types.int  // @ts-ignore

// now we can dereference to get the "meaningful" value
console.log(ref.deref(buf))  // ← 12345
// usage of types and windef:
import { K, FModel as FM, DTypes as W } from 'win32-api'
import * as ref from 'ref-napi'

const knl32 = K.load()

const buf = <FM.Buffer> Buffer.alloc(4)   // ← here the types
buf.writeInt32LE(12345, 0)

// const hInstance =<FM.Buffer> Buffer.alloc(process.arch === 'x64' ? 8 : 4)
const hInstance = <FM.Buffer> ref.alloc(W.HINSTANCE)    // W.HINSTANCE is 'int64*' under x64, 'int32*' under ia32
knl32.GetModuleHandleExW(0, null, hInstance)

Struct

// struct usage with ref-struct
import * as Struct from 'ref-struct'
import { DModel as M, DStruct as DS } from 'win32-api'

// https://msdn.microsoft.com/en-us/library/windows/desktop/dd162805(v=vs.85).aspx
const point: M.POINT_Struct = new Struct(DS.POINT)()
point.x = 100
point.y = 200
console.log(point)

// struct usage with ref-struct-di
import * as ref from 'ref-napi'
import * as StructDi from 'ref-struct-di'
import { DModel as M, DStruct as DS } from 'win32-api'

const Struct = StructDi(ref)
const point: M.POINT_Struct = new Struct(DS.POINT)()
point.x = 100
point.y = 200
console.log(point)

Async Find window and set window title

// **Find calc's hWnd, need running a calculator program manually at first**

import { U } from 'win32-api'
import * as ref from 'ref-napi'


const u32 = U.load(['FindWindowExW', 'SetWindowTextW'])
const lpszClass = Buffer.from('CalcFrame\0', 'ucs2')

u32.FindWindowExW.async(null, null, lpszClass, null, (err, hWnd) => {
  if (err) {
    throw err
  }

  if (hWnd && !ref.isNull(hWnd) && ref.address(hWnd)) {
    const title = 'Node-Calculator'
    // Change title of the 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

Relevant

License

MIT

Languages

You can’t perform that action at this time.