nodejs与c语言交互应用实例
nodejs与c/c++交互目前主流的方式有两种,node addon c++ 和 node-ffi .
1、node addon c++
1)nodejs从c语言读取数据
addon.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> typedef struct
{
double lon;
double lat;
}gps_info_t; gps_info_t* gps; void get_gps_shm_init(void)
{
gps = (gps_info_t *)malloc(sizeof(gps_info_t));
} Napi::Object CreateObject(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object obj = Napi::Object::New(env); gps->lon = 55.5;
gps->lat = 66.6; printf("send lon: %f\n", gps->lon);
printf("send lat: %f\n", gps->lat); obj.Set(Napi::String::New(env, "lon"), gps->lon);
obj.Set(Napi::String::New(env, "lat"), gps->lat); return obj;
} Napi::Object Init(Napi::Env env, Napi::Object exports) {
get_gps_shm_init();
return Napi::Function::New(env, CreateObject, "createObject");
} NODE_API_MODULE(addon, Init)
addon.js
var addon = require('bindings')('addon');
var obj = addon('gps');
console.log(obj.lon);
console.log(obj.lat);
module.exports.obj = obj;
2)nodejs向c语言写数据
addon.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> typedef struct
{
char *user_name;
char *passwd;
char *dev_id;
}user_info_t; user_info_t* user; void put_user_info_shm_init(void)
{
user = (user_info_t *)malloc(sizeof(user_info_t));
user->user_name = (char*)malloc(*sizeof(char));
user->passwd = (char*)malloc(*sizeof(char));
user->dev_id = (char*)malloc(*sizeof(char));
} Napi::Object CreateObject(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object obj = Napi::Object::New(env); memset(user->user_name, , );
memset(user->passwd, , );
memset(user->dev_id, , );
memcpy(user->user_name, info[].ToString(), );
memcpy(user->passwd, info[].ToString(), );
memcpy(user->dev_id, info[].ToString(), ); printf("recv user info (user_name): %s\n", user->user_name);
printf("recv user info (passwd): %s\n", user->passwd);
printf("recv user info (dev_id): %s\n", user->dev_id); return obj;
} Napi::Object Init(Napi::Env env, Napi::Object exports) {
put_user_info_shm_init();
return Napi::Function::New(env, CreateObject, "createObject");
} NODE_API_MODULE(addon, Init)
addon.js
var addon = require('bindings')('addon');
var user_info = {
user_name = 'zdd';
passwd = '123';
dev_id = '65535'
}
var obj = addon(user_info.user_name,user_info.passwd,user_info.dev_id);
module.exports.user_info = user_info;
nodejs的C/C++拓展,将c/c++源码编译成js模板库
ubuntu下编译
node-gyp configure
node-gyp build
node addon.js
交叉编译
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++
export LD=arm-linux-gnueabihf-ld
export RAINLIB=arm-linux-gnueabihf-rainlib
export AR=arm-linux-gnueabihf-ar
export LINK=arm-linux-gnueabihf-g++
node-gyp configure --arch=arm
node-gyp build
node addon.js
2、node-ffi
1)nodejs从c语言读取数据
factorial.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h> #if defined(WIN32) || defined(_WIN32)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif typedef struct
{
double lon;
double lat;
}gps_info_t; gps_info_t* gps; void get_gps_shm_init(void)
{
gps = (gps_info_t *)malloc(sizeof(gps_info_t));
} EXPORT gps_info_t* get_gps_info(void) {
get_gps_shm_init();
gps->lon = 55.5;
gps->lat = 55.5;return gps;
}
factorial.js
var ffi = require('../node-ffi-master/')
var refStruct = require('ref-struct');
var refArray = require('ref-array');
var ref = require('ref');
//gps date struct
var gps = refStruct({
'lon':ref.types.double,
'lat':ref.types.double,
});
var gpsStructArrayType = refArray(gps);
var gps_info = gpsStructArrayType();
var libfactorial = ffi.Library('./libfactorial', {
'get_gps_info': [ gpsStructArrayType, [ 'void' ] ],
});
gps_info = libfactorial.get_gps_info()
console.log('Your output: ' + gps_info[].lon)
console.log('Your output: ' + gps_info[].lat)
2)nodejs向c语言写数据
factorial.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h> #if defined(WIN32) || defined(_WIN32)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif typedef struct
{
char *user_name;
char *passwd;
char *dev_id;
}user_info_t; user_info_t* user; void put_user_info_shm_init(void)
{
user = (user_info_t *)malloc(sizeof(user_info_t));
user->user_name = (char*)malloc(*sizeof(char));
user->passwd = (char*)malloc(*sizeof(char));
user->dev_id = (char*)malloc(*sizeof(char));
} EXPORT void put_user_info(user_info_t* user_info) {
put_user_info_shm_init();
printf("get userinfo(user_name):%s",user_info->user_name);
printf("get userinfo(user_name):%s",user_info->passwd);
printf("get userinfo(user_name):%s",user_info->dev_id);
}
factorial.js
var ffi = require('../node-ffi-master/')
var refStruct = require('ref-struct');
var refArray = require('ref-array');
var ref = require('ref');
//ui date struct
var user = refStruct({
'user_name':'string',
'passwd':'string',
'dev_id':'string',
});
var userStructArrayType = refArray(user);
var user_info = userStructArrayType();
user_info[].user_name = 'zdd';
user_info[].passwd = '';
user_info[].dev_id = '';
var libfactorial = ffi.Library('./libfactorial', {
'put_user_info': [ 'void', [ userStructArrayType ] ]
});
libfactorial.put_user_info(user_info)
console.log('Your input: ' + user_info[].user_name)
console.log('Your input: ' + user_info[].passwd)
console.log('Your input: ' + user_info[].dev_id)
gcc / arm-linux-gnueabihf-gcc -shared -fpic factorial.c -o libfactorial.so
node factorial.js
上面两种方式写了个demo放在github上了
https://github.com/zhoudd1/nodejs_call_c_cc
3、还有一种小众化的方式
通过child_process 方式调用EXE进程来实现数据交互。
//同步的方式
letout = cp.execFileSync("testdll.exe", ["arg1","arg2"],{});
//异步的方式
console.log(stdout);
})
这种方式NODE 会接管stdout 和 stderr ,exe中把结果通过 printf 输出。
缺点:需要先生成EXE,EXE 中调用 DLL/so 并且返回结果。
优点:不需要配置 FFI,不受DLL/so 的影响
更多细节
https://www.cnblogs.com/chyingp/p/node-learning-guide-child_process.html
https://blog.csdn.net/zeping891103/article/details/52230175
nodejs与c语言交互应用实例的更多相关文章
- python与c语言交互应用实例
1.python向c语言写数据 1) 先将接收端编译成一个共享链接库gcc/arm-linux-gnueabihf-gcc -o bluetooth_proxy.so -shared -fPIC bl ...
- UML标准建模语言与应用实例
一.基本信息 标题:UML标准建模语言与应用实例 时间:2012 出版源:科技创新导报 领域分类:UML标准建模语言 面向对象 系统分析与设计 二.研究背景 问题定义:UML建模语言用图形来表现典型的 ...
- linux下C语言多线程编程实例
用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...
- golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍
golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍 go语言爬虫框架:gocolly/colly,goquery,colly,chrom ...
- FFI (语言交互接口(Foreign Function Interface))
FFI(Foreign Function Interface)是用来与其它语言交互的接口, 在有些语言里面称为语言绑定(language bindings), Java 里面一般称为 JNI(Java ...
- nodejs实现前后端交互
本人nodejs入门级选手,站在巨人(文殊)的肩膀上学习了一些相关知识,有幸在项目中使用nodejs实现了前后端交互,因此,将整个交互过程记录下来,方便以后学习. 本文从宏观讲述nodejs实现前后端 ...
- react UI交互 简单实例
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
- nodejs+mongoose操作mongodb副本集实例
继上一篇设置mongodb副本集之后,开始使用nodejs访问mongodb副本集: 1:创建项目 express 项目名称 2:npm install mongoose 安装mongo ...
- 第4章-Vue.js 交互及实例的生命周期
一.学习目标 了解实例生命周期的过程 理解钩子函数的作用 掌握Vue.js过滤器的使用方法 (重点) 能够使用网络请求进行前后端交互 (重点.难点) 二.交互的基本概念 2.1.前端和后端的概念 说明 ...
随机推荐
- 网卡优化RPS/RFS
网卡优化 RSS receive side scaling,网卡多队列,需要硬件支持.网卡接收到网络数据包后,要发送一个硬件中断,通知CPU取数据包.默认配置,都是由CPU0去做. RPS recei ...
- php常用的正则表达式
1. 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用:2. "^\d+$" //非负整数(正整数 + 0)3. "^[0-9]*[1-9][0 ...
- iPhone开发秘籍(第2版)--具体书签版
http://download.csdn.net/download/fksec/4872499
- zabbix监控数据库
Zabbix通过percona监控MySQL 因为Zabbix自带的MySQL监控没有提供可以直接使用的Key,所以一般不采用,业界的同学们都使用Percona Monitoring Plugin ...
- _BV()
#define _BV(bit) (1 << (bit)) _BV()是把1左移N位的函数._BV(7)相当于(1<<7) 常用于位的置位或清零 示例解析: PC7=7; PO ...
- git clone 故障 fatal could not create work tree dir
问题如上图,原因是openWRT目录权限的问题,该目录是新创建的查看目录权限后发现该目录只对root有读写权限,对所有者及其他用户无读写权限.最简单的chmod 777 openWRT即可解决问题.
- 解决request.getRemoteAddr()获取的值为0:0:0:0:0:0:0:1这个小问题
症状: Windows操作系统,eclipse开发环境下,在本机上使用http://localhost:8080/...访问本机上的页面,使用tomcat作为服务器 在Servlet或者Action中 ...
- html 处理
近期做了一个后台管理网站,后台页面都是Html页面,里面再通过ajax访问后台服务.要做到比较好的用户体验,即:如果用户没有登录或没有权限马上调到登录页面,而不是等到页面加载后再ajax时判断是否登录 ...
- CLion 2017 注册码
注册码使用时间2017-2018 CNEKJPQZEX-eyJsaWNlbnNlSWQiOiJDTkVLSlBRWkVYIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNz ...
- Unity的 Stats 窗体, Batched、SetPass、Draw Call 等
孙广东 2015.8.12 在Game View 中的右上角有一个统计数据 Stats button.当按下button时.覆盖窗体显示,可用于优化性能的实时渲染统计信息. 确切的统计数据显示生成目 ...