nodejs 调用win32 api

时间:2020-04-27 16:50:09   收藏:0   阅读:520
>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, // 要创建的消息框的所有者窗口的句柄。如果此参数为NULL,则消息框没有所有者窗口
  TEXT("Hello from Node.js!"), // 要显示的消息。如果字符串包含多行,则可以在每行之间使用回车符和/或换行符来分隔行
  TEXT("Hello, World!"), // 对话框标题。如果此参数为NULL,则默认标题为Error。
  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

技术分享图片

原文:https://www.cnblogs.com/ajanuw/p/12787653.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!