nodejs 调用win32 api

时间:2023-03-10 07:08:22
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, // 要创建的消息框的所有者窗口的句柄。如果此参数为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

nodejs 调用win32 api