All Projects → waitingsong → Node Win32 Api

waitingsong / Node Win32 Api

Licence: mit
win32 api

Programming Languages

typescript
32286 projects

Labels

Projects that are alternatives of or similar to Node Win32 Api

windigo
Windows API and GUI in idiomatic Go.
Stars: ✭ 187 (-12.62%)
Mutual labels:  ffi, win32
winsafe
Windows API and GUI in safe, idiomatic Rust.
Stars: ✭ 110 (-48.6%)
Mutual labels:  ffi, win32
Win32
Build Win32 apps with Dart!
Stars: ✭ 256 (+19.63%)
Mutual labels:  win32, ffi
Libnotify
Ruby bindings for libnotify using FFI.
Stars: ✭ 138 (-35.51%)
Mutual labels:  ffi
Win32 Programming
Win32编程
Stars: ✭ 151 (-29.44%)
Mutual labels:  win32
Rust Bindgen
Automatically generates Rust FFI bindings to C (and some C++) libraries.
Stars: ✭ 2,453 (+1046.26%)
Mutual labels:  ffi
Rust Ffi Guide
A guide for doing FFI using Rust
Stars: ✭ 207 (-3.27%)
Mutual labels:  ffi
Winmerge
WinMerge is an Open Source differencing and merging tool for Windows. WinMerge can compare both folders and files, presenting differences in a visual text format that is easy to understand and handle.
Stars: ✭ 2,358 (+1001.87%)
Mutual labels:  win32
Resourcelib
C# File Resource Management Library
Stars: ✭ 197 (-7.94%)
Mutual labels:  win32
Goluwa
a game framework written in luajit
Stars: ✭ 173 (-19.16%)
Mutual labels:  ffi
Radasm2
RadASM v2
Stars: ✭ 161 (-24.77%)
Mutual labels:  win32
Crosswindow
💻📱 A cross platform system abstraction library written in C++ for managing windows and performing OS tasks.
Stars: ✭ 155 (-27.57%)
Mutual labels:  win32
Framelesshelper
Frameless windows for Qt Widgets and Qt Quick applications. Support Win32, X11, Wayland and macOS.
Stars: ✭ 183 (-14.49%)
Mutual labels:  win32
Hexed
Windows console-based hex editor
Stars: ✭ 145 (-32.24%)
Mutual labels:  win32
Xdpw
XD Pascal: A small embeddable self-hosting Pascal compiler for Windows. Supports Go-style methods and interfaces
Stars: ✭ 199 (-7.01%)
Mutual labels:  win32
Injectcollection
A collection of injection via vc++ in ring3
Stars: ✭ 131 (-38.79%)
Mutual labels:  win32
Inline Java
Haskell/Java interop via inline Java code in Haskell modules.
Stars: ✭ 197 (-7.94%)
Mutual labels:  ffi
Nos
RTOS for microcontrollers
Stars: ✭ 160 (-25.23%)
Mutual labels:  win32
Rustr
Rust and R Integration
Stars: ✭ 160 (-25.23%)
Mutual labels:  ffi
Win32 Darkmode
Example application shows how to use undocumented dark mode API introduced in Windows 10 1809.
Stars: ✭ 176 (-17.76%)
Mutual labels:  win32

win32-api

FFI Definitions of Windows win32 api for node-ffi-napi

GitHub tag License Available platform Build status Coverage Status Conventional Commits lerna

Initialization

npm run repo:init

Packages

Package Version Dependencies DevDependencies
win32-api main-svg main-d-svg main-dd-svg
win32-def def-svg def-d-svg def-dd-svg

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
 */
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'    // null-terminated string 字符串必须以\0即null结尾!

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)

  // 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
// use of types and windef:

import * as ref from 'ref-napi'
import { K, DTypes as W } from '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)
// <[email protected] 00 00 a4 60 ff 7f 00 00, type: { indirection: 2, name: 'uint64*' }>
console.log(hInstanceBuffer)
console.log(hInstanceBuffer.readInt32LE(0))     // -> 1621360640           (60A40000)
console.log(hInstanceBuffer.readBigUInt64LE())  // -> 140734814748672n (7FFF60A40000)

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)

StructExt

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


// https://docs.microsoft.com/zh-cn/windows/win32/api/wingdi/ns-wingdi-display_devicew 
const dd: M.DISPLAY_DEVICEW_Struct = new Struct(DStructExt.DISPLAY_DEVICEW)()
dd.cb = dd.ref().byteLength
console.log(dd)
/**
Detail in:
https://github.com/waitingsong/node-win32-api/blob/master/packages/win32-api/src/data-struct-ext/wingdi.h.ts
https://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**

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(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'
    // 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

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].