nodejs 调用win32 api


>node -v
v12.16.1

>npm install -g node-gyp
>npm i @saleae/ffi

>node test.js
1

test.js

const ffi = require("@saleae/ffi");

// Convert JSString to CString
function TEXT(text) {
  return Buffer.from(`${text}\0`, "ucs2");
}

// Import user32
const user32 = new ffi.Library("user32", {
  // 返回值类型,参数列表类型
  MessageBoxW: ["int32", ["int32", "string", "string", "int32"]],
  SetCursorPos: ["bool", ["int32", "int32"]],
});

const OK_or_Cancel = user32.MessageBoxW(0, TEXT("Hello from Node.js!"), TEXT("Hello, World!"), 1);

console.log(OK_or_Cancel);

// user32.SetCursorPos(0, 0);

获取窗口句柄

const ffi = require("@saleae/ffi");

// Convert JSString to CString
function TEXT(text) {
  return Buffer.from(`${text}\0`, "ucs2");
}

// Import user32
const user32 = new ffi.Library("user32", {
  FindWindowW: ["int32", ["string", "string"]],
});

var hwnd = user32.FindWindowW(TEXT('Progman'), null);
console.log(hwnd); // 65814
console.log(hwnd.toString(16)); // 10116


如果你熟练win32 api 知道自己在做什么,你可以使用 node-windows-x64

install

λ npm i node-windows-x64

调用 MessageBoxA 示例

import { node_windows_x64 as nw } from "node-windows-x64";

nw["user32.messageboxa"](0, "body", "title", 2);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM