electron调用C++动态链接库

1、实验环境
electron调用C++动态链接库
文章图片

2、centOS下安装nodejs 下载已经编译好的node.js
wget http://nodejs.org/dist/latest-v5.x/node-v5.7.1-linux-x64.tar.gz
tar zxvf node-v5.7.1-linux-x64.tar.gz
mv node-v5.7.1-linux-x64/usr/local/node
配置NODE_HOME,进入profile编辑环境变量
vim /etc/profile
#set for nodejs
export NODE_HOME=/usr/local/node
export PATH=$NODE_HOME/bin:$PATH
:wq保存并退出,编译/etc/profile使配置生效
source /etc/profile
验证是否安装配置成功
node -v
输出 v5.7.1表示配置成功
npm模块安装路径
/usr/local/node/bin/npm
3、安装electron # mkdir /usr/local/electron
# cd /usr/local/electron
# wget https://npm.taobao.org/mirrors/electron/0.36.9/electron-v0.36.9-linux-x64.zip
# unzipelectron-v0.36.9-linux-x64.zip
# vim /etc/profile
export ELECTRON=/usr/local/electron
export PATH=$ELECTRON:$PATH
# source /etc/profile
4、编写node.js调用C/C++例子 从一个简单的例子开始,说明JavaScript调用C/C++的流程。示例函数包含3部分:配置文件、js文件、cc文件
mkdir /root/test
cd /root/test
4.1、配置文件
binding.gyp(一定要叫这个名字)
简单的配置如下:
{
"targets": [
{
"target_name": "test",
"sources":[ "test.cc" ]
}
]
}
4.2、js文件
test.js,hello接受两个参数,一个字符串,一个回调函数:
var test = require('./build/Release/test');
test.hello('test', function(data) {
console.log(data);
});
4.3、cc文件
test.cc,文件应用两个头部node.h和v8.h,整体结构和node的module非常相似,都是先定义Function/Object,然后export:
#include
#include

using namespace v8;

// 传入了两个参数,args[0]字符串,args[1] 回调函数
void hello(const FunctionCallbackInfo& args) {
// 使用 HandleScope 来管理生命周期
Isolate* isolate =Isolate::GetCurrent();
HandleScopescope(isolate);

// 判断参数格式和格式
if (args.Length() < 2|| !args[0]->IsString()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}

// callback, 使用Cast方法来转换
Localcallback = Local::Cast(args[1]);
Localargv[1] = {
// 拼接String
String::Concat(Local::Cast(args[0]),String::NewFromUtf8(isolate, " apexsoft"))
};
// 调用回调, 参数: 当前上下文,参数个数,参数列表
callback->Call(isolate->GetCurrentContext()->Global(), 1,argv);
}

// 相当于在 exports 对象中添加 { hello: hello }
void init(Handle exports) {
NODE_SET_METHOD(exports,"hello", hello);
}

// 将 export 对象暴露出去
// 原型`NODE_MODULE(module_name, Initialize)`
NODE_MODULE(test, init);
4.4、编译和使用
安装node-gyp
npm install node-gyp -g
在项目根目录下使用:
node-gyp configure
node-gyp build
5、让Electron去识别C/C++ 5.1、修改test.js
vim test.js
varhttp = require("http");

http.createServer(function(req,res) {
res.writeHead( 200 ,{"Content-Type":"text/html"});
var test = require('./build/Release/test');
var data01;
test.hello('hello', function(data) {
data01=data;
});
res.write(data01);
res.end("apexsoft.com.cn
");
}).listen(3000);
console.log("HTTP server is listening at port 3000.");

5.2、main.js
vim main.js
'use strict';

const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

// Keep a global reference of the window object, if you don't,the window will
// be closed automatically when the JavaScript object is garbagecollected.
let mainWindow;

function createWindow () {
// Create the browserwindow.
mainWindow = newBrowserWindow({width: 800, height: 600});

// and load theindex.html of the app.
//mainWindow.loadURL('file://' + __dirname + '/index.html');
mainWindow.loadURL("http:192.168.1.172:3000");
// Open the DevTools.
mainWindow.webContents.openDevTools();

// Emitted when thewindow is closed.
mainWindow.on('closed',function() {
// Dereference thewindow object, usually you would store windows
// in an array if yourapp supports multi windows, this is the time
// when you shoulddelete the corresponding element.
mainWindow = null;
});
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is commonfor applications and their menu bar
// to stay active untilthe user quits explicitly with Cmd + Q
if (process.platform !=='darwin') {
app.quit();
}
});

app.on('activate', function () {
// On OS X it's commonto re-create a window in the app when the
// dock icon is clickedand there are no other windows open.
if (mainWindow === null){
createWindow();
}
});
5.3、package.json
vim package.json
{
"name":"electron-quick-start",
"version": "1.0.0",
"description": "A minimalElectron application",
"main": "main.js",
"scripts": {
"start": "electronmain.js"
},
"repository": {
【electron调用C++动态链接库】"type": "git",
"url":"git+https://github.com/atom/electron-quick-start.git"
},
"keywords": [
"Electron",
"quick",
"start",
"tutorial"
],
"author": "GitHub",
"license": "CC0-1.0",
"bugs": {
"url": "https://github.com/atom/electron-quick-start/issues"
},
"homepage":"https://github.com/atom/electron-quick-start#readme",
"devDependencies": {
"electron-prebuilt":"^0.36.0"
}
}
6、使用方法 窗口一:启动test.js
node test.js
窗口二:启动electron
electron /root/test
运行效果
electron调用C++动态链接库
文章图片

7、打包 使用命令完成打包工具命令行的安装
npm install -g asar
打包你的工程目录
asar pack test test.asar
生成app.asar。在windows下,可以将app.asar直接拉入electron.exe下就可以用了。


转载于:https://blog.51cto.com/corasql/1748400

    推荐阅读