Electron 应用程序不适用于 `ffi-napi` 模块

Electron App Not Working With `ffi-napi` Module

我有以下电子代码,

//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);

我的主要 objective 是访问电子应用程序中的 windows.h 功能。 ffi-node 作为单独的节点应用程序似乎工作正常。

然而,当我将它嵌入电子应用程序时 运行 electron main.js 它看起来像 运行ning 几秒钟,然后退出。没有错误代码什么都没有。当我尝试删除行 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后跟[=​​12=]