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.前端和后端的概念 说明 ...
随机推荐
- Android多媒体系列2:利用MediaRecorder实现录音
- selenium:chromedriver与chrome版本的对应关系
转自:http://blog.csdn.NET/huilan_same/article/details/51896672 再使用selenium打开chrome浏览器的时候,需要用chromedriv ...
- 第二百六十一节,Tornado框架模板引擎本质
Tornado框架模板引擎本质 只需要了解一下即可 本篇就来详细的剖析模板处理的整个过程. 上图是返回给用户一个html文件的整个流程,较之前的Demo多了绿色流线的步骤,其实就是把[self.wri ...
- OAuth2.0 介绍
一.基本协议流程: (1) Client请求RO(Resource Owner)的授权:请求中一般包含:要访问的资源路径,操作类型,Client的身份等信息.(2) RO批准授权:并将“授权证据”发送 ...
- 股票指数kdj,sar,macd
http://blog.eastmoney.com/gulingqianketong2011/blog_120832611.html http://blog.sina.com.cn/s/blog_a3 ...
- 【BZOJ】1638: [Usaco2007 Mar]Cow Traffic 奶牛交通(dfs+dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1638 一条边(u, v)经过的数量=度0到u的数量×v到n的数量 两次记忆化dfs算出他们即可 #i ...
- 插件之下拉框Select2
select2为代替常规的select而出现,可自定义select的样式,最明显的功能就是集合中可以搜索 关于浏览器要求,ie8+,Chrome 8+,Firefox 10+,Safari 3+,Op ...
- MFC存储图片到SQL Server数据库
第一步:建立数据库表,比如:id char,pic image. 第二步:建立MFC单文档应用程序,再添加类CMyRecordset,基类选择CRecordset,导入数据库的刚建立的表. 第三步:在 ...
- springmvc传递有特殊字符的路径参数
因为hostKey这里是IP(例如127.0.0.1)包含了特殊字符. 实际传递到后台的是127.0.0少了一截 @GetMapping("/metrics/jobId/{jobId}/{ ...
- 码农深耕 - 说说IDisposable
概要 C#提供了方便的垃圾回收机制,使我们几乎不再需要为资源管理费心.可事实上,能被垃圾回收释放掉的只是托管资源,非托管资源还是需要我们手动释放.而为了实现这一目的,C#提供了 IDisposable ...