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);
posted @ 2020-04-27 16:34  Ajanuw  阅读(6281)  评论(0编辑  收藏  举报