我有以下 Electron 代码,
//main.js
const e = require("electron");
const ffi = require("ffi-napi");
const user32 = new ffi.Library("user32", {
GetKeyState: ["short", ["int32"]],
});
console.log(user32.GetKeyState(0x06) < 0);
function createWindow() {
let win = new e.BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
win.loadFile("index.html");
}
e.app.whenReady().then(createWindow);
我的主要目标是访问 Electron 应用程序中的 windows.h 函数。 ffi-node
似乎作为一个单独的 Node 应用程序运行良好。但是,当我将其嵌入到 Electron 应用程序中并运行
electron main.js
看起来它运行了几秒钟,然后退出。没有错误代码什么都没有。当我尝试删除 const ffi = require("ffi-napi");
行时和相应的方法调用,它似乎工作正常。我还尝试通过 electron-builder install-app-deps
重建应用程序它没有帮助。
最佳答案
我设法通过将代码更改为此来解决问题。
const { app, BrowserWindow } = require("electron");
const ffi = require("ffi-napi");
const user32 = new ffi.Library("user32", {
GetKeyState: ["short", ["int32"]],
});
console.log(user32.GetKeyState(0x06) < 0);
var win;
function createWindow() {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
win.loadFile("index.html");
}
app.on("ready", createWindow);
和npm install electron -g
后跟 npm link electron
关于node.js - Electron 应用程序无法使用 `ffi-napi` 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62576713/