我对 Node JS 和 Electron 完全陌生。我正在尝试使用 Electron 和 Node JS 将 C++ 与 HTML 集成。我已经经历了一些例子:GIT
我正在尝试做的是从我的网页的由 Electron 加载的 javascript 调用 native 函数 (hello())。我已使用 node-gyp configure
生成我的 Visual Studio 解决方案文件。 (.sln)。后来我用 Visual Studio 2013 Express 编译了我的代码,它成功地在 build\Release 文件夹中生成了我的 .node 文件。
这是我的 index.js 文件:
var addon = require('./build/Release/hello.node');
console.log(addon.hello());
当我简单地使用 node index.js
运行它时,它给出了我想要的输出:
world
但是当我使用 Electron 时,问题就来了。我正在使用 Electron 二进制文件(32 位)来运行我的网页。
以下是我的main.js文件:
var app = require('app'); // Module to control application life.
var BrowserWindow = require('browser-window'); // Module to create native browser window.
require('crash-reporter').start();
var mainWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
mainWindow = new BrowserWindow({width: 1366, height: 768});
mainWindow.loadUrl("file://" + __dirname + "/HtmlFile/index.html");
mainWindow.on('closed', function() {
mainWindow = null;
});
});
现在这是我调用 native 插件的 javascript:
//************* My Functional logic **************
//************************************************
var addon = require('../build/Release/hello');
alert(addon.hello());
当我运行或加载此页面时,出现以下错误:
Uncaught Error: %1 is not a valid Win32 application. ATOM_SHELL_ASAR.js:137
C:\Users\Administrator\Desktop\MyAPP\build\Release\hello.node
以下是我的package.json
:
{
"name": "MyAPP",
"version": "1.0.0",
"description": "Desc",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"nan": "^2.0.9"
},
"gypfile": true
}
这是我的binding.gyp
:
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cc" ],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
}
最佳答案
看起来您可能没有配置正确的二进制文件。抱歉不确定这是否适用于 native 模块,但您可以尝试 rebuilding ...
注意:请确保您的 node-gyp 命令具有正确的参数(如果这是您重建的方式)。
-
--target=<your electron version>
-
--target_platform=win32
(不在示例链接中,但您似乎使用的是 Windows) -
--arch=<your architecture>
(x64 = 64 位,x86 = 32 位)
关于c++ - 在 Electron 中调用 Node Native Addons (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986826/