-
Notifications
You must be signed in to change notification settings - Fork 251
node
qiannian edited this page Jun 13, 2026
·
1 revision
Node.js 可以通过 winax 创建 op.opsoft COM 对象。下面示例同时包含已注册 COM 调用和免注册调用方式。
npm install winax
npm install ffi-napiwinax 和 ffi-napi 都是原生模块,Node.js、tools.dll、op_x86.dll/op_x64.dll 的位数必须一致。
适用于已经通过 regsvr32 或安装脚本注册过 op 插件的环境。
const winax = require("winax");
// 创建 op COM 对象
const op = new winax.Object("op.opsoft");
// 调用 op.Ver()
console.log("op version:", op.Ver());通过 tools.dll 的 setupW 加载 op_x86.dll 或 op_x64.dll,不需要使用 regsvr32 注册插件。
示例假设脚本旁边有如下目录结构:
demo.js
op/
x86/
tools.dll
op_x86.dll
x64/
tools.dll
op_x64.dll
const fs = require("node:fs");
const path = require("node:path");
const ffi = require("ffi-napi");
const winax = require("winax");
function toWideBuffer(value) {
// setupW 接收 wchar_t*,Windows 下 wchar_t 是 UTF-16LE
return Buffer.from(value + "\0", "utf16le");
}
function setupOpRegFree(pluginRoot) {
// 根据当前 Node.js 进程位数选择 x86 或 x64
const isX64 = process.arch === "x64";
const arch = isX64 ? "x64" : "x86";
const opName = isX64 ? "op_x64.dll" : "op_x86.dll";
const runtimeDir = path.join(pluginRoot, arch);
const toolsDll = path.join(runtimeDir, "tools.dll");
const opDll = path.join(runtimeDir, opName);
if (!fs.existsSync(toolsDll)) {
throw new Error(`找不到免注册工具: ${toolsDll}`);
}
if (!fs.existsSync(opDll)) {
throw new Error(`找不到 op 插件: ${opDll}`);
}
// setupW 是 cdecl 调用约定,ffi-napi 默认使用 cdecl
const tools = ffi.Library(toolsDll, {
setupW: ["int", ["pointer"]],
});
// setupW 必须在 new winax.Object 之前调用
const ret = tools.setupW(toWideBuffer(opDll));
if (ret !== 1) {
throw new Error(`免注册加载失败: ${opDll}`);
}
// 返回 tools,保留 DLL 句柄在当前进程中有效
return tools;
}
// 当前脚本旁边的 op 目录,也可以改成自己的插件释放目录
const toolHandle = setupOpRegFree(path.join(__dirname, "op"));
// setupW 成功后,就可以按普通 COM 方式创建对象
const op = new winax.Object("op.opsoft");
// 调用 op.Ver()
console.log("op version:", op.Ver());-
setupW只在当前进程内生效,不会写入系统注册表。 -
setupW是cdecl调用约定,ffi-napi默认按cdecl调用。 -
tools.dll安装了当前进程内的 COM Hook,加载成功后保持到进程退出即可。 - 如果是 64 位 Node.js,请使用
x64/tools.dll和x64/op_x64.dll。 - 如果是 32 位 Node.js,请使用
x86/tools.dll和x86/op_x86.dll。