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