关于electron调用C库出现的问题!

最近用electron做一个桌面端项目,作一个上位机程序和硬件CH375通信这么个功能。因为需要用nodejs去调用C库的函数,在node环境中用 ffi 测试没有问题,但是在electron中无法使用 ffi 这个库,查了网上很多资料试了多种方法,也没有成功。希望有调用成功过的人士帮我解答,谢谢了

在electron中运行报错:

App threw an error during load
Error: The module '\\?\D:\szl\projects\elect8\node_modules\ref\build\Release\binding.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 69. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:160:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:722:18)
    at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:160:31)
    at Module.load (internal/modules/cjs/loader.js:602:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:541:12)
    at Function.Module._load (internal/modules/cjs/loader.js:533:3)
    at Module.require (internal/modules/cjs/loader.js:640:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at bindings (D:\szl\projects\elect8\node_modules\bindings\bindings.js:76:44)
    at Object. (D:\szl\projects\elect8\node_modules\ref\lib\ref.js:5:47)

我的package.json:

{
  "name": "elect8",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "dependencies": {
    "electron": "^4.2.12",
    "electron-rebuild": "^3.2.9",
    "ffi": "^2.3.0",
    "ref": "^1.3.5"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "rebuild": "electron-rebuild -d=http://npm.taobao.org/mirrors/atom-shell -v 4.2.12 -m=./",
    "start": "electron ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

主程序main.js:

const ffi = require("ffi");
const User32 = ffi.Library('user32', {
    'GetWindowLongPtrW': ['int', ['int', 'int']],
    'SetWindowLongPtrW': ['int', ['int', 'int', 'long']],
    'GetSystemMenu': ['int', ['int', 'bool']],
    'DestroyWindow': ['bool', ['int']]
});
console.log(User32.GetWindowLongPtrW);

用node main.js测试正常(node版本10.11.0)

D:\szl\projects\elect8>node main.js
{ [Function: proxy] async: [Function] }

用npm run start 报错

D:\szl\projects\elect8>npm run start

> elect8@1.0.0 start D:\szl\projects\elect8
> electron .


App threw an error during load
Error: The module '\\?\D:\szl\projects\elect8\node_modules\ref\build\Release\binding.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 69. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:160:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:722:18)
    at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:160:31)
    at Module.load (internal/modules/cjs/loader.js:602:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:541:12)
    at Function.Module._load (internal/modules/cjs/loader.js:533:3)
    at Module.require (internal/modules/cjs/loader.js:640:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at bindings (D:\szl\projects\elect8\node_modules\bindings\bindings.js:76:44)
    at Object. (D:\szl\projects\elect8\node_modules\ref\lib\ref.js:5:47)

根据网上找的资料,我也尝试很多次,都失败了。比如下载electron-rebuild,到node_modules/ffi 里面去用node-gyp rebuild --runtime=electron --arch=x64 --target=4.2.12 --dist-url=https://npm.taobao.org/mirrors/atom-shell 命令也失败。我查了electron官网,说electron4.2.12里面用的node版本也是10.11.0 就是不知道问题出在哪,头大了

关于在electron端要调用c#的问题详解
如有帮助,望采纳
https://blog.csdn.net/lbn2676043895/article/details/89881180

这个错误提示提到了一个模块被编译成了一个不同的 Node.js 版本,而当前程序运行的 Node.js 版本需要另一个版本的模块。这意味着当前程序所使用的模块和程序运行的 Node.js 版本不匹配,导致了程序在加载时抛出了错误。

解决这个问题的方法就是重新编译或重新安装当前程序所使用的模块。可以通过执行 npm rebuild 或 npm install 命令来完成这个操作。这样,模块就会被重新编译成与当前程序运行的 Node.js 版本匹配的版本。这样就可以解决程序在加载时抛出错误的问题了。

看了一下
https://nodejs.org/en/download/releases/
nodejs都没有69版本的,
而且你用的版本都太老了,你把electron升级到最新,nodejs也安装一个对应的新版本再试试